Running Google Maps in an AVD

After a couple failed attempts, I managed to successfully run com.example.mapdemo in an Android Virtual Device (AVD). It’s not tough, but required cobbling together instructions from a variety of sources.

The following instructions work for the ADT installed under Ubuntu, version linux-x86-20130522. I make no guarantee that these steps will work for you. It’s possible that some of the steps are extraneous.

  1. Using the Android SDK Manager, install Google Play Services (v7).

  2. Using the Android SDK Manager, install Google APIs (v3) (located under Android 4.2.2 (API 17)).

  3. Using the Android Virtual Device Manager, create an AVD with a target of Android 4.2.2 – API Level 17. (I created mine with a 4.65″ 720p, not sure if that’s important.)

  4. Start the AVD

  5. Download two application package files (APKs):

    com.google.android.gms-(3.1.36).apk
    com.android.vending-4.1.6.apk

  6. Install the APKs using adb (located in the ADT directory ./sdk/platform-tools):

    adb install com.android.vending-4.1.6.apk
    adb install com.google.android.gms-(3.1.36).apk

  7. Restart the AVD

  8. Copy ./sdk/extras/google/google_play_services/libproject/google-play-services_lib/ to your Android projects area.

  9. Create a project for Google Play Services (projects needing to use Google Maps will need to depend on this):
    Import->Android->Existing Android Code Into Workspace.
    Navigate to ./sdk/extras/google/google_play_services/libproject and select google-play-services-lib.

  10. Create a project for the map samples:
    Import->Android-Existing Android Code Into Workspace.
    Navigate to ./sdk/extras/google/google_play_services/samples and select the maps directory.

  11. Create a Google Maps API key (note: these steps supposedly will not work for your production deployment):

    • Copy the SHA1 from your Android Debug keystore. Here’s the instructions I used under Ubuntu to display the contents of the keystore:

      keytool -list -v -keystore ~/.android/debug.keystore

      By default, the password for the keystore is android.

    • Go to https://code.google.com/apis/console and select API Access from the menu (to the left).

    • Click the button marked Create new Android Key….

    • Paste in your SHA1, append a semicolon, then append com.example.mapdemo

    • Click the Create button.

    • Copy the API key into the paste buffer (you will use it in the next step).

  12. Configure the map sample project:

    • From the project’s build path settings, Add External JAR: ./sdk/extras/android/support/v4/android-support-v4.jar

    • Go to the Order and Export tab, and export android-support-v4.jar

    • Go to Properties->Android. In the Library section, add the google_play_services_lib project as a dependency. (You will need to open google-play-services_lib if it is not already.)

  13. Edit AndroidManifest.xml to contain your API key: In MainActivityManifest.xml, ensure you have a meta-data element with something like the following:

     <meta-data android:name="com.google.android.maps.v2.API_KEY"
               android:value="AIzaSyCawgR4hVSiAzHIg4wJMbg7jvNyt-KZNgs"/>
    

    Substitute your API key for the android:value attribute. The meta-data element is a direct child of <application>.

  14. Edit AndroidManifest.xml to contain appropriate permissions. For example (don’t forget to substitute your package name):

    <permission
            android:name="com.langrsoft.plangrok.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />
    
        <uses-permission android:name="com.langrsoft.plangrok.permission.MAPS_RECEIVE" />
    
  15. Run it!

    Here are some of the links I used to piece this together:

    Google APIs Console
    Install Google Play Services SDK
    Sample code
    Getting the Google Maps Android API
    Getting the Google Maps API Key
    Emil’s Design (a useful blog entry)

    Please let me know if I missed any steps or if you discovered something else.

Atom