I've ran into some issues with reading variables to and from the build.gradle(:app):.
I asked a question how to fix so that gradle recognizes the apikey.properties file I have added to root, and gradle does not complain any more.
Here's my old question: Gradle build won't find the apikey.properties file in root folder
But I cannot read the values in my Java app code, the BuildConfig.SCOPE and what not is coming back "null".
My build.gradle(:app):
Properties properties = new Properties()
if (rootProject.file("apikey.properties").exists()) {
properties.load(rootProject.file("apikey.properties").newDataInputStream())
}
def scope = properties.getProperty("SCOPE")
def client_Id = properties.getProperty("CLIENT_ID")
def client_Secret = properties.getProperty("CLIENT_SECRET")
def grant_Type = properties.getProperty("GRANT_TYPE")
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
lintOptions {
abortOnError false
}
defaultConfig {
applicationId "com.my_fantastic_app"
minSdkVersion 21
targetSdkVersion 30
versionCode 2
versionName "1.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// should correspond to key/value pairs inside the file
buildConfigField("String", "SCOPE", "\"$scope\"")
buildConfigField("String", "CLIENT_ID", "\"$client_Id\"")
buildConfigField("String", "CLIENT_SECRET", "\"$client_Secret\"")
buildConfigField("String", "GRANT_TYPE", "\"$grant_Type\"")
}
...
println("scope: " + scope)
println("client_Id: " + client_Id)
println("client_Secret: " + client_Secret)
println("grant_Type: " + grant_Type)
...
apikey.properties file:
SCOPE=xxx
CLIENT_yyy
CLIENT_zzz
GRANT_TYPE=client_credentiaals
When I try to println from the build.gradle, I get result: scope: null client_Id: null client_Secret: null grant_Type: null
When I debug a random java.class:
String scope = BuildConfig.SCOPE;
String clientId = BuildConfig.CLIENT_ID;
String clientSecret = BuildConfig.CLIENT_SECRET;
String grantType = BuildConfig.GRANT_TYPE;
System.out.println("Scope: " + scope);
System.out.println("clientId: " + clientId);
System.out.println("clientSecret: " + clientSecret);
System.out.println("grantType: " + grantType);
Any suggestions? Should I redesign the whole handling of variables in gradle? Any suggestion how to do this so I can handle both local and DevOps pipeline/variables? Big thanks!