Gradle task to change boolean value in assembly configuration

I would like to create a very simple task that changes the boolean value in my gradle configuration.

I am working on an Android application that can be run with multiple profiles, and for each build you need to indicate whether the application will fake Bluetooth in my code or not.

My gradle (corresponding code):

def fakeBluetooth = "true" buildTypes { debug { minifyEnabled false signingConfig android.signingConfigs.debug buildConfigField "boolean", "fakeBluetooth", fakeBluetooth } release { minifyEnabled true signingConfig android.signingConfigs.release buildConfigField "boolean", "fakeBluetooth", fakeBluetooth } } task noFakeBluetooth { fakeBluetooth = "false" } 

Example usage in my java code:

 if (BuildConfig.fakeBluetooth) { processFictiveBluetoothService(); } else { // other case } 

Command line usage examples:

 ./gradlew iDebug noFakeBluetooth 

and

 ./gradlew iDebug 

Problem: in both cases, the fakeBluetooth value is always "true" (with or without "noFakeBluetooth" in the cmd line).

+6
source share
3 answers

You can use project properties to pass a value:

 buildTypes { debug { minifyEnabled false signingConfig android.signingConfigs.debug buildConfigField "boolean", "fakeBluetooth", fakeBluetooth() } release { minifyEnabled true signingConfig android.signingConfigs.release buildConfigField "boolean", "fakeBluetooth", fakeBluetooth() } } def fakeBluetooth() { def value = project.getProperties().get("fakeBluetooth") return value != null ? value : "true" } 

And then you can pass the property with:

 ./gradlew iDebug -PfakeBluetooth=true 
+16
source

It works

  android.defaultConfig.buildConfigField "String", "value", "1" 
0
source

I think the correct approach would be to determine the value of the resource for buildTypes or productFlavours:

resValue "string", "key", "value"

then read it inside your code, for example: getResources().getString(R.string.key);

0
source

Source: https://habr.com/ru/post/982376/


All Articles