8

My Android application is OpenSource, and I am a little bit afraid to share the code on Github, because my API keys were also shared.

I can git ignore my strings.xml file, but I really hope there is an alternative and I will be able to do something clever, so I wont have to worry about it.

Any idea or suggestion?

Waza_Be
  • 39,407
  • 49
  • 186
  • 260

4 Answers4

4

Customize the ant build script of your project to generate a mapskey.xml (strings) from local.properties (which you add to git ignore). Those who fork just create their own local.properties with debug and release keys for Google Maps.

Sveinung Kval Bakken
  • 3,715
  • 1
  • 24
  • 30
3

Put the key in a separate file and read it from there, and only commit a placeholder file to the repository.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • 2
    A text asset would be a good place for that. You can check if an asset exists, so you don't even have to put a place holder file: if it doesn't exist, set some flag and display a warning message when they run the app for the first time. – Nikolay Elenkov Feb 21 '12 at 02:46
  • I asked this nearly 2 years ago: How can I set a Map API programmatically? http://stackoverflow.com/questions/3517833/mapactivity-set-apikey-programmatically – Waza_Be Feb 21 '12 at 08:18
1

In android studio put your API key and secrets in the local.properties file.

  1. In your top-level settings.gradle file, include the Gradle plugin portal, Google Maven repository, and Maven central repository under the pluginManagement block. The pluginManagement block must appear before any other statements in the script.

    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
        }
    }
    
  2. In Android Studio, open your project-level build.gradle file and add the following code to the dependencies element under buildscript:

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
    }
    
  3. Open your module-level build.gradle file and add the following code to the plugins element.

    plugins {
        //  ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }
    
  4. Save the file and sync your project with Gradle.

  5. Open the local.properties in your project level directory, and then add the following code. Replace YOUR_API_KEY with your API key.

    MAPS_API_KEY=YOUR_API_KEY
    
  6. Save the file.

    • Access the key within AndroidManifest.xml file: In your AndroidManifest.xml file, go to com.google.android.geo.API_KEY and update the android:value attribute with MAPS_API_KEY as follows:

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="${MAPS_API_KEY}" />
      
    • Access the key within Java/Kotlin code: Just use BuildConfig as follows:

        BuildConfig.MAPS_API_KEY
      

More can be found in docs.

Amin Soheyli
  • 605
  • 2
  • 7
  • 16
-1

If strings.xml is required by the program, then it should be included in the source, but strings specific to each user of the source should be left out. In their place, provide documentation on where the specific strings should be included.

qxn
  • 17,162
  • 3
  • 49
  • 72