It is in android gradle build scripts. I have multiple flavors in gradle:
flavorDimensions "brand", "appVariant"
productFlavors {
stage {
dimension "appVariant"
}
prod {
dimension "appVariant"
}
brand1 {
dimension "brand"
}
brand2 {
dimension "brand"
}
}
How can I rewrite that using kotlin-dsl?
This will work:
flavorDimensions("brand", "appVariant")
productFlavors {
create("var1") {
setDimension("appVariant")
}
create("var2") {
setDimension("appVariant")
}
create("brand1") {
setDimension("brand")
}
create("brand2") {
setDimension("brand")
}
}
Related
I have the following flavours configuration in an Android project:
android {
buildTypes {
release { }
debug { }
}
flavorDimensions "fruit", "color"
productFlavors {
apple {
dimension "fruit"
}
peach {
dimension "fruit"
}
banana {
dimension "fruit"
}
red {
dimension "color"
}
green {
dimension "color"
}
yellow {
dimension "color"
}
}
}
dependencies {
appleRedImplementation 'com.apple:color_red:1.1.0'
appleGreenImplementation 'com.apple:color_green:1.1.0'
appleYellowImplementation 'com.apple:color_yellow:1.1.0'
// No dependency for other fruit build type
}
I need to keep the com.apple:color_*:1.1.0 dependency only for the apple flavorDimensions builds, but change it based on the color flavorDimensions builds?
If you want to add dependencies to specific configurations of multiple build flavor dimensions, you need to define those combinations first:
android{
...
productFlavors {
...
}
configurations {
appleRedImplementation {}
appleGreenImplementation {}
appleYellowImplementation {}
}
}
This answer here references this as well: https://stackoverflow.com/a/60776044/2258611
In the main build.gradle I have
android {
……
defaultConfig {
……
}
buildTypes {
debug {……}
release {……}
}
productFlavors {
lite {
apply from: 'lite_api_config.gradle'
……
}
pro {
apply from: 'pro_api_config.gradle'
……
}
}
}
lite_api_config.gradle
android {
defaultConfig {
buildConfigField('String', 'url', '"https://lite.com/"')
}
buildTypes {
debug {
buildConfigField('String', 'url', '"https://debug.lite.com/"')
}
release {
buildConfigField('String', 'url', '"https://release.lite.com/"')
}
}
}
pro_api_config.gradle
android {
defaultConfig {
buildConfigField('String', 'url', '"https://pro.com/"')
}
buildTypes {
debug {
buildConfigField('String', 'url', '"https://debug.pro.com/"')
}
release {
buildConfigField('String', 'url', '"https://release.pro.com/"')
}
}
}
But when I build this, using a build variant of liteDebug, in the BuildConfig.url I get "https://debug.pro.com/", which is wrong and looks like both the child gradle files are included somehow
Is it even possible to have separate files for different product flavors and include them in the build.gradle based on which flavor is being built.
I've experimented gradle-kotlin-dsl in an android project. I'm managed to make it work, but I'm stuck in how to define productFlavors
android {
compileSdkVersion(Config.Android.compileSdkVersion)
buildToolsVersion(Config.Android.buildToolsVersion)
defaultConfig {
minSdkVersion(Config.Android.minSdkVersion)
targetSdkVersion(Config.Android.targetSdkVersion)
versionCode = Config.Version.code
versionName = Config.Version.name
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
flavorDimensions("dimension")
productFlavors {
//product flavors here
}
}
After some investigation I've got the solution, just use the create method:
productFlavors {
create("flavor1") {
//flavor configurations here
}
create("flavor2") {
//flavor configurations here
}
}
After some investigation I've got the solution, just use the create method, adding here in case someone need it:
productFlavors {
create("flavor1") {
//flavor configurations here
}
create("flavor2") {
//flavor configurations here
}
}
As of com.android.tools.build:gradle:1.3.0 you'll get 'flavorDimension' will be removed by Android Gradle Plugin 2.0, it has been replaced by 'dimension'.
This is my build file:
build.gradle
android {
flavorDimensions "store", "api"
productFlavors {
googleplay {
flavorDimension "store"
}
amazon {
flavorDimension "store"
}
pre21 {
flavorDimension "api"
}
post21 {
flavorDimension "api"
}
}
}
In order to fix it one has to rename flavorDimension simply to dimension.
android {
flavorDimensions "store", "api"
productFlavors {
googleplay {
dimension "store"
}
amazon {
dimension "store"
}
pre21 {
dimension "api"
}
post21 {
dimension "api"
}
}
}
When using Gradle flavorDimensions, is it possible to exclude specific variants?
For example -
android {
...
flavorDimensions "abi", "version"
productFlavors {
freeapp {
flavorDimension "version"
...
}
x86 {
flavorDimension "abi"
...
}
}
the following build variants will be created:
x86-freeapp-debug
x86-freeapp-release
arm-freeapp-debug
arm-freeapp-release
mips-freeapp-debug
mips-freeapp-release
x86-paidapp-debug
x86-paidapp-release
arm-paidapp-debug
arm-paidapp-release
mips-paidapp-debug
mips-paidapp-release
Can "mips-paidapp-release" be manually removed?
Since Gradle 0.9 you can apply a variant filter and iterate over them:
productFlavors {
freeapp {
dimension "version"
}
x86 {
dimension "abi"
}
paidapp {
dimension "mips"
}
}
// Loop variants
android.variantFilter { variant ->
// Loop flavors
variant.getFlavors().each { flavor ->
println variant.buildType.name + " " + flavor.name + " " + flavor.dimension
if (variant.buildType.name.equals('release') &&
flavor.name.equals('paidapp') &&
flavor.dimension.equals('mips')) {
variant.setIgnore(true)
}
}
}
Note: that I changed flavorDimension to dimension because the latter is now the preferred way specify it.
Note2: the above note requires you to use the newer gradle version:
Project/build.gradle should have the following:
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
while Project/app/build.gradle should have this:
android {
buildToolsVersion "22.0.1"
...
}
This is how I do it:
flavorDimensions "device", "server"
productFlavors {
emulator {
dimension = "device"
}
phone {
dimension = "device"
}
staging {
dimension = "server"
}
production {
dimension = "server"
}
}
android.variantFilter { variant ->
def device = variant.getFlavors().get(0).name
def server = variant.getFlavors().get(1).name
def isRelease = variant.buildType.name.equals('release')
def isDebug = variant.buildType.name.equals('debug')
// Disable emulatorProductionRelease build variant
if (device.equals('emulator') && server.equals('production') && isRelease) {
variant.setIgnore(true)
}
}
I like it because it's easy to read and you can target specific build variants.