Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set variable according to gradle flavors

I want to pass a variable test that I set differently per flavor as a define to the NDK. But for some reason he always passes the value of the last flavor.

Here is the build.gradle:

apply plugin: 'com.android.library'  def test  android {     compileSdkVersion 23     buildToolsVersion "23.0.2"     defaultPublishConfig "flavorARelease"     publishNonDefault true      defaultConfig {         minSdkVersion 15         targetSdkVersion 17          ndk {             moduleName "test"             ldLibs "log"         }     }      productFlavors {             flavorA {             test = 1         }          flavorB {             test = 2         }         }      buildTypes {             debug {             ndk {                 if (cFlags == null) { cFlags = "" }                 cFlags = cFlags + " -DLOGGING=1 -DTEST="+test+" "             }             minifyEnabled false         }         release {             ndk {                 if (cFlags == null) { cFlags = "" }                 cFlags = cFlags + " -DLOGGING=0 -DTEST="+test+" "             }             minifyEnabled true             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'         }     } }  dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     testCompile 'junit:junit:4.12'     compile 'com.android.support:appcompat-v7:23.1.1' } 

And here are the CFLAG lines from the generated Android.mk

build/intermediates/ndk/flavorA/debug/Android.mk:

LOCAL_CFLAGS :=  -DLOGGING=1 -DTEST=2 

I expected -DTEST=1 here

build/intermediates/ndk/flavorB/debug/Android.mk:

LOCAL_CFLAGS :=  -DLOGGING=1 -DTEST=2 

So where is my mistake? Or how can I achieve my goal? Please also consider that the real problem is more complex and I want to make those defines in the "buildTypes" segment if possible.

like image 324
Torge Avatar asked Mar 16 '16 13:03

Torge


People also ask

How do I get the current flavor in Gradle?

There is no direct way to get the current flavor; the call getGradle(). getStartParameter().

What is a Buildtype in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

When would you use a product flavor in your build setup?

When to use Product Flavors. When we want to address the issue of having separate project code for each version of the app while still having one project code. Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.

What is flavorDimensions?

A flavorDimension is something like a flavor category and every combination of a flavor from each dimension will produce a variant. In your case, you must define one flavorDimension named "type" and another dimension named "organization".

What are set variables in Gradle?

Set variables in build.gradle. The new Gradle based build system offers a lot of cool features for Android developers. One of those is the ability to parametrize application build process. This means that you can create different variants of the application and set variables based on the variant or build type.

What is productflavours in Gradle?

Till Gradle introduced ProductFlavours we were only able to build application with the a single configuration or default configuration. Since the introduction of ProductFlavours, we are able to create variables based on build types.

How does Gradle perform variant aware selection?

In both cases, Gradle performs variant aware selection. Local components expose variants as outgoing configurations, which are consumable configurations . When dependency resolution happens, the engine will select one variant of an outgoing component by selecting one of its consumable configurations.

How to check for custom fields created in Gradle build?

During build time, Gradle generates the BuildConfig.java class in which you can check and confirm for custom fields like, BASE_URL created using the buildConfigField () Extracting the configurable items from the code base to build scripts is one of the important process in defining ProductFlovours in build.gradle.


Video Answer


1 Answers

You can use buildConfigField

productFlavors {     demo {         buildConfigField "int", "FOO", "1"         buildConfigField "String", "FOO_STRING", "\"foo1\""     }     full {         buildConfigField "int", "FOO", "2"         buildConfigField "String", "FOO_STRING", "\"foo2\""     } } 
like image 145
Linh Avatar answered Sep 23 '22 04:09

Linh