Android | Can't build multi flavorDimension - android

I have multi dimensionFlavor as:
flavorDimensions "app", "endpoint"
productFlavors {
ph {
applicationIdSuffix ".app1"
dimension "app"
}
pac {
applicationIdSuffix ".app2"
dimension "app"
}
production {
dimension "endpoint"
// applicationIdSuffix ".production"
}
staging {
dimension "endpoint"
// applicationIdSuffix ".staging"
}
}
But I want the application id to have both the app and env name. like: com.company.application.app1.staging
And then comes the confusion in the directory structure to put the google-services.json separately for each package name.
I've tried like app/src/app1Staging/google-services.json
But it fails in the build.

google-services.json should be put under assets directory and accessed via AssetManager, something like AssetManager assetMgr = activity.getAssets();
And the paths should be organised according to your build flavors, e.g.
app/src/staging/assets/google-services.json

Related

Use different googleservices of different projects in single app android

In my application i have different productFlavors(DEV, DEMO,PROD) and each productFlavors has debug and release modes.
As i need to use different values for remote config, i have created 2 projects, one for Prod, another one QA. Now i have 2 different google services.json file.
I tried to keep json file in my app and run app, but app is always expecting json file in app folder and if keep one in app folder and another one at flavor folder is is not picking flavor json and always taking app json file.
buildTypes {
release {
}
debug {
}
}
// Specifies one flavor dimension.
flavorDimensions "client", "server"
productFlavors {
Customer1 {
dimension "client"
}
Customer2 {
dimension "client"
}
DEV {
dimension "server"
applicationIdSuffix ".dev"
}
DEMO {
dimension "server"
applicationIdSuffix ".demo"
}
PROD {
dimension "server"
}
QA {
dimension "server"
applicationIdSuffix ".qa"
}
}
Any help on this?

How to include a dependency in an Android gradle file based on flavorDimension?

I have an Android project with 5 flavors and 2 flavorDimensions.
See Below:
flavorDimensions "env", "device"
productFlavors {
dev {
applicationIdSuffix ".dev"
dimension "env"
}
staging {
applicationIdSuffix ".staging"
dimension "env"
}
prod {
applicationIdSuffix ""
dimension "env"
}
amazon {
dimension "device"
}
google {
dimension "device"
}
}
Is there a way to only include a dependency to one flavorDimension instead of just one flavor? Basically I have a dependency I only want included in googleDev, googleStaging, and googleProd (but not the amazon dimension)
Is there an easy way to do this without repeating each dependency 3 times?

Import lib with multiple flavor

I have a lib with this flavors:
flavorDimensions "dim"
productFlavors {
nocustomer {
versionNameSuffix "-nocustomer"
dimension = "dim"
}
customer001 {
versionNameSuffix "-customer001"
dimension = "dim"
}
}
My build produce this file:
How to use my library with different flavors?
Example: i want to build an app that use nocustomer flavor. What should i write on gradle app file? Which library to include?

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

Build android with gradle, replace string each product flavor

Before i build android project to two different application paid and free.
I changed each values and strings so yesterday I made a big mistake.
So, I'm laerning how to use gradle to build my app.
My app have some differents.
app name (just add suffix '-Free') -> values/string.xml
change flag in my *.java
// signingConfigs is ommited.
productFlavors{
free{
packageName "my.app.free"
versionCode 20
signingConfig signingConfigs.freeConfing
copy{
from('/res'){
include '**/*.xml'
}
into 'build/res/'
filter{
String line -> line.replaceAll("android:label=\"#string/app_name\"", "android:label=\"#string/app_name_free\"")
}
}
copy{
from('/src'){
include '**/*.java'
}
into 'build/src/'
filter{
String line -> line.replaceAll("public static final Boolean IS_FULL_VER = true;", "public static final Boolean IS_FULL_VER = false;")
}
}
}
paid{
packageName "my.app.paid"
versionCode 20
signingConfig signingConfigs.paidConfing
}
}
but, built app changed nothing at all.
What i missed?
See the documentation on product flavors:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors
In your build.gradle, in each flavor, you can define flags to be generated in your BuildConfig.java file:
productFlavors {
free {
packageName "com.company.appfree"
buildConfig "public final static com.company.common.MonetizationType monetizationType = com.company.common.MonetizationType.FREE;"
}
paid {
packageName "com.company.apppaid"
buildConfig "public final static com.company.common.MonetizationType monetizationType = com.company.common.MonetizationType.PAID;"
}
}
This example uses an enum (that you need to define somewhere in your java code):
public enum MonetizationType {
PAID, FREE
}
You can now use this anywhere like this:
if (BuildConfig.monetizationType == MonetizationType.FREE) { ... }
For overriding resources, you can create different resource files in the source folders for each flavor:
Use the following structure
app/build.gradle
app/ [.. some other files...]
app/src/main/
app/src/main/java
app/src/main/res
app/src/main/assets
app/src/main/AndroidManifest.xml
app/src/free/res/values/apptitle.xml
app/src/paid/res/values/apptitle.xml
apptitle.xml would be a string resource file (just like strings.xml), but with only one string: the one you want to be different depending on the flavor.
(You don't need have a apptitle.xml in your main/res directory).
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="app_title">App Title (or whatever you want)</string>
</resources>
You might be able to override strings in different ways, but I like to keep the overridden strings separate from the rest for clarity.
The accepted answer is not working with the newer versions of Gradle.
You need to replace buildConfig with buildConfigField to get the same result:
productFlavors {
free {
packageName "com.company.appfree"
buildConfigField "com.company.common.MonetizationType", "MONETIZATION_TYPE", "company.common.MonetizationType.FREE"
}
paid {
packageName "com.company.apppaid"
buildConfigField "com.company.common.MonetizationType", "MONETIZATION_TYPE", "company.common.MonetizationType.PAID"
}
}
treesAreEverywhere's answer (as well as user name) is right on. But it's also very valuable to know that Android Studio will greatly simplify the process of creating build flavor or attribute specific resource files.
In the AS project window, right click on the res/values folder and select New > Values resource file. Then name it (e.g., "strings"), select the Source set if not the default, and select any desired qualifiers (e.g., Screen Width = 800). This is the easiest way to make sure you're putting your resource overrides where the compiler wants them.
This is how I did in my project. I created multiple build types instead of flavours. This solution will add a prefix to your package name based on what build type you are trying to assemble. For ex, for dev the package name will be com.sample.myapp.dev and similarly for beta package name will be changed to com.sample.myapp.release. You can tweak it to get Free and Paid prefixes. Hope it would help.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.production
applicationIdSuffix '.release'
versionNameSuffix '-RELEASE'
}
dev {
signingConfig signingConfigs.debug
applicationIdSuffix '.dev'
versionNameSuffix '-DEV'
}
beta {
signingConfig signingConfigs.debug
applicationIdSuffix '.beta'
versionNameSuffix '-BETA'
}
debug {
signingConfig signingConfigs.debug
applicationIdSuffix '.debug'
versionNameSuffix '-DEBUG'
debuggable true
}
}

Categories

Resources