Gradle:android.variantFilter升级到Gradle 2.2.1后导致构建失败

时间:2015-02-03 12:50:01

标签: android gradle variant

我在一个有很多种风格的Android项目中工作,我在build.gradle中使用此代码为每种风格设置包名称:

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"
    }
}

一切都很好,直到我将我的gradle从1.10.0更新到2.2.1,构建失败并收到此错误消息:

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()

现在似乎variant.getDefaultConfig().applicationId工作正常。我在互联网上搜索android.variantFilter的文件,但似乎不存在。

任何人都可以告诉我如何在gradle 2.2.1中使用此代码?非常感谢。

1 个答案:

答案 0 :(得分:3)

variantFilter仅用于允许或禁止某些组合的构建。 defaultConfig对象是共享且只读的。如果您想将applicationId分配给变体,我认为您应该更类似于此:

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)
}

// ...

}

来自Dynamically generate package name for multi-flavors configuration