Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gradle to replace Google Maps API key?

I use one among many options to store my API keys in the computer/keychain rather than directly in the cloud-backed-up project code (namely gradle-credentials-plugin ). What I'd like is to be able to manage Google Maps keys in a similar way, but they're used in a different file (Manage Google Maps API Key with Gradle in Android Studio).

If anyone has a simple (or intricate, but reliable) option for me to dig into, I'd be grateful!

like image 581
Kheldar Avatar asked Mar 17 '18 17:03

Kheldar


People also ask

How do I change my Google Maps API key?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key. Click Close.

Can I use Google Maps API without API key?

In order to embed a Google Map iframe without api key add a Google Map widget from the left side panel. Then open it and add the address. This way you can embed Google Map without api key via Elementor.

How do I remove Google Maps API key?

To delete an API key: Go to the Credentials page. Select the API key you want to delete. Select the Delete button near the top of the page.

Can I get Google Maps API key without billing?

Yes you need to setup a billing account, there is no way around it these days.


1 Answers

You can use manifestPlaceholders to inject the string from your build.gradle.

  1. Inset your API key into a file like local.properties
google.maps_api_key=SECRET_KEY
  1. Make sure this file is ignored using .gitignore:
local.properties
  1. Make the API key available to your AndroidManifest.xml. Using withInputStream() ensures the InputStream is closed automatically.
android{
  defaultConfig {

    // Load credentials.
    def properties = new Properties()
    file("../local.properties").withInputStream { properties.load(it) }

    // Share the key with your `AndroidManifest.xml`
    manifestPlaceholders = [ googleMapsApiKey:"${properties.getProperty('google.maps_api_key')}"]
  1. Load the API key in your AndroidManifest.xml:
<application>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="${googleMapsApiKey}" />
like image 144
hb0 Avatar answered Nov 15 '22 07:11

hb0