Goal
Building APKs for different environments using build types
Approach
I'm using this post to build my apk without android studio. Everything works except when I use environment variables in gradle:
Before build, I set the environment variable
export MY_API_TOKEN="ABCDEFGH"
Then in my build.gradle:
buildTypes {
debug {
buildConfigField("String", "MY_API_TOKEN", System.getenv('MY_API_TOKEN'))
}
}
And when I execute : gradle assemble I get this error:
/home/apps/app/src/main/java/com/my/package/controller/api/MyAwesomeCode.java:64: error: cannot find symbol
BuildConfig.MY_API_TOKEN;
^
symbol: variable BuildConfig
location: class RestAPI
/home/apps/app/build/generated/source/buildConfig/debug/com/my/package/BuildConfig.java:14: error: cannot find symbol
public static final String MY_API_TOKEN = ABCDEFGH;
^
symbol: variable ABCDEFGH
location: class BuildConfig
I tried with several combinations, and I get the same error:
- BuildConfig not getting created correctly (Gradle Android)
- BuildConfig variable. Error: cannot find symbol
- https://medium.com/@rafamatias/gradle-android-build-variables-done-right-d0c0e296ee93
- Gradle : how to use BuildConfig in an android-library with a flag that gets set in an app
I also verified the same behavior with variables
def MY_API_TOKEN_VAR = "ABCDEFGH"
debug {
buildConfigField "String", "MY_API_TOKEN", MY_API_TOKEN_VAR
}
Error :
BuildConfig.java:14: error: cannot find symbol
public static final String MY_API_TOKEN = ABCDEFGH;
^
symbol: variable ABCDEFGH
location: class BuildConfig
Question
buildTypes works only for hardcoded values in build.gradle?