Replace an XML file for each flavour in android - android

I am trying to integrate Braze into my application for push notification. Braze need us to create a braze.xml file inside src/main/res/values where we add the API key and other braze related stuff(here is the documentation).
Now I need to differentiate prod and qa environment meaning they will have 2 different API keys.
I was wondering how I could use a different braze.xml for different flavours.
I found this:
sourceSets {
main {
java {
srcDirs = ['src']
}
}
test {
java {
srcDirs = ['test']
}
}
}
and I was wondering how to use it to replace my braze.xml for different build variants.

You can create multiple source sets for different flavours of your project. By default there is only main/ source set created by studio which contains the common code that will be shared across different variants. For more details on how to create and maintain the source sets check official documentation.
EDIT - 1
To elaborate more you can create multiple flavor of your project by using the build.gradle (module level file) and specifying flavors like -
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
sit {
initWith debug
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Once you have created your flavors Gradle Sync your project.
Now you can create the braze.xml fike for each flavor by right click Values folder >> New >> Values Resource File. Type the name of file example braze.xml and under Source Set select name of flavor for which you want to create this file. As shown in the image name below
You can repeat this step until all the flavors are covered then whenever you switch the gradle flavor from the build variant then IDE will automatically start using the designated file for that flavor.

Related

How to set up different Constants in Kotlin for environment

We have an App in Kotlin ( Android Stdio) which has different constants by environment.
We are using Constants.kt
const val IMAGES_API = "https://localhost:3000/v1/images"
and we want to use the same variable in staging/qa/prod.
The App is building in Kotlin and we are using gradle (groovy scripts) to compiling and packing the different environment staging/qa/prod.
My first approach has been to create this properties on the gradle.properties and load the properties on the build.gradle file like this :
def loadProperties() {
def props = new Properties()
file("gradle.properties").withInputStream { props.load(it) }
def config = props
project.ext.config = config
}
And when I run gradle I can see the new properties, but I don't know how to get this value inside the App ( in the kotlin code).
My only idea is to create a task on build.gradle to copy a Constants.kt file by environment. But, I don't think, it's a good practice. I think, there must be another way to set different variables in the App.
Please, can anybody help me with this?
What you want is to configure build types in your app module's gradle file with buildConfigField in each:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildConfigField "String", "SERVER_URL", '"http://prod.this-is-so-fake.com"'
}
debug {
applicationIdSuffix ".debug"
debuggable true
buildConfigField "String", "SERVER_URL", '"http://test.this-is-so-fake.com"'
}
/**
* The `initWith` property allows you to copy configurations from other build types,
* then configure only the settings you want to change. This one copies the debug build
* type, and then changes the application ID.
*/
staging {
initWith debug
applicationIdSuffix ".debugStaging"
buildConfigField "String", "SERVER_URL", '"http://prod.this-is-so-fake.com"'
}
}
In code, you can refer to BuildConfig.SERVER_URL, and it will be populated with the string based on the build type you choose at compile time.
You can build different apk/app bundles to distribute.
Referencing this answer .
EDIT As an aside, in real life I have found this approach to be... annoying. It is easier to bundle inside the app a toggle that allows QA to switch between environments. This way you only have one bundle to deal with.

Is Proguard necessary for gradle 3.2.1?

I am doing this first time. Using Facebook SDK for android app.
I am following this tutorial.
https://developers.facebook.com/docs/sharing/android/
My app is gradle 3.2.1
Do I need to use ProGuard here?
What code should I write between the given two codes on this link :
https://developer.android.com/studio/build/shrink-code.html?fbclid=IwAR3hmG6hOtzfyiHa3Sehxa4o2j9vq9sPrk8ZVbr-WWyUDakiskFMZQgloJM
android {
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
...
}
And another code:
android {
...
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
// List additional ProGuard rules for the given build type here. By default,
// Android Studio creates and includes an empty rules file for you (located
// at the root directory of each module).
'proguard-rules.pro'
}
}
flavorDimensions "version"
productFlavors {
flavor1 {
...
}
flavor2 {
proguardFile 'flavor2-rules.pro'
}
}
}
There are some more small codes below it, which one should I add?
Please explain.
As you add ProGuard files you have to add this
useProguard true
in your gradle File(module)

How do I setup gradle for different product flavors and different icons depending on releas/debug?

I want to have different images depending on product flavor and if its debug or release, what is the proper setup of the res folder to achieve this?
In my build.gradle:
productFlavors {
flavor1 {
applicationId "com.myapp.flavor1"
}
flavor2 {
applicationId "com.myapp.flavor2"
}
}
buildTypes {
debug {
applicationIdSuffix '.debug'
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
shrinkResources true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
In my folder strucure I now have:
src/flavor1/res
/flavor2/res
/main/res
/debug/res
I've tried having in my debug folder have flavor1 and flavor2 as subfolders with resources, but that does not apply. Only having a res folder in debug results in same assets for both flavors, whereas I want them to be different. So how do I setup the project so I have different images depending on debug/release and the different product flavors?
EDIT:
updated folder structure
From android build variants docs,
Gradle automatically creates build variants based on your build types and product flavors, and names them according to product-flavor Build-Type
So in your case you have 4 variants already. And if you want different source sets for them you can have them as follows
src/flavor1Debug
src/flavor2Release and so on.. Notice the naming convention
This is actually easier than you might think.
You currently have:
src/flavor1/res
/flavor2/res
/main/res
/debug/res
Just add:
src/flavor1Debug/res
/flavor1Release/res
/flavor2Debug/res
/flavor2Release/res
Resources in there will override the ones in the less-specific folders.
The 'Build Variants' UI in Android Studio is helpful to confirm the final set of possibilities.
I use this pattern myself for Google Maps API keys, which in my case need to be different per-customer (flavour) and are already different between release and debug because of the difference in app signing.
You should try this
productFlavors {
flavor1 {
applicationId "com.myapp.flavor1"
}
flavor2 {
applicationId "com.myapp.flavor2"
}
}
sourceSets.flavor1 {
res {
srcDir 'flavor1'
}
resources {
srcDir 'flavor1'
}
}
sourceSets.flavor2 {
res {
srcDir 'flavor2'
}
resources {
srcDir 'flavor2'
}
}
}

Is it possible to have one proguard mappings.txt file for multiple flavor?

I would like to publish my app to multiple market. the version code should be same in all market and my app has IAP so i should have multiple flavor and one manifest per flavor.
And also i need the mapping file for the crash report(for example in tracepot).
My problem is: How can i have one mappings.txt file for multiple flavor?
build.gradle:
android {
productFlavors {
Market1 {
...
}
Market2 {
....
}
signingConfigs {
debug {
....
}
release {
....
}
}
buildTypes {
debug {
debuggable true
signingConfig signingConfigs.debug
}
release {
debuggable false
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Yes, the current settings in your build.gradle will apply the proguard file to all release builds of all flavors. You already have it there.
Update after the conversation with OP in comments below:
Now i understood the question better. You are trying to have a single mapping.txt file and are not talking about the proguard-rules.pro. The answer is No, you cannot have a single file for multiple generated apks. Proguard rules are applied to each flavor separately and each time it generates a separate mapping file. There might not be a any differences in your mapping.txt files right now because you may not be using different sourceSets (Java classes). But, if you have different sourceSets, the mappings.txt file will not be the same. Probably you have only minimal difference between your flavors right now like different drawables with same name, If i'm assuming right.

Avoid shrinking/removing wearable micro app from apk file

I activated the resouce shrinking in my build.gradle but now my embeded wearable app is stripped out. How can I avoid that my micro app is removed, because it is unused?
Skipped unused resource res/raw/android_wear_micro_apk.apk: 382310 bytes
Since I want to shrink the other not used resouces I'm using this DSL:
buildTypes {
release {
shrinkResources true
// ...
}
}
I would guess that I need to use proguard but I have no idea how to achieve that. I checked of cause the documentation, but I didn't get it how protect a single member variable.
Are you referencing the R.raw.apkpath? Looking at the Packaging Wearable Apps training mentions rawPathResId in the res/xml/wearable_app_desc.xml
On a side note enabling proGuard is simple with Gradle
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
This is Bug 78620 and was fixed in the gradle build tools 0.14.1.

Categories

Resources