Android - Changing Gradle.properties according to flavor - android

I'm building an Android app with React Native and I have troubles configuring some of my API key.
I have one of my API key in my Gradle.properties
RNGP_ANDROID_API_KEY=mysupersecretkey
And I have two different flavor: beta for testing and prod for production.
I would like to override the RNGP_ANDROID_API_KEY in my two flavors
Like this:
flavorDimensions 'env'
productFlavors {
beta {
dimension 'env'
applicationId "com.fake.package"
signingConfig signingConfigs.release
manifestPlaceholders=[google_api:"beta_key"]
}
prod {
dimension 'env'
applicationId "com.fake.package"
signingConfig signingConfigs.release
manifestPlaceholders=[google_api:"prod_key"]
}
}
It works well to override value in the manifest but I don't know how to change the Gradle.properties value.
Please let me know what I can do!

Instead of writing in Gradle.properties
Try to use it as below,
flavorDimensions 'env'
productFlavors {
beta {
dimension 'env'
applicationId "com.fake.package"
signingConfig signingConfigs.release
manifestPlaceholders=[google_api:"beta_key"]
buildConfigField 'String', 'RNGP_ANDROID_API_KEY', '"mysupersecretkey"'
}
prod {
dimension 'env'
applicationId "com.fake.package"
signingConfig signingConfigs.release
manifestPlaceholders=[google_api:"prod_key"]
buildConfigField 'String', 'RNGP_ANDROID_API_KEY', '"mysupersecretkey"'
}
}
You can access this variable by BuildConfig.RNGP_ANDROID_API_KEY

Related

Android configuration specific to each combination of 2 flavor dimensions

I am trying to add a second flavor dimension to an existing Android project. I already had a dimension for different environments (DEV, BETA, PROD), each with their own backend API and their own application id (to be able to install apps connected to several environments on the same device). And now I want to add another dimension for 2 variants of my app, a general one with all the features, and a specific one with a subset of features. And in addition to that, I still have the default debug and release build types I want to keep.
So here is what my configuration in build.gradle looks like so far:
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.example"
minSdkVersion 21
targetSdkVersion 30
versionCode 56
versionName "1.1.6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
manifestPlaceholders = [
appAuthRedirectScheme: 'com.example'
]
}
signingConfigs {...}
buildTypes {
debug {...}
release {...}
}
flavorDimensions "env", "variant"
productFlavors {
dev {
dimension "env"
applicationIdSuffix ".dev"
resValue 'string', 'backend_url', 'https://dev.example.com/api/v1/'
manifestPlaceholders = [
appAuthRedirectScheme: 'com.example.dev'
]
}
beta {
dimension "env"
applicationIdSuffix ".beta"
resValue 'string', 'backend_url', 'https://beta.example.com/api/v1/'
manifestPlaceholders = [
appAuthRedirectScheme: 'com.example.beta'
]
}
prod {
dimension "env"
resValue 'string', 'backend_url', 'https://example.com/api/v1/'
manifestPlaceholders = [
appAuthRedirectScheme: 'com.example'
]
}
general {
dimension "variant"
applicationId "com.example"
}
specific {
dimension "variant"
applicationId "com.example.specific"
}
}
...
}
As you can see, I will be able to have an application variant specific to each environment/variant flavor by combining applicationId and applicationIdSuffix.
But I also need to have the corresponding mapping in manifestPlaceholders, knowing that the appAuthRedirectScheme placeholder is not integrated inside of my project's manifest, but in the openId Appauth dependency, so I can't just have several manifests in the various flavor directories like I have read elsewhere.
Is there a way to define a build setting in build.gradle that is specific to each flavor dimension combination? In other words, I would like to have different values of manifestPlaceholders for devGeneral, devSpecific, betaGeneral, betaSpecific, prodGeneral and prodSpecific.
I don't specially advice this solution, but you could try the following approach:
applicationVariants.all { variant ->
if(variant.productFlavors.get(0).name == "dev") {
if (variant.buildType.name == "debug" ) {
variant.getMergedFlavor().manifestPlaceholders = [
appAuthRedirectScheme: 'com.example.dev'
]
}
if (variant.buildType.name == "release" ) {
variant.getMergedFlavor().manifestPlaceholders = [
appAuthRedirectScheme: 'com.example.dev.foo'
]
}
}
if(variant.productFlavors.get(0).name == "beta") { ... }
if(variant.productFlavors.get(0).name == "prod") { ... }
}

Why doesn't Firebase display events issued by my app?

I built an app and registered it with Firebase as part of a project that includes three other apps. Here's what my buildTypes in the app gradle build file looks like:
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "int", "RULE_DEBUG_LEVEL", "0"
}
releaseDebuggable {
debuggable true
buildConfigField "int", "RULE_DEBUG_LEVEL", "1"
}
debug {
//applicationIdSuffix '.debug'
versionNameSuffix " Debug"
buildConfigField "int", "RULE_DEBUG_LEVEL", "1"
}
and the productFlavors are as follows:
flavorDimensions "fmplus"
productFlavors {
String name = "fmplus"
fmplus {
name = "fmplus"
applicationId getPackageName(name)
resValue 'string', 'app_name', getAppName(name)
resValue 'color', 'accent', getThemeColor(name)
resValue 'color', 'accentDark', getThemeColorDark(name)
}
fmplus_nab_htc_one_m8 {
name = "fmplus"
applicationId getPackageName(name)
applicationIdSuffix ".nab_htc_one_m8"
versionNameSuffix "-nab_htc_one_m8"
resValue 'string', 'app_name', getAppName(name)
resValue 'color', 'accent', getThemeColor(name)
resValue 'color', 'accentDark', getThemeColorDark(name)
}
fmplus_nyx_supraim_sn10 {
name = "fmplus"
applicationId getPackageName(name)
applicationIdSuffix ".nyx_supraim_sn10"
versionNameSuffix "-nyx-supraim-sn10"
resValue 'string', 'app_name', getAppName(name)
resValue 'color', 'accent', getThemeColor(name)
resValue 'color', 'accentDark', getThemeColorDark(name)
}
changeFileManifest(name)
}
Another programmer on our team handled the flavor labelled 'fmplus_nyx_supraim_sn10' and I'm doing 'fmplus_nab_htc_one_m8'. Both apps appear in Firebase under the same project, but Firebase reports his events but not mine. I've been struggling with this for some time now and have run out of ideas. Does anyone have an idea what I'm doing wrong. And yes, I downloaded and installed the google-services.json file and rebuilt the app before running it, just like the docs say.
The problem was in the app gradle file: Android Studio 3.0 requires a default dimensions statement, which I have, but what I also needed to do was add a dimension statement to the build flavor in question. The other developer, whose app didn't have this problem, was using Studio 2.3.3.

Product Flavors Configurations in Android

I am completely new to Product Flavors and I have gone through many links to understand it. But there are few doubts which are still not clear. I am having 3 product flavors i.e: qa, dev and prod. I have only created these three product flavors because I need to change the URLs and some API keys for different flavors which I have done by creating 3 different packages and placing the same java(having the URLs) file in the app/src directory. This is how my build.gradle. What are the mandatory things I need to add in each flavor? Something related to: proguard, signingConfigs:
android {
useLibrary 'org.apache.http.legacy'
compileSdkVersion 26
buildToolsVersion '26.0.2'
flavorDimensions "default"
defaultConfig {
applicationId "com.sagar.demo"
minSdkVersion 21
targetSdkVersion 25
multiDexEnabled true
versionCode 67
versionName "1.0.0" //Update Version build number
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
signingConfigs {
release {
storeFile file("myKeystore")
storePassword "Keystore2017"
keyAlias "SagarSuri"
keyPassword "Keystore2020"
}
}
buildTypes {
debug {
debuggable true
minifyEnabled false // shrink
useProguard false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
debuggable false
minifyEnabled true
useProguard true
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
productFlavors {
qa {
dimension "default"
}
prod {
dimension "default"
signingConfig signingConfigs.release
}
dev {
dimension "default"
}
}
}
The way product flavors work is such that you'll end up with the number flavors in each dimension multiplied by the number of buildTypes, in your case you have 3 flavors in a single dimension and 2 build types which ends up with 3x2=6 build variants which are the following:
qaDebug
qaRelease
prodDebug
prodRelease
devDebug
devRelease
Each combination is an actual combination of the related product flavor config and the related build type config combined. Meaning for variant qaDebug, it's configuration are from the defined under qa and from the defined under debug combined. Hence if debug already defines the proguardFiles there's generally no need to define it in qa, unless if qa requires extra proguard configs for it's own code then that's a different matter that needs to be decided and hence proguard should be defined specific for each flavor and not in the build types.
Another aspect I'd like to point out is signingConfig which here is defined the same value for prod and release. This means that any combination that starts with prod or ends with Release will have the signingConfig set, which in this case are: qaRelease, prodDebug, prodRelease and devRelease.
Hence the configuration is really up to your choice and design, and not all projects will have the same config.

Changing only Application ID, not package name

I only want to change application id in gradle file, but dont want to change package name.
Is it possible?
It is not recommended to change your application id. What you can do is change your application suffix.
As an example, if your application id is com.example.my_app then add different suffixes for different build types, such as com.example.myapp.dev for debug.
Go to app/build.gradle file and on android block add the suffix you want:
buildTypes {
release {
applicationIdSuffix ".production"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".dev"
versionNameSuffix '-DEBUG'
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Read more about it here
No problem. You can change your application Id as long as your app is not on store. If you change your application id, It would immediately become a different app for google play.
I have many apps with different package name and application ids.
Yes you can change androidId in defaultConfig.
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.somepkg" // <- we can change applicationId in defaultConfig
minSdkVersion 21
targetSdkVersion 26
vectorDrawables.useSupportLibrary = true
....
}
}
Also we can change applicationId in flavors use suffix or override it:
flavorDimensions "app"
productFlavors {
qa {
dimension "app"
applicationIdSuffix = ".qa" // <- add suffix (it will be )
}
production {
applicationId = "com.someotherpkg" // <- we can change applicationId in flavors
dimension "app"
}
}
However your src files will be still in the same folders.

different applicationIds not resulting in side-by-side installs

I've been trying to build different product flavours to allow multiple side-by-side installs for our QA teams so I changed the applicationId to be different in each one.
buildTypes {
debug {
applicationId = "com.mypackagename.qa"
....
}
release {
applicationId = "com.mypackagename"
....
}
development {
applicationId = "com.mypackagename.development
....
}
}
However when I attempt to install them all, the release version is installed alone but both the development and the debug end up replacing each other.
Any thoughts on how to get them to install side by side?
Try to add versionNameSuffix. This is how it should look like:
buildTypes {
debug {
versionNameSuffix ".dev"
applicationIdSuffix '.dev'
}
iqa {
versionNameSuffix ".IQA"
debuggable true
signingConfig signingConfigs.debug
applicationIdSuffix '.IQA'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

Categories

Resources