Skip to content

Instantly share code, notes, and snippets.

@thedroiddiv
Created January 12, 2024 15:27
Show Gist options
  • Save thedroiddiv/044cdee5bc24e56133a500c5beca4f41 to your computer and use it in GitHub Desktop.
Save thedroiddiv/044cdee5bc24e56133a500c5beca4f41 to your computer and use it in GitHub Desktop.
sectets-on-android-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 build.gradle.kts file (module-level):
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
  1. Add to the android { defaultConfig { } } block of your module-level build.gradle.kts file:
android {
    // ...
    
    defaultConfig {
        // ...
        buildConfigField("String", "API_KEY", gradleLocalProperties(rootDir).getProperty("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