Search code examples
androidandroid-ndkandroid-gradle-pluginandroid-productflavorsgradle-experimental

Flavor dimensions and variant filters for experimental plugin?


Does anyone know the syntax for defining flavor dimensions and variant filters using the experimental Gradle plugin? (This would include the syntax for assigning a flavor to a particular dimension inside each flavor's create() block.)

The experimental plugin user guide doesn't address any of this and I couldn't find any examples in the code samples. I'm using plugin version 0.2.1 (com.android.tools.build:gradle-experimental:0.2.1). The syntax for the regular plugin clearly doesn't work at all.

Here's the original script, which defines product flavors for Google Play and for Amazon AppStore:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = '23.0.1'

        defaultConfig.with {
            ... // normal stuff
        }
    }
    android.aaptOptions {
        noCompress = 'dict'
    }
    android.ndk {
        ... // normal stuff
    }
    android.compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
    }
    android.buildTypes {
        release {
            minifyEnabled = true
            proguardFiles += file('proguard-rules.pro')
        }
    }
    android.productFlavors {
        create("google") {
        }
        create("amazon") {
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:support-annotations:23.1.0'
}

Since this app uses native code, I want to add a dimension for different hardware platforms. Also, since the Amazon AppStore and Google Play have different models for supporting different hardware, I want to use variant filters to treat hardware platforms differently for the different markets.

I've stumbled about blindly in the dark tried a number of things to define flavor dimensions; these all generate errors when compiling the Gradle script.

I haven't tried creating variant filters yet, since I'm stuck at step 1, but absent guidance from someone "in the know", I expect equal (lack of) success.

Attempt 1:

apply plugin: 'com.android.model.application'

model {
    android {
        ...

        defaultConfig.with {
            ...
            flavorDimensions = ["abi", "market"]
        }
    }
    ...
}
...

Error:

No such property: flavorDimensions for class: com.android.build.gradle.managed.ProductFlavor

Attempt 2:

apply plugin: 'com.android.model.application'

model {
    android {
        ...

        defaultConfig.with {
            ...
        }
        flavorDimensions = ["abi", "market"]
    }
    ...
}
...

Error:

No such property: flavorDimensions for class: com.android.build.gradle.managed.AndroidConfig

Attempt 3:

apply plugin: 'com.android.model.application'

model {
    android {
        ... // as in original script
    }
    android.flavorDimensions {
        create("abi") {}
        create("market") {}
    }
}
...

Error:

A problem occurred configuring project ':app'.

The following model rules are unbound: model.android.flavorDimensions
Mutable:
- android.flavorDimensions (java.lang.Object)

Attempt 4:

apply plugin: 'com.android.model.application'

model {
    android {
        ...
    }
    android.flavorDimensions = ["abi", "market"]
}

Error:

No such property: flavorDimensions for class: org.gradle.model.dsl.internal.NonTransformedModelDslBacking


Solution

  • I also had this issue today. Finally, I found that you don't need to specify android.flavorDimensions. You only need to simply add dimension to each flavors.

    I am using experimental experimental plugin 0.2.1 with gradle-2.5.

    android.productFlavors {
        create("product1"){
            dimension = "product"
        }
        create("product"){
            dimension = "product"
        }
        create("arm"){
            dimension = "abi"
        }
        create("armv7"){
            dimension = "abi"
        }
    }