Reputation: 1575
I'm using secrets-gradle-plugin to read the API keys that I put in my local.properties.
I've added this code into the root build.gradle
buildscript {
dependencies {
classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.0"
}
}
For app build.gradle
plugins {
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}
android {
compileSdk 31
...
}
And this is my local.properties
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#
sdk.dir=/Users/xxx/Library/Android/sdk
apiKey=YOUR_API_KEY
Then when I tried to access it from any class/application BuildConfig.apiKey
is not there. Am I missing any steps here? Been trying for few hours but doesn't found any way to make this work.
Upvotes: 3
Views: 4253
Reputation: 21
Add in build.gradle.kts
buildFeatures {
buildConfig = true
}
Add in gradle.properties
android.defaults.buildfeatures.buildconfig=true
defaultConfig in build.gradle.kts
defaultConfig {
applicationId = "com.example.yourProject"
minSdk = 29
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
// Get the API key from local.properties
val localProperties = Properties().apply {
val localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localPropertiesFile.inputStream().use { load(it) }
}
}
buildConfigField("String", "apiKey", "\"${localProperties["apiKey"]}\"")
}
In local.properties, add the api key (for example)
apiKey= [YOUR API KEY]
In your code
val apiKey = BuildConfig.apiKey
try to clean project and rebuild project (Build -> Clean Project / Rebuild Project)
And the BuildConfig.java file will generated
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.yourProject";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Field from default config.
public static final String apiKey = "[YOUR API KEY]";
}
Upvotes: 2
Reputation: 135
The answer didn't exactly work on my 2023 project (Kotlin).
in local.properties
API_KEY=absdefghijklmnop
In my app level build.gradle.kts
I had to do the following
buildFeatures.buildConfig = true // to enable custom build config keys
defaultConfig {
...
...
}
Build > Clean Project
Build > rebuild Project
Finally:
val apiKey = BuildConfig.API_KEY
Text(text = "API KEY ${apiKey}")
Upvotes: 3
Reputation: 1575
I end up reading the API value in local.properties
in app's build.gradle
and assigning it to BuildConfig
. This is done under defaultConfig
tag.
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
buildConfigField "String", "API_KEY", "\"${properties.getProperty('API_KEY')}\""
Upvotes: 4
Reputation: 190
try and add this line in app/build.gradle
within the defaultConfig
buildConfigField("String", "apiKey", API_KEY)
then try to fetch it
String apiKey = BuildConfig.apiKey;
Upvotes: 1