APPENDIX C: How to Download and Install Google Play Services, Use Maps

Chapter opener image: © Fon_nongkran/Shutterstock

The Android Standard Development Kit (Android SDK) does not include all the packages and classes we need for development. For example, maps and advertising-related classes may not be included. However, we can download and install them with the Android SDK Manager. TABLE C.1 shows the possible extra steps needed.

Steps 1 and 2 relate to the development environment, and we only need to perform them once. Steps 3, 4, and 5 are project related, and we need to perform them for each project.

TABLE C.1 Steps to use Google Play services

Step Description
1 Download and install Google Play Services (if not already done).
2 Download and install Google APIs (if not already done).
3 Update the build.gradle (Module: app) file (if not already done—that depends on the Android template we use).
4 For an app using Google Maps, obtain a key from Google (if not already done).
5 Add the appropriate elements to the AndroidManifest.xml file (if not already done—that depends on the Android template we use).

Step 1 (to do once, and periodically if needed): Install Google Play services

Select Tools, select Android, select SDK Manager (or click on the SDK Manager icon), select the SDK Tools tab. Look for Google Play services. If it indicates Not installed, check its checkbox and click on Install x Packages (x is likely to be more than 1 and can vary depending on our current version of the development environment). Accept the license terms and click on Install. Depending on how many files are downloaded and installed, this may take between a few seconds or several minutes. In FIGURE C.1, we can see that Google Play services are installed.

Step 2 (to do once, and periodically if needed): Install the Google APIs if necessary

The library we need may or may not have been installed as part of Step 1. For example, the Google Maps Android API Version 2 is part of the Google Play Services SDK. It may have been installed as part of Step 1 (it may have been checked by default and was installed along with Google Play services). If not, we must install it in order for us to use maps. Reopen the SDK Manager (Select Tools, select Android, select SDK Manager). Select Android SDK, then select the SDK Platforms tab. Select the most recent Android directory (for the author, at the time of this writing, it is Android 6.0 API 23), and click on the Show Package Details button at the bottom right of the panel. In FIGURE C.2, we can see that the Google APIs are already installed but there is an update available. If Google APIs indicates Not installed, we need to select it and then click on Install packages. If there is an update available, we should update. We can also update by selecting Check for Update. . . from the Help menu and following the directions. This may take several minutes.

FIGURE C.1 The Android SDK manager before Google Play services are installed

FIGURE C.2 The Android SDK manager: Google APIs are installed and there is an update available

Step 3 (to do for each project): Make Google Play services libraries available to an app

In order to do this, we need to modify the build.gradle file (there are two such files. We need to edit the one for the module). EXAMPLE C.1 shows the edited file. The only addition is at line 27: we need to include the appropriate version of the Google Play services libraries. This example shows edition 9.4.0. Any time we modify a gradle file, we should sync the project. We can do that by clicking on the sync icon. FIGURE C.3 shows the sync icon. After this step, the project should compile properly.

Note that if we use an existing Android template such as the Google Maps Activity template when creating a new project, the build.gradle file already contains line 27.

Step 4 (to do only once): In order to use Google maps, obtain a key from Google

To obtain a key from Google that we can use for development, we need to do the following:

  1. Obtain a debug certificate fingerprint.

  2. Register a project in the Google APIs Console and add the Maps API as a service for the project.

  3. Request and obtain a key.

EXAMPLE C.1 The edited build.gradle (Module: app) file

FIGURE C.3 The sync icon

When it is time to release our app to Google Play, we need to obtain a release certificate fingerprint instead of a debug certificate fingerprint, and the process is similar. However, in order to do that, we need to be a registered developer with Google Play.

In order to obtain a debug certificate fingerprint, do the following at the command line:

For Linux or Unix systems:

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

For Windows systems:

keytool -list -v -keystore ”C:Usersyour_user_name.androiddebug.
keystore” -alias androiddebugkey -storepass android -keypass android

For the author’s computer, it is:

keytool -list -v -keystore ”C:UsersHerve.androiddebug.keystore”
-alias androiddebugkey -storepass android -keypass android

If the keytool command is not recognized by the system, we can either add the directory where it is located to the path, or run the keytool command from inside its directory. The executable file for keytool, keytool.exe, should be located inside the bin directory of the Java jdk directory. On the author’s computer, keytool.exe is located in the C:Program FilesJavajdk1.8.0_60in directory.

In Windows, we should see an output similar to the one shown in FIGURE C.4 (note that the author has replaced the certificate fingerprints and key identifier with random values). The fingerprint is the sequence of 20 two-digit hexadecimal numbers separated by colons that appear after SHA1.

FIGURE C.4 Possible output when running keytool

Fingerprint certificates are unique and provide a way to identify an app. That enables Google to track the app in Google Play as well as track the use of map resources by our app.

Now, we need to go the Google APIs Console to register a project. At the time of this writing, the URL is:

https://console.developers.google.com/

If we have already registered a project, the project shows on the web page. Otherwise, click on Create project. Enter a name for our project and click on Create. Then, select Use Google APIs.

Among the list of available services, locate Google Maps Android API, as shown in FIGURE C.5, and click on it. Next, click on Enable API, as shown in FIGURE C.6. At that time, unless we have done it before, we will be asked to enter our credentials.

FIGURE C.5 Finding Google Maps Android API in Google Play services

FIGURE C.6 Enabling the Google Maps Android API

FIGURE C.7 Selecting the API Manager

Next, we need to obtain a key. On the top left, click on the three small horizontal lines next to Google Developers Console. From the menu, choose API Manager (see FIGURE C.7). Then select Credentials. From the New Credentials menu, select API key (see FIGURE C.8), then choose Android key. At that point, we should enter the package name of our app and the SHA-1 fingerprint from Figure C.4, as shown in FIGURE C.9. Note that we have hidden the author’s fingerprint in the figure. We then click on Create and the key shows.

A key depends on the fingerprint certificate and the application package, so we recommend that you get a different key for each app, but it is not required.

Step 5 (to do for each project): Add the appropriate permissions to the AndroidManifest.xml file

Depending on the app, we need to edit the AndroidManifest.xml file as follows:

  • ▸ List permissions using the permission element.

  • ▸ List use permissions using the uses-permission element.

    FIGURE C.8 Selecting API key from the New credentials menu

    FIGURE C.9 Entering package name and SHA-1 fingerprint

  • ▸ List use features using a uses-feature element.

  • ▸ If the app is using Google Maps, specify the name and value of the key to access map data using a meta-data element.

  • ▸ If the app is using Google Maps, list the Google Maps library using a uses-library element.

If we use the Google Maps Activity template, some of these may have been included in the AndroidManifest.xml automatically.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.236.17