1

I have some secret keys which I want to store in a File, define the path in the environment variable and then get the values inside it in build.gradle.

I have a file - somekeys and there are 2 3 key-value pair - (Key1 - value 1) (Key2 - value 2) (key3 - value 3)

I will define the path of the file in the system variable. SOME_KEY and its path.

I get the file inside the gradle using - System.getenv("SOME_KEY")

Now that I have the file How to extract the value from the keys inside the file.

And then pass it to the build(release and debug) type keys like this -

resValue "string", "fc_app_id", "getting the key from the file in environment variable".
Aman Verma
  • 3,155
  • 7
  • 30
  • 60

2 Answers2

0

This can be done by adding your key to the gradle.properties file.

This file can be found in .gradle directory in your home directory. You might have to create the file if it's not there already.

Just add the key as key-value pair in gradle.properties file as shown below.

SECRET_KEY="my-super-secret-key123"

In your module level build.gradle file add the key as a buildConfigField and if you want to use the key in xml or app code add the key as a resValue.

buildTypes { debug { buildConfigField 'String', "SecretKey", SECRET_KEY resValue 'string', "secret_key", SECRET_KEY } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField 'String', "SecretKey", SECRET_KEY resValue 'string', "secret_key", SECRET_KEY } }

This code can be accessed as shown below:

String apiKey = BuildConfig.ApiKey;

Alok
  • 36
  • 3
0

I recommend the documentation around properties and this other question if you really want to load your own file.

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43