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.
- Get information about your app’s certificate.
- 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.
- 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
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.
TODO: Before you run your application, you need a Google Maps API key.
To get one, follow this link, follow the directions and press "Create" at the end:
You can also add your credentials to an existing key, using this line:
Alternatively, follow the directions here:
Once you have your key (it starts with "AIza"), replace the "google_maps_key"
string in this file.
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string>
Follow the steps below to display a certificate’s SHA-1 fingerprint using the
keytool program with the
Displaying the debug certificate fingerprint
- 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:
- 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:
- Go to the Google API Console.
- Create or select a project.
- Click Continue to enable the API.
- 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.
- In the resulting dialog, enter your app’s SHA-1 fingerprint and package name. For example:
- 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:
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, add the following element as a child of the
<application>element, by inserting it just before the closing
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.
AndroidManifest.xmland re-build your application.