Export environment variables in build.gradle - android

Gradle supports multiple flavor builds for Android Applications. How can I export an environment variable which has different flavor values? For example, export FLAVOR with value "flavor1" when flavor1 is built and "flavor2" for flavor2 build.
android {
...
productFlavors {
flavor1 {
...
}
flavor2 {
...
}
}
}

This link http://inaka.net/blog/2014/12/22/create-separate-production-and-staging-builds-in-android/ may help you out.
If you have two productFlavors (Production and Staging, for instance)
You should create two different resource folders:
project/app/src/production/res/values/strings.xml
<resources>
<string name="root_url">http://production.service.com/api</string>
</resources>
project/app/src/staging/res/values/strings.xml
<resources>
<string name="root_url">http://staging.service.com/api</string>
</resources>
You should add this following code inside the android {}:
productFlavors {
production {
applicationId "com.inaka.app.production"
}
staging {
applicationId "com.inaka.app.staging"
}
}
It's a good idea to have different icons for different productFlavors, just add the icon inside each different resource folder.

Related

How to use sourceSets to replace/override app icon in gradle

I'd like to use different icon in different flavor.
There's a default icon in my main sourceSets (in dir src/main/res/mipmap-*dpi), and I try to replace/override icon in build.gradle with sourceSets.
buildTypes {
release {
...
}
debug {
...
}
}
flavorDimensions "d1", "d2"
dimension11 {
dimension "d1"
}
dimension21 {
dimension "d2"
sourceSets.debug {
resources {
srcDir 'config/icon/free' // my icon is in free/res/mipmap-*dpi
exclude 'src/main/res/mipmap-*dpi' //not working, mipmap-*dpi and mipmap-*dpi-v4 are all in the final apk file.
}
}
}
dimension22 {
dimension "d2"
}
dimension23 {
dimension "d2"
}
It's not working, mipmap-*dpi and mipmap-*dpi-v4 are all in the final apk file. mipmap-*dpi-v4 are generated by agp.
If I rename mipmap-*dpi which in dir config/icon/free to mipmap-*dpi-v4, then:
Entry name 'res/mipmap-hdpi-v4/ic_launcher.png' collided
If I remove files which are in src/main/res/mipmap-*dpi,
AAPT: error: resource mipmap/ic_launcher (aka mypackage:mipmap/ic_launcher) not found.
So how can I replace or override app icon? Can I achieve that with custom sourceSets?
TASK: installDimension11Dimension21Debug
GRADLE: 6.8.3
AGP: 4.1.3
To use different app icons for different flavors, you need to create directory under src dir on the name of flavor. Add mimap folders under that dir with app icons.

Product flavors in android not working

I have been stuck on this for a day now. I created 2 flavors for my project and added them under the buildtype in android in build.gradle. I then created a res folder for both and changed the icons for each. When I run the project for any of the variants, it is showing the icon from the project.
My project structure in as follows
[project]
[app]
src
main
res
mipmap
flavor1
flavor2
res
mipmap
and my build.gradle is
productFlavors {
flavor1 {
applicationId "abc"
}
flavor2 {
applicationId "abc"
}
}
sourceSets {
flavo1 {
res.srcDirs = ['src/flavor1/res']
}
flavor2 {
res.srcDirs = ['src/flavor2/res']
}
}
PS: nvm the naming
You should remove the icon from main and leave it only in flavor1 and flavor2 directories.
Also check the names of the flavors in the build.gradle, I think it should be flavor1 instead of flavo1 and flavor2 instead of staging.
Another note, sourceSets definition is not required, standard res directories inside flavors' directories will be used automatically.

Combining product flavors from two modules

I have a project with two flavors dimensions. One is endpoint and it is used for switching prod/staging. Other is site and it is for different color schemes and data sources. There are about 20 site flavors and only two endpoints. So, flavors configuration looks like this:
flavorDimensions "site", "endpoint"
productFlavors {
pro {
dimension 'endpoint'
}
staging {
dimension 'endpoint'
}
site1 {
dimension 'site'
applicationId "co.site1"
}
…
site20 {
dimension 'site'
applicationId "co.site20"
}
}
Now I need to move API requests to separate module. So, it'll be fine to move endpoints configuration to this module. So, app flavors should be:
In app:
productFlavors {
site1 {
applicationId "co.site1"
}
…
site20 {
applicationId "co.site20"
}
}
In API library:
productFlavors {
pro {
}
staging {
}
}
But how to combine them? Earlier I could easily get builds like App-proSite15. How to configure the same with two modules?
Currently my solution is to leave configuration as is. I have only string resources in endpoint flavors, and can access them in API module by string name via getIdentifier

Android Gradle identify current falvor at compile time

How is it possible to identify the current flavor being compiled. I'm trying to add a file to compile only if I'm compiling a certain product flavor.
buildTypes {
android.applicationVariants.all { variant ->
variant.productFlavors.each() { flavor ->
if (flavor.name.equals(currentFlavorName)) {
The problem is that I can't seem to find where the currentFlavourName of the flavor which I am currently building is located.
just put the strings you want for flavor1 into:
src/flavor1/res/values/strings.xml
and the strings for flavor2 into:
src/flavor2/res/values/strings.xml
no need to put logic into your gradle file
Android uses a unique build process regarding your resources for different flavors and it is very easy to control.
if you set up your main source:
project-name
------------/app
---------------/src
-------------------/main
------------------------/res
----------------------------/values
------------------------/java
-------------------/development
-------------------------------/res
-----------------------------------/values
-------------------------------/java
-------------------/production
------------------------------/res
----------------------------------/values
------------------------------/java
This would be a bottom up approach from product flavor into main. Meaning if you have a strings.xml with items having the same name existing in development/res/values and have values that also exist in main/res/values/strings.xml these will be over written (and same would go for the production flavor) based on the build variant defined in your gradle file.
android {
productFlavors {
production {
applicationId "com.test.prod"
versionName "1.0.0"
}
development {
applicationId "com.test.dev"
versionName "1.0.0"
}
}
I don't know if exits a method to get the currentFlavor. I haven't found it yet.
A ugly solution can be
variant.productFlavors.each() { flavor ->
if (flavor.name.equals("flavor1")) {
//..........
}
}
However, if you want to be able to control which strings.xml you are using, you can achieve it in different ways.
First of all you can just define a xml file in your flavor folder.
app/src/main/res/values/ -> for common resources
app/src/flavor1/res/values -> resources for flavor1
app/src/flavor2/res/values -> resources for flavor2
This doesn't require any config in your build.gradle script.
A second option is to define a resource value using build.gradle.
Something like:
productFlavors {
flavor1 {
resValue "string", "app_name", "IRCEnterprise"
}
//....
}
Another option is to create some field in your BuildConfig class using this kind of script.
productFlavors {
flavor1 {
buildConfigField "String", "name", "\"MY FLAVOR NAME\""
}
}

Combine BuildType and Flavor resources

I have 2 versions of app: paid-app and free-app. I use gradle and Android Studio.
I have next folder structure:
src folder, and inside it 3 folders: main, paidapp, freeapp - where paidapp and freeapp are flavors.
My configs:
defaultConfig {
applicationId "com.company"
}
buildTypes {
debug {
applicationIdSuffix '.dev'
}
}
productFlavors {
freeapp {
}
paidapp {
applicationId 'com.company.paidapp'
}
}
What I want: have on my phone 4 apps:
package com.company.dev, name "dev freeapp"
package com.company.paidapp.dev, name "dev paidapp"
package com.company, name "freeapp"
package com.company.paidapp, name "paidapp"
package is ok, gradle manage it automatically, but how to create different names for app?
App name located in strings.xml
in src/paidapp/res/values/strings.xml:
<string name="appName">dev paidapp</string>
in src/freeapp/res/values/strings.xml:
<string name="appName">dev freeapp</string>
I looked there - http://tools.android.com/tech-docs/new-build-system/resource-merging
The priority order is the following:
BuildType -> Flavor -> main -> Dependencies.
So I can chose or string from BuildType or from Flavor, but how to combine BuildType and Flavor?
Just create a folder src/freeappRelease/res/values/ and add your very specific strings in this folder.

Categories

Resources