Skip to content

Instantly share code, notes, and snippets.

@ramonaharrison
Last active January 12, 2024 15:23
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ramonaharrison/7a2061bf5ee920dfff53b37ed83520ec to your computer and use it in GitHub Desktop.
Save ramonaharrison/7a2061bf5ee920dfff53b37ed83520ec 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