Document Location SDK for Android Guides Set Up Your Project Initialization & Compliance

Initialization & Compliance

Add the AMAP API Key

To ensure the AMAP Android SDK functions correctly, you must apply for an AMAP API key and configure it in your project.

Add the following code to your project's AndroidManifest.xml file:

<application
         android:icon="@drawable/icon"
         android:label="@string/app_name" >
         <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="Please enter your User Key"/>
            ……
</application>

Privacy Compliance Interface

1. Location Privacy Compliance

The following interfaces must be called before instantiating AMapLocationClient:

/**
 * Sets whether the privacy policy contains the AMap privacy policy and displays a user authorization dialog.
 * <b>Must be called before instantiating AMapLocationClient</b>
 *
 * @param context
 * @param isContains Whether the privacy policy includes the AMap privacy policy. true: includes
 * @param isShow     Whether to display a dialog to inform the user about the privacy policy. true: display
 * @since 5.6.0
 */
public static void updatePrivacyShow(Context context, boolean isContains, boolean isShow);

/**
 * Sets whether the user has agreed to the privacy policy.
 * <b>Must be called before instantiating AMapLocationClient</b>
 *
 * @param context
 * @param isAgree  Whether the user has agreed to the privacy policy. true: user agreed
 * @since 5.6.0
 */
public static void updatePrivacyAgree(Context context, boolean isAgree);

2. Perform Privacy Compliance Check

Before constructing AMapLocationClient, you must perform a privacy compliance check. Call the following interfaces to ensure compliance:

AMapLocationClient.updatePrivacyShow(context, true, true);
AMapLocationClient.updatePrivacyAgree(context, true);

3. Handle Exceptions When Constructing AMapLocationClient

Wrap the constructor call in a try-catch block to handle any exceptions:

try {
    mClient = new AMapLocationClient(context);
} catch (Exception e) {
    // Handle exception
}

4. Null Check Before Using mClient

Always perform a null check before using the mClient object:

if (mClient != null) {
    // TODO: Use mClient
}

Personal and Device Information Collection Settings

/**
 * Sets whether to allow the collection of personal and device information.
 *
 * @param collectEnable true: allow collection, false: disallow collection
 */
AMapUtilCoreApi.setCollectInfoEnable(boolean collectEnable);

Code Obfuscation

When generating the APK with code obfuscation enabled, add the following rules to your ProGuard configuration file. If warnings are reported for a specific package, add a -dontwarn statement for that package.

# 3D Map (before V5.0.0)
-keep   class com.amap.api.maps.**{*;}
-keep   class com.autonavi.amap.mapcore.*{*;}
-keep   class com.amap.api.trace.**{*;}

# 3D Map (V5.0.0 and later)
-keep   class com.amap.api.maps.**{*;}
-keep   class com.autonavi.**{*;}
-keep   class com.amap.api.trace.**{*;}

# Location
-keep class com.amap.api.location.**{*;}
-keep class com.amap.api.fence.**{*;}
-keep class com.loc.**{*;}
-keep class com.autonavi.aps.amapapi.model.**{*;}

# Search
-keep   class com.amap.api.services.**{*;}

# 2D Map
-keep class com.amap.api.maps2d.**{*;}
-keep class com.amap.api.mapcore2d.**{*;}

# Navigation
-keep class com.amap.api.navi.**{*;}
-keep class com.autonavi.**{*;}

Compatibility

The SDK supports Android 2.3 and later.