How to configure Azure AD B2C for different environments in Android - android

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?

Related

Is it possible to pass Build Config parameters via ./gradlew command?

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

Add environment variable to product flavor

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.

access environment variable in React-Native AndroidManifest.xml

I'm new to React Native. The task at hand is to set my Google API key in AndroidManifest.xml without exposing it, when pushed to GitHub.
I set up an environment variable called API_KEY, but however I want to access it in the .xml, I get an error when trying to spin up the dev server.
So far I tried:
android:value=${env.API_KEY}
android:value="${env.API_KEY}"
android:value="${API_KEY}"
Thank you!
Based on the second comment (from kenmistry), the solution that worked for me was indeed to create a placeholder in build.gradle, but since, for whatever reason, configuring and referring a .env file did't work, I invoked my environment variables like so in build.gradle:
manifestPlaceholders = [API_KEY: "$System.env.API_KEY"]
and accessed it in the .xml as suggested by kenmistry:
android:value="${API_KEY}"
assuming that you have defined the key in your .env file, you can set that up on build.gradle as manifestPlaceholders.
android {
defaultConfig {
manifestPlaceholders = [API_KEY: "$process.env.your_key"]
}
...
}
on your AndroidManifest.xml,
android:value="${API_KEY}"

Building two different android apps on the same codebase that only differ by const

I am building two android applications, which relay on the same codebase but differ by the server address which they grab their files from.
Till now i've created two line of my server's address constant, and builded the application once with the first const, and second with the second const.
Is there any way to make my app compile twice, once with the first constant, and second with the second one?
I am using Android studio with Gradle build.
Thank you!
You can use product flavors to teach Gradle to build two separate copies of your app, where your server address is defined in BuildConfig:
android {
// other stuff here
productFlavors {
flavorOne {
buildConfigField "String", "URL", '"https://..."'
}
flavorTwo {
buildConfigField "String", "URL", '"https://..."'
}
}
}
In your Java code, refer to BuildConfig.URL to get the URL to use.
Yeah, you can use Build Variants. You can move those strings into resources under the variants directory and the build will pull in the right one.
Here's the link to the full documentation for how to set them up: https://developer.android.com/studio/build/build-variants.html

How to define static String values in Android that change for test configurations with Gradle?

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.

Categories

Resources