If you see it

Live for passion

Android App With Google Map API V2

1. Get logistic things out of your way

I assume you have your Google Android SDK downloaded.

Setting up these in .bash_profile to enable you using command like adb and emulator.

1
2
export ANDROID_HOME='/path/to/adt-bundle-mac-x86_64/sdk'
export PATH=${PATH}:${ANDROID_HOME}/platform-tools:${ANDROID_HOME}/tools

Build your environment

Running your Android SDK Manager from your teminal:

1
android sdk

Select Android 4.2.2 (API 17), Google Play Services under extra(scroll down to the bottom) alt text alt text

Install the packages, it will probably take a while… Coffee?

More details about: Managing avds from command line

Create Device Emulator from your command and start your emulator
1
2
echo no  | android create avd -n BiteMoi -t android-17 --skin WVGA800 --force --abi armeabi-v7a  
emulator -avd BiteMoi -partition-size 512 -no-audio -no-boot-anim

Updated You can create your Virtual Device as follows.
Run android sdk

Choose “Manage AVDs…” like this:

alt text

Create AVD.

alt text

Click OK.

Debuging notes

Installation and uninstall by apk or package name

1
2
adb install target/bitemoi-1.0.apk  # Install and uninstall apk through adb  
adb uninstall com.thoughtworks.bitemoi # Unistall by given package name

Logcat

1
2
adb logcat -c    # Clean logcat log
adb logcat    # Start monitoring logcat, live version
References

Command line help from Google SDK


2. Build your app

Create new Android Application

alt text

alt text

Next if you don’t have anything else to modify.

alt text

AndroidManifest.xml

AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thoughtworks.bitemoi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.thoughtworks.bitemoi.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <permission
        android:name="com.thoughtworks.bitemoi.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.thoughtworks.bitemoi.MapActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

</manifest>
Generate your Google Android Map API Key

Find your Google API Console, under left sidebar “Services”, choose the Google Maps Android API v2. Like the following picture shows. This is important.

alt text

Then under “API Access”, choose “Create new Android key…” button. You can follow the instruction there and paste your SHA-1 along with your package name there to get a production key, but for debugging purpose, I did the following.

1
2
3
cd ~/.android   
$ keytool -list -v -alias androiddebugkey -keystore debug.keystore \
-storepass android -keypass android # using debug.keystore to discover the SHA-1

More read on : Google: generate dev keys

API Key
After you created, you will see a new section under “API Access”. alt text Note: this is a debug key for my app, will not work on yours.

Put this key to your AndroidManifest.xml.

Add the map fragment to your layout
Add this between <RelativeLayout></RelativeLayout>, you can preview the fragment in “Graphical Layout” tab.

Layout/activity_map.xml (partial)
1
2
3
4
5
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

Add Google Play Service Support

File -> Import -> Android -> Existing Android->Find the following path to Play Service Lib

/{YOUR_PATH_TO_ADK}/adt-bundle-mac-x86_64/sdk/extras/google/google_play_services/libproject/google-play-services_lib

alt text alt text After imported, google-play-service_lib is shown up as a project module. alt text

Right click on Your app, here is AndroidGoogleMap, choose properties, add the new lib support we just had. alt text alt text alt text

Now we can use Goolge Android Map v2 api to build our app

MapActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

package com.thoughtworks.bitemoi;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.UiSettings;

public class MapActivity extends Activity {

  private GoogleMap map;
  private UiSettings uiSettings;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_map);
      setUpMapIfNeeded();
  }

  private void setUpMapIfNeeded() {
      if (map == null) {
          map = ((MapFragment) getFragmentManager()
                  .findFragmentById(R.id.map)).getMap();
          if (map != null) {
              setUpMap();
          }
      }
  }

  private void setUpMap() {
      uiSettings = map.getUiSettings();
      map.setMyLocationEnabled(true);
  }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.map, menu);
        return true;
    }

}

Build and run your application while your AVD is running.

alt text

There may be some issues with Google Play Services not found or not updated. Till Apr 09, the hacky way which are intall these two apks works for me.

1
2
$ adb install com.android.vending-1.apk
$ adb install com.google.android.gms-3025110-v3.0.25\ \(583950-10\).apk

Reference Google Maps Android API v2 - Tutorial

And, more fun with build tool

This side project which I’m currently working on has maven support build in. Though you still need to create right AVD and install two apks.

GIT REPO: BiteMeAndroid

Requirements: Android sdk configured; installed mvn plugins for regular eclipse(see pics below); installed google play services apks dependencies(gms, vending).

steps for making BiteMoi working locally
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
git clone git://github.com/CapeDev/BiteMeAndroid.git
**Very Important**
Visit here : https://github.com/JakeWharton/gms-mvn-install, download the repo, follow the readme instruction, it will eventually added to your .m2/repository/com/google/android/ directory.

(Make sure you have Google-Play-Service-lib rev.5 installed for this project.)

mvn clean install
mvn eclipse:clean eclipse:eclipse
mkdir ../BiteMeAndroidWorkspace (Your new eclipse workspace)
Open regular Eclipse(mine is Juno at the moment), use the directory we just created as your workspace.
Import -> existing maven project (You need maven plugin for Eclipse first, you can install from marketPlace or some other way you can find)
Choose by click "Browse" button, navigate to /BiteMeAndroid (the github repo we just cloned at the beginning) as your "Root Directory"
"Next"-> You should see maven-compiler-plugin:2.5.1:compile; maven-compiler-plugin:2.5.1:testCompile with two green check mark ->"Finish" 
Right click on project if there is red cross -> "Maven" -> "Update project…"

-----Create IntelliJ 12 project 

mvn clean install idea:clean idea:idea

-----
Create device and start it:


echo no  | android create avd -n BiteMoiCMD -t android-17 --skin WVGA800 --force --abi armeabi-v7a

$android sdk
You should see "Android SDK Manager" window pop out, select "Tools" from the top
Select "Manage avds", you should see "Android Virtual Device Manager" window.
Select the device you just created or have
(Click "Edit", in the CPU/ABI area, change to Intel Atom(x86), because it's faster)
Click "Start…" and "Laurch"

-----
Troubleshooting, not in order: 

propertities -> builders -> uncheck Java Builder

If you see missing gms ish, install two apks from google-apk folder under BiteMoi git-repo by this: 
adb install XXX

check Propertities -> Java Build Path -> Source tab -> check target/unpack/apklibs/com.google.android.gms_google-play-services_apklib_5/src

project -> clean, clean and clean!!!

Disable Maven Nature -> mvn eclipse:clean eclipse:eclipse -> add Maven back

-----Install the apk and run

$ adb uninstall com.thoughtworks.bitemoi
$ mvn clean install
$ adb install target/bitemoi-1.0.apk
steps for IntelliJ 12
1
2
3
4
1) git clone git://github.com/CapeDev/BiteMeAndroid.git 
2) Open IntelliJ 12 IDE, "File" -> "Import Project" -> "Select File or Firectory to Import" window, navigate to root directory of the git repo we just cloned. -> "OK"
3) Check  radio "Import project from external model" and choose Maven from the list -> "Next"
4) Check "Import Maven projects automatically" -> "Next" to the end…

Regarding Eclipse plugin, this is what I have for my Eclipse alt text alt text

Reference for installing Google Play Service: GIT REPO: GMS MVN INSTALL

UPDATE

Downloaded the newest google android sdk (Jun 25, 2013)

1
2
3
4
5
export PATH=${PATH}:${ANDROID_HOME}/platform-tools:${ANDROID_HOME}/tools:${ANDROID_HOME}/build-tools

$ ln -sf ../build-tools/android-4.2.2/aapt aapt
$ ln -sf ../build-tools/android-4.2.2/lib lib
$ ln -sf ../build-tools/android-4.2.2/aidl aidl

Comments