9 min read

In this article by Raj Amal, author of the book Learning Android Google Maps, we will cover the following topics:

  • Generating an SHA1 fingerprint in the Windows, Linux, and Mac OS X
  • Registering our application in the Google Developer Console
  • Configuring Google Play services with our application
  • Adding permissions and defining an API key

Generating the SHA1 fingerprint

Let’s learn about generating the SHA1 fingerprint in different platforms one by one.

Windows

The keytool usually comes with the JDK package. We use the keytool to generate the SHA1 fingerprint. Navigate to the bin directory in your default JDK installation location, which is what you configured in the JAVA_HOME variable, for example, C:Program FilesJavajdk 1.7.0_71. Then, navigate to File | Open command prompt. Now, the command prompt window will open. Enter the following command, and then hit the Enter key:

keytool -list -v -keystore "%USERPROFILE%.androiddebug.keystore" - alias androiddebugkey -storepass android -keypass android

You will see output similar to what is shown here:

Valid from: Sun Nov 02 16:49:26 IST 2014 until: Tue Oct 25 16:49:26 
IST 2044
Certificate fingerprints:
    MD5:  55:66:D0:61:60:4D:66:B3:69:39:23:DB:84:15:AE:17
    SHA1: C9:44:2E:76:C4:C2:B7:64:79:78:46:FD:9A:83:B7:90:6D:75:94:33

In the preceding output, note down the SHA1 value that is required to register our application with the Google Developer Console:

Learning Android Google Maps

The preceding screenshot is representative of the typical output screen that is shown when the preceding command is executed.

Linux

We are going to obtain the SHA1 fingerprint from the debug.keystore file, which is present in the .android folder in your home directory. If you install Java directly from PPA, open the terminal and enter the following command:

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -
storepass android -keypass android

This will return an output similar to the one we’ve obtained in Windows. Note down the SHA1 fingerprint, which we will use later.

If you’ve installed Java manually, you’ll need to run a keytool from the keytool location. You can export the Java JDK path as follows:

export JAVA_HOME={PATH to JDK}

After exporting the path, run the keytool as follows:

$JAVA_HOME/bin/keytool -list -v -keystore ~/.android/debug.keystore -
alias androiddebugkey -storepass android -keypass android

The output of the preceding command is shown as follows:

Learning Android Google Maps

Mac OS X

Generating the SHA1 fingerprint in Mac OS X is similar to you what you performed in Linux. Open the terminal and enter the command. It will show output similar to what we obtained in Linux. Note down the SHA1 fingerprint, which we will use later:

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -
storepass android -keypass android

Registering your application to the Google Developer Console

This is one of the most important steps in our process. Our application will not function without obtaining an API key from the Google Developer Console. Follow these steps one by one to obtain the API key:

Open the Google Developer Console by visiting https://console.developers.google.com and click on the CREATE PROJECT button.

A new dialog box appears. Give your project a name and a unique project ID. Then, click on Create:

Learning Android Google Maps

As soon as your project is created, you will be redirected to the Project dashboard. On the left-hand side, under the APIs & auth section, select APIs:

Learning Android Google Maps

Then, scroll down and enable Google Maps Android API v2:

Learning Android Google Maps

Next, under the same APIs & auth section, select Credentials. Select Create new Key under the Public API access, and then select Android key in the following dialog:

Learning Android Google Maps

In the next window, enter the SHA1 fingerprint we noted in our previous section followed by a semicolon and the package name of the Android application we wish to register. For example, my SHA1 fingerprint value is C9:44:2E:76:C4:C2:B7:64:79:78:46:FD:9A:83:B7:90:6D:75:94:33, and the package name of the app I wish to create is com.raj.map; so, I need to enter the following:

C9:44:2E:76:C4:C2:B7:64:79:78:46:FD:9A:83:B7:90:6D:75:94:33;com.raj.map

You need to enter the value shown in the following screen:

Learning Android Google Maps

Finally, click on Create. Now our Android application will be registered with the Google Developer Console and it will a display a screen similar to the following one:

Learning Android Google Maps

Note down the API key from the screen, which will be similar to this:

AIzaSyAdJdnEG5vfo925VV2T9sNrPQ_rGgIGnEU

Configuring Google Play services

Google Play services includes the classes required for our map application. So, it is required to be set up properly. It differs for Eclipse with the ADT plugin and Gradle-based Android Studio. Let’s see how to configure Google Play services for both separately; It is relatively simple.

Android Studio

Configuring Google Play Services with Android Studio is very simple. You need to add a line of code to your build.gradle file, which contains the Gradle build script required to build our project. There are two build.gradle files. You must add the code to the inner app’s build.gradle file. The following screenshot shows the structure of the project:

Learning Android Google Maps

The code should be added to the second Gradle build file, which contains our app module’s configuration. Add the following code to the dependencies section in the Gradle build file:

compile 'com.google.android.gms:play-services:7.5.0

The structure should be similar to the following code:

dependencies {
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.android.support:appcompat-v7:21.0.3'
}

The 7.5.0 in the code is the version number of Google Play services. Change the version number according to your current version. The current version can be found from the values.xml file present in the res/values directory of the Google Play services library project.

The newest version of Google Play services can found at https://developers.google.com/android/guides/setup.

That’s it. Now resync your project. You can sync by navigating to Tools | Android | Sync Project with Gradle Files.

Now, Google Play services will be integrated with your project.

Eclipse

Let’s take a look at how to configure Google Play services in Eclipse with the ADT plugin. First, we need to import Google Play services into Workspace. Navigate to File | Import and the following window will appear:

Learning Android Google Maps

In the preceding screenshot, navigate to Android | Existing Android Code Into Workspace.

Then click on Next. In the next window, browse the sdk/extras/google/google_play_services/libproject/google-play-services_lib directory root directory as shown in the following screenshot:

Learning Android Google Maps

Finally, click on Finish. Now, google-play-services_lib will be added to your Workspace. Next, let’s take a look at how to configure Google Play services with our application project. Select your project, right-click on it, and select Properties.

In the Library section, click on Add and choose google-play-services_lib.

Then, click on OK. Now, google-play-services_lib will be added as a library to our application project as shown in the following screenshot:

Learning Android Google Maps

In the next section, we will see how to configure the API key and add permissions that will help us to deploy our application.

Adding permissions and defining the API key

The permissions and API key must be defined in the AndroidManifest.xml file, which provides essential information about applications in the operating system. The OpenGL ES version must be specified in the manifest file, which is required to render the map and also the Google Play services version.

Adding permissions

Three permissions are required for our map application to work properly.

The permissions should be added inside the <manifest> element. The four permissions are as follows:

  1. INTERNET
  2. ACCESS_NETWORK_STATE
  3. WRITE_EXTERNAL_STORAGE
  4. READ_GSERVICES

Let’s take a look at what these permissions are for.

INTERNET

This permission is required for our application to gain access to the Internet. Since Google Maps mainly works on real-time Internet access, the Internet it is essential.

ACCESS_NETWORK_STATE

This permission gives information about a network and whether we are connected to a particular network or not.

WRITE_EXTERNAL_STORAGE

This permission is required to write data to an external storage. In our application, it is required to cache map data to the external storage.

READ_GSERVICES

This permission allows you to read Google services. The permissions are added to AndroidManifest.xml as follows:

<uses-permission android_name="android.permission.INTERNET"/>
<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" 
/>

There are some more permissions that are currently not required.

Specifying the Google Play services version

The Google Play services version must be specified in the manifest file for the functioning of maps. It must be within the <application> element.

Add the following code to AndroidManifest.xml:

<meta-data android_name="com.google.android.gms.version"
           android_value="@integer/google_play_services_version" />

 

Specifying the OpenGL ES version 2

Android Google maps uses OpenGL to render a map. Google maps will not work on devices that do not support version 2 of OpenGL. Hence, it is necessary to specify the version in the manifest file.

It must be added within the <manifest> element, similar to permissions. Add the following code to AndroidManifest.xml:

<uses-feature
        android_glEsVersion="0x00020000"
        android_required="true"/>

The preceding code specifies that version 2 of OpenGL is required for the functioning of our application.

Defining the API key

The Google maps API key is required to provide authorization to the Google maps service. It must be specified within the <application> element.

Add the following code to AndroidManifest.xml:

<meta-data
    android_name="com.google.android.maps.v2.API_KEY"
    android_value="API_KEY"/>

The API_KEY value must be replaced with the API key we noted earlier from the Google Developer Console.

The complete AndroidManifest structure after adding permissions, specifying OpenGL, the Google Play services version, and defining the API key is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest 
    package="com.raj.sampleapplication"
    android_versionCode="1"
    android_versionName="1.0" >


    <uses-feature
           android_glEsVersion="0x00020000"
           android_required="true"/>


    <uses-permission android_name="android.permission.INTERNET"/>
    <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" 
/>



   <application>



   <meta-data android_name="com.google.android.gms.version"
                android_value="@integer/google_play_services_version" />
   <meta-data
                android_name="com.google.android.maps.v2.API_KEY"
                android_value="AIzaSyBVMWTLk4uKcXSHBJTzrxsrPNSjfL18lk0"/>


   </application>
</manifest>

 

Summary

In this article, we learned how to generate the SHA1 fingerprint in different platforms, registering our application in the Google Developer Console, and generating an API key. We also configured Google Play services in Android Studio and Eclipse and added permissions and other data in a manifest file that are essential to create a map application.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here