morristech
8/3/2019 - 1:51 PM

api-keys-local-props.md

Hiding API keys in local.properties

  1. Add the API key to your local.properties file:
apiKey=<value>
  1. Add to the root level of your app-level build.gradle file:
def localProperties = new Properties()
localProperties.load(new FileInputStream(rootProject.file("local.properties")))
  1. Add to the android { defaultConfig { } } block of your app-level build.gradle file:
android {
    // ...
    
    defaultConfig {
        // ...
        buildConfigField "String", "API_KEY", localProperties['apiKey']

    }
    
    // ...
    
}
  1. Sync Gradle and build the project. You can now reference the key:
val apiKey = BuildConfig.API_KEY