How to mock Gradle's buildConfigField for Travis CI build? - android

I try to integrate my android app with a Travis CI. My app need an api key, that shouldn't be posted in repo. So, i put my api key in global gradle properties file ~/.gradle/gradle.properties:
MY_SECRET_API_KEY="aaaabbbcccdddeeefff"
Then I read this value in app/build.gradle file (which is in public repository) and set it as buildconfig field:
apply plugin: 'com.android.application'
android {
// ...
buildTypes.each {
it.buildConfigField "String", "API_KEY", MY_SECRET_API_KEY
}
// ...
}
and use this api key in app code by accessing to BuildConfig.API_KEY.
I get following error message from Travic CI:
Could not find property 'MY_SECRET_API_KEY' on com.android.build.gradle.AppExtension_Decorated.

Use Travis' environment variables; more specifically use encrypted variables so that the values of secure variables are always masked in the build output. You read Tavis env variables in the gradle script as System.getenv('key') though. It is cleaner to use environment variables on the local end as well. If you want to still use gradle.properties, you could do something like this:
hasProperty('secret_api_key') ? secret_api_key: System.getenv('secret_api_key')
To set Travis env variables, see here:

Related

How to configure Azure AD B2C for different environments in 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?

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

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}"

Add build machine signature to gradle generated variable

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";

Passing -P parameters to gradle from android studio

Is there a functionality inside android studio to do things like this.
Basically my gradle script reads parameter named version based on which it sets dependency version of a certain library.
So when I do gradlew -Pversion=‘1.2.3' I get this string inside gradle. But this only works if I invoke gradle from console.
Is there a way to pass parameters to gradle when started using configuration for project inside Android Studio (pressing little play triangle)?
NOTE:I did find Gradle VM options and Script prameters under default configs but adding -Pversion=‘1.2.3' there doesn't seem to have an effect.
In order to provide parameter to run option of Android Studio you should setup them under AndroidStudios Compile Preferences
AndroidStudio > Preferences...
Under Compiler: ComandLineOptions
Run > Edit Configurations..
Replace "abcdef" in Script parameters by your params.
For example let's try to pass server base endpoint while building project. First we need to define a method which gets url and sets it
def serverUrl = "https://mydefaulturl.com"
task(runProgram){
if(project.hasProperty("url")){
serverUrl = url;
}
}
In your buildTypes create your base url with this value:
debug{
buildConfigField("java.lang.String","BASE_URL","$serverUrl")
}
Now we can pass debug build an url and use it.
You can build your project from terminal with the command below:
./gradlew installDebug task runProgram '-Purl="https://yournewurl.com"'
Put version="1.2.3" in gradle.properties or under
buildscript {
ext{
version="1.2.3"
}
...
}

Categories

Resources