I want to define an environment variable that is specific to a particular product flavor but I can't seem to get it to work.
I have tried adding the following
buildConfigField "string", "APP_TYPE", "\"demo\""
But I receive the error
BuildConfig.java:14: error: cannot find symbol
public static final string APP_TYPE = "demo";
My project is a React Native project and I am looking to set an environment variable which I can use in the Metro Bundler.
In Java, String has a capital S. Yours does not, and so the generated code will not compile.
So, switch to:
buildConfigField "String", "APP_TYPE", "\"demo\""
Note that this is not really an environment variable. If you mean that you will be replacing "\"demo\"" with the contents of an environment variable, just bear in mind that Android Studio does not expose environment variables to its builds, while command-line builds (e.g., Gradle) will.
Related
I am using Azure AD B2C and need a different configuration file for each environment. I expect to have multiple B2C Tenants for my app. One for each environment. That means I need to alter my calls to reference different files. Currently I am calling:
PublicClientApplication.createMultipleAccountPublicClientApplication
Where the second parameter is an:
int configFileResourceId
I have been using:
R.raw.auth_config_multiple_account
But now I need to fold in additional environments. I handle most/all of my environment changes in the build.gradle like this:
buildTypes {
release {
buildConfigField "String", "SERVER_URL", '"xxx.xxxx.com"'
}
debug {
buildConfigField "String", "SERVER_URL", '"yyyy.yyy.com"'
}
}
But how do I do this while referencing the file itself? I can use the R.raw.auth_config_multiple_account from anywhere, but cannot from the build.gradle. How are others doing this? It's also very convenient to reference it from anywhere.
• I would suggest you to reference the build.gradle file for any environment changes by calling the System.env(“variable name”) which works in gradle to get the path of any variable. Thus, this should get you the actual path of the folder which contains this file.
• So, to reference the build.gradle file in the variable, it needs to be exported to a location like as below shown as an example: -
home = System.getenv(‘HOME’) OR "${System.env.HOME}/something/plugins"
• Once it is exported and called as above, you can edit it for adding or modifying any of your environment changes in the build.gradle file.
Please find the below links for more information: -
how to use Environment Variables in Gradle build files?
I know that it's possible to supply different parameters depending on the build type via buildConfigField, eg.:
// app.gradle
buildConfigField "boolean", "ADS_ENABLED", false
buildConfigField "String", "URL", "https://host.de"
Then I would be able to access these fields in the source code like this: BuildConfig.ADS_ENABLED. But what I need, is not to hard-code these values in the gradle file, but instead to supply them at build time via the gradlew command.
Something like this (this does not work, obviously):
./gradlew assembleQa -ADS_ENABLED=true, -URL="https://anotherhost.de"
and then to be able to access these fields from the source code.
The use case for this is automation, and specifically to have different jobs on the CI pipeline that can build an apk with different combinations of parameters, without having to create a new build type for each combination.
Other suggestions are welcomed.
You can solve this by reading an environmental variable in your code and set it in the gitlab ci like this
You can find the entire gitlab config from which the example was taken over here: https://gitlab.com/viae-modules/viae-modules/-/blob/master/.gitlab-ci.yml
My project has different build types i.e. debug, beta and production and also have different product flavors i.e. QA and Integration. I have defined
a
buildConfigField 'int', 'APP_THEME', 'R.style.AKTheme'
in the productFlavors to have a separate theme for each flavor. The generated BuildConfig.java for app source set have the APP_THEME field and it is working as expected.
Recently I have started writing instrumentation tests for my app. When I try to run these tests Android studio gives me the error that can not resolve AKTheme i.e.
final int APP_THEME = R.style.AKTheme in the generated BuildConfig.java for the test source set.
It seems that R.style.AKTheme is not accessible to the generated BuildConfig.java file (test source set). I searched over internet but didn't find any help.
R.style.AKTheme is a reference, not a value, while in BuildConfig you can only use values.
There are couple of ways to achieve what you want:
Use the String name of the style in BuildConfig:
buildConfigField 'String', 'APP_THEME', '"AKTheme"'
and then in code to get the style res id:
int style = context.getResources().getIdentifier(BuildConfig.APP_THEME, "style", context.getPackageName());
Now you can use style.
You can use different source-sets.
If you are using different buildtypes, you can create a directory for that build type, and put any different resources specially for that build type in that directory. The directory should be created in the same directory as main sources directory, and named exactly the same as the buildType. Details: https://developer.android.com/studio/build/build-variants
I had quite the same issue. I had this in my build.gradle to get the right ssl certificate depending on the env. :
buildConfigField 'int', 'SSL_CERTIFICAT_RAWRES_', 'R.raw.devcert'
This was working to build and run the project, but I faced an issue when I wanted to run the task : "compileDebugAndroidTestJavaWithJavac" (used for sonarqube in my case). The compiler didn't found the resource file R when he automatically generates the buildConfig file.
The solution was to put a String to identify my certificate file "devcert" in the build.gradle instead of using directly the resInt "R.raw" :
buildConfigField 'String', 'SSL_CERTIFICAT_RAWRES_STRING', '"devcert"'
and then in my code, I get the raw file certificate like this :
final int raw = context.getResources().getIdentifier(BuildConfig.SSL_CERTIFICAT_RAWRES_STRING, "raw", context.getPackageName());
That way, the generated buildConfig found correctly the String identifier to retrieve the wanted raw file using the code above.
I found my answer here: https://developer.android.com/studio/test#create_instrumented_test_for_a_build_variant
For my case my test project and my main project is referencing a different package name, e.g. at the top of BuildConfig.java one referencing to package 'com.xxx.xxx' while one is referencing to 'com.xxx.xxx.test'
adding the line
testApplicationId = "com.xxx.xxx"
in defaultConfig in app level build.gradle file solves the issue for me
I'd like to stamp some variable generated from gradle (in my case it's User Agent used later with HTTP requests) to later be able to distinguish which developer build the app (for example if some developer made a mistake and his app is DDoSing the server).
So for now I can distinguish release from debug with:
buildTypes {
debug {
buildConfigField "String", "USER_AGENT", "\"Android-debug\""
}
release {
buildConfigField "String", "USER_AGENT", "\"Android-release\""
}
}
But for the debug I'd like to add something to know who built the app instance, it may be git login, machine name, or something else.
A gradle build file is actually Groovy code, and you're free to put whatever you want in it. You just have to make sure that the code runs before it would be used in the DSL that describes the build. So if you want to grab something from the system, just write the Groovy code to do that. Groovy is a lot like Java, and you have the full JDK to work with at runtime, so it should be easy to get started.
If you want to access things about the build machine and environment, you might have to shell out to different commands in order to gather that data. Populate some variables with that data. Then use buildConfigField as you already are to drop those values into BuildConfig.java.
Bear in mind that you might want to provide some value in both debug and release so they both generate the same BuildConfig symbols. Otherwise your app might not compile in one config or the other.
BTW. You can tell the difference between debug and release with properties that are already added to BuildConfig, so you don't need to add anything more to tell the difference. Lines like these will always appear (look in the generated BuildConfig.java to see for yourself):
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String BUILD_TYPE = "debug";
Hey I am trying to statically define String values that change according to the configuration I am running. So if I run a test configuration, it uses the test API url, but if I run a regular build, it statically sets the real API URL.
I am using two strings files right now, one in the main folder and one in the androidTest folder in Android Studio. This works well for getting different Strings per configuration, but I'de like to do it statically rather than dealing with Resource fetches.
Is this possible?
I have seen this answer for ANT, but I am not sure how to do it with Gradle.
You can generate gradle constants like this:
build.gradle
android {
buildTypes {
debug {
buildConfigField "String", "FOO", "\"foo\""
}
release {
buildConfigField "String", "FOO", "\"bar\""
}
}
}
And access them in your code through BuildConfig.FOO
Note you may need to clean and/or restart your IDE for the to come in to effect.