Enter your keyword

Creating Android Google Maps API Key

Creating Android Google Maps API Key

Creating Android Google Maps API Key

To use the Google Maps Android API, you must register your app project on the Google API Console and get a Google API key which you can add to your app. There are various types of API keys, but in this tutorial were concerned with getting an Android key not a browser key.

Your API key is based on a short form of your app’s digital certificate. All Android apps are signed with a digital certificate for which you hold the private key. API keys for Android are linked to specific certificate/package pairs. You only need one key for each certificate, no matter how many users you have for the app.

Getting an API key for your app requires several steps. These steps are outlined below, and described in detail on the rest of this page.

  1. Get information about your app’s certificate.
  2. Register a project in the Google API Console if you don’t already have one, add the Google Maps Android API as a service for the project, and create an API key.
  3. Add the key to your app by adding an element to your app manifest.

Display your app’s certificate information

The API key is based on a short form of your app’s digital certificate, known as its SHA-1 fingerprint. To display the SHA-1 fingerprint for your certificate, first ensure that you are using the right certificate. You may have two certificates:

  • A debug certificate: The Android SDK tools generate this certificate automatically when you do a debug build. Only use this certificate with apps that you’re testing.
  • A release certificate: The Android SDK tools generate this certificate when you do a release build.You can also generate this certificate using the keytool program.

If you added a Google Maps Activity to your project during its creation in Android Studio, you can access the SHA-1 fingerprint in an auto generated file google_maps_api.xml. The line that begins with SHA1 contains the certificate’s SHA-1 fingerprint. The fingerprint is the sequence of 20 two-digit hexadecimal numbers separated by colons.

Follow the steps below to display a certificate’s SHA-1 fingerprint using the keytool program with the -v parameter.

Displaying the debug certificate fingerprint
  1. Locate your debug keystore file. The file name is debug.keystore, and is created the first time you build your project. By default, it is stored in the same directory as your Android Virtual Device (AVD) files:
  • MacOS and Linux:

~/.android/

  • Windows:

C:\Users\your_user_name\.android\

  1. List the SHA-1 fingerprint:
  • For Linux or macOS, open a terminal window and enter the following:

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

  • For Windows Vista and Windows 7, run:

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
You should see output similar to this:

keytool

  1. Go to the Google API Console.Create Project
  2. Create or select a project.Select project
  3. Click Continue to enable the API.Enable API
  4. Go to Credentials to get an Android key and set the API credentials.
    Note: If you have an existing Android key, you may use that key.Credentials
  5. In the resulting dialog, enter your app’s SHA-1 fingerprint and package name. For example:Add credentials to project
  6. Your new Android key appears in the list of API keys for your project. An API key is a string of characters, something like this:Generate API Key
Add the API key to your application

Follow the steps below to include the API key in your application’s manifest, contained in the file AndroidManifest.xml.

  1. In AndroidManifest.xml, add the following element as a child of the <application> element, by inserting it just before the closing </application> tag:

Substitute your API key for YOUR_API_KEY in the value attribute. This element sets the key com.google.android.geo.API_KEY to the value of your API key.

  1. Save AndroidManifest.xml and re-build your application.