How to create Google maps API key for android app
Tags: android, security
Published: 2020-08-04 13:58:58

Working with Google API keys is confusing. Securing them is even more confusing. Here's what I learned about creating API keys for android apps.

# Create a new project

Before you can create an API key, you need to create a "project". Each project can have multiple API keys. You can skip this step if you already have a project for this app.

Go to https://console.developers.google.com/project and click on "Create project":

![Create project screen](https://dev-to-uploads.s3.amazonaws.com/i/rj3ywki023d891l8pl52.png)


You'll be taken to the next screen:
![Project details screen](https://dev-to-uploads.s3.amazonaws.com/i/cjy8eayk1c8yylepgsis.png)


Enter the details and click "Create". Next you'll see the new project in your projects list with a notification
![Projects list screen](https://dev-to-uploads.s3.amazonaws.com/i/3eimvhzm0w7syw5bue7v.png)


# Open your project
Next you'll have to enable the maps API in the project

From the project list Click on the navbar button > "APIs & Services" > "Dashboard"
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/45pv7om8vf5v2njow4hr.png)


Make sure that the project name shown on top left is correct
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a6whqwo1qbiy766hyfjj.png)

If not then click on the project name to open the project selection screen:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8dlv2y4d2sj8v4b5lezf.png)

Click on the project name to select that project


# Enable Android Maps API

From the dashboard click on "Enable APIs and Services" link
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vch6tdyr6y423cw3heq6.png)


Search for "Android maps" and select the "Maps SDK for Android"
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u0we563gchn0g57dodm3.png)


Click on the "Enable" button to enable it:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iohjj13ll3nayalyrbsl.png)


# Create the API key

Go to "APIs & Services" > "Credentials"
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kbtwqnb5a9qagdvjpu07.png)


Click on "Create credentials" and select "API key"
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/15a9zv8wcx3hafo4wz7u.png)


After some loading, your key is ready
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zsudeuo481md0od7bavm.png)

Congratulations! You're done...almost


# Restrict the key
Click on the "Restrict" button and you'll be taken to the "Restrict and rename API key" page

## Restrict key to android apps

*Note*: This will restrict your app to a particular keystore. You might need a separate key for each app variant (debug, release, etc).

Under the "Application restrictions" sections select "Android apps" and click on "Add an item" button
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qt5g1mj9e4go0rzzjh5i.png)

You'll be prompted to enter information about your android app
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xxd7qmmxahx7c267gcnt.png)

For package name, enter your android app's package name. For example: com.myapp.thingy

Getting the SHA-1 fingerprint is somewhat tricky. Google will give you the platform specific instructions on the same page. You can also check [this answer](https://stackoverflow.com/a/15727931)

Click "Done" and we're done with restricting your key to your own app

## Set API restrictions
This makes sure that your key is used for the intended purposes only.

Under the "API restrictions" section click on "Restrict key" > "Select APIs" and check the "Maps SDK for Android" option.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h7of1cx4hzz24pauufu4.png)

Click on "Save" and the API key is ready to be used with your android app!