1

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.

Mike
  • 697
  • 1
  • 9
  • 21
  • Take a look at this https://stackoverflow.com/questions/21999829/how-do-i-read-properties-defined-in-local-properties-in-build-gradle – HashT Feb 03 '22 at 11:48
  • @HashT Found the way to get properties value from your link. Thanks alot. – Mike Feb 03 '22 at 12:39

2 Answers2

1
  1. try and add this line in app/build.gradle within the defaultConfig

    buildConfigField("String", "apiKey", API_KEY)

  2. then try to fetch it

    String apiKey = BuildConfig.apiKey;

mordor619
  • 154
  • 11
  • I'm able to get the value by using this code ```buildConfigField "String", "API_KEY", "\" API_KEY\""``` But using this means I need to put the key in app ```build.gradle``` which will be push into git. Which the main reason I'm using this secrets gradle plugin. Am I able using this way but getting the data from ```local.properties```? – Mike Feb 03 '22 at 12:25
  • Okay, found it. I use this to load the data from ```local.properties```. ```Properties properties = new Properties()``` ```properties.load(project.rootProject.file('local.properties').newDataInputStream())``` ```buildConfigField "String", "API_KEY", ${properties.getProperty('API_KEY')}``` – Mike Feb 03 '22 at 12:37
  • yes it is generally not a good practice to put apikeys in properties file. But if its just a test project then i guess its fine. – mordor619 Feb 03 '22 at 14:15
  • I see here, everyone recommends to not put keys in local.properties However, please be considerate enough to let people know where to put it, rather than discouraging a potential workaround. Share the best practice. – Rahul Bali Mar 19 '23 at 06:03
1

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')}\""
Mike
  • 697
  • 1
  • 9
  • 21