Gradle: android.variantFilter cause build fail after upgrade to Gradle 2.2.1
Asked Answered
R

1

1

I am working in an Android project which has many flavors, I used this code in build.gradle to set the package name for each flavor:

flavorDimensions "type", "feature"

productFlavors {
    abc { flavorDimension "type" }

    def { flavorDimension "type" }

    ABC { flavorDimension "feature" } 

    DEF { flavorDimension "feature" } 
}
android.variantFilter { variant ->
    def flavorString = ""
    def flavors = variant.getFlavors()
    for (int i = 0; i < flavors.size(); i++) {
        flavorString += flavors[i].name;
    }
    if(flavorString.equalsIgnoreCase("abcABC")) {
        variant.getDefaultConfig().applicationId "com.my.app.abc.abc"
    }
    if(flavorString.equalsIgnoreCase("abcDEF")) {
        variant.getDefaultConfig().applicationId "com.my.app.abc.def"
    }
    if(flavorString.equalsIgnoreCase("defABC")) {
        variant.getDefaultConfig().applicationId "com.my.app.def.abc"
    }
    if(flavorString.equalsIgnoreCase("defDEF")) {
        variant.getDefaultConfig().applicationId "com.my.app.def.def"
    }
}

Everything was fine until I updated my gradle from 1.10.0 to 2.2.1, the build is failed and I get this error message:

Error:(63, 0) No signature of method: com.android.build.gradle.internal.api.ReadOnlyProductFlavor.applicationId() is applicable for argument types: (java.lang.String) values: [com.hac.apps.megahd.acc]
Possible solutions: getApplicationId()

It seems like the variant.getDefaultConfig().applicationId is working different now. I searched the internet for the document for android.variantFilter but it seems not to be exist.

Anyone can tell me how do I get this code work in gradle 2.2.1? Thanks a lot.

Racemose answered 3/2, 2015 at 12:50 Comment(0)
P
4

The variantFilter is meant to be used only to allow or disallow builds of certain combinations. The defaultConfig object is shared and read-only. If you want to assign applicationId's to variants, I think you should something more similar to this:

buildTypes {
applicationVariants.all { variant ->

    def projectFlavorNames = []

    variant.productFlavors.each() { flavor ->
        projectFlavorNames.add(flavor.name)
    }

    project.logger.debug('Application variant ' + variant.name + '. Flavor names list: ' + projectFlavorNames)

    if (projectFlavorNames.contains('customer1') && projectFlavorNames.contains('variant1')) {
        variant.mergedFlavor.applicationId = 'com.customer1.variant1'
    } else if (projectFlavorNames.contains('customer2') && projectFlavorNames.contains('variant2')) {
        variant.mergedFlavor.applicationId = 'com.customer2.variant2'
    } // else use standard package name

    project.logger.debug('Using project name: ' + variant.packageName)
}

// ...

}

From Dynamically generate package name for multi-flavors configuration

Parke answered 3/2, 2015 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.