动态生成的多口味的配置包的名称 [英] Dynamically generate package name for multi-flavors configuration

查看:254
本文介绍了动态生成的多口味的配置包的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将用于多个客户摇篮Android项目。它也将有免费和付费版本。我意识到,这可以通过使用flavorDimensions来实现。但问题是,我想有生成包名取决于所选择的口味的方法。

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?

推荐答案

想通了如何实现这一点。下面的Groovy code允许获得灵活代包名。

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

    // ...

}

这篇关于动态生成的多口味的配置包的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆