Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rubenpla-develop/47fbfc40b7c2628a1ca9df2062a898b4 to your computer and use it in GitHub Desktop.
Save rubenpla-develop/47fbfc40b7c2628a1ca9df2062a898b4 to your computer and use it in GitHub Desktop.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment