- Make sure your
build
directory is gitignored. It should be, by default, in a new Android Studio project -- you can double check by making sure that your.gitignore
file contains the line:
/build
- In your project root directory, add the API key to
local.properties
file like this:
apiKey="hgjfkhg5764317698315768549027nfdsngadf"
Where hgjfkhg5764317698315768549027nfdsngadf
would be your API key.
- Add the following lines at the root level of your app-level
build.gradle
file:
def localPropertiesFile = rootProject.file("local.properties")
def localProperties = new Properties()
localProperties.load(new FileInputStream(localPropertiesFile))
- Pull the API into your project as a build config value by adding the following line to your app level
build.gradle
file in theandroid { defaultConfig { } }
block:
def localPropertiesFile = rootProject.file("local.properties")
def localProperties = new Properties()
localProperties.load(new FileInputStream(localPropertiesFile))
android {
// ...
defaultConfig {
// ...
buildConfigField "String", "API_KEY", localProperties['apiKey']
}
// ...
}
- Sync Gradle and build the project. You can now access the key in your Java code with
BuildConfig.API_KEY
, for example:
String myApiKey = BuildConfig.API_KEY;
- You can add as many secret values as you'd like to your
local.properties
by following these steps. Give each one a unique reference, i.e.myApiKey2
,BuildConfig.API_KEY_2
Sure! I'm glad it helped :)