Dynamically generate package name for multi-flavors configuration

假如想象 提交于 2019-12-11 00:31:16

问题


I have Gradle Android project that will be used for several customers. Also it will have free and paid version. I realized that it can be achieved by using flavorDimensions. But the problem is that I want to have a method to generate package name depending on selected flavors.

flavorDimensions 'branding', 'version'
productFlavors {
    free {
        flavorDimension 'version'
    }
    paid{
        flavorDimension 'version'
    }
    customer1 {
        flavorDimension 'branding'
    }
    customer2 {
        flavorDimension 'branding'
    }
}

// pseudocode
def getGeneratePackageName() {
    if (customer1 && free) {
        return 'com.customer1.free'
    }
    if (customer2 && free) {
        return 'com.customer2.free'
    }
    if (customer1 && paid) {
        return 'com.customer1.paid'
    }
    if (customer2 && paid) {
        return 'com.customer2.paid'
    }
}

I wonder when do I need to call this method and what variable do I need to set?


回答1:


Figured it out how to implement this. Groovy code below allows to get flexibility in generation of package names.

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.packageName = 'com.customer1.variant1'
        } else if (projectFlavorNames.contains('customer2') && projectFlavorNames.contains('variant2')) {
            variant.mergedFlavor.packageName = 'com.customer2.variant2'
        } // else use standard package name

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

    // ...

}


来源:https://stackoverflow.com/questions/24303522/dynamically-generate-package-name-for-multi-flavors-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!