Gradle 风味变量的抽象

abstraction of Gradle Flavor variable to

最近,我弄清楚了如何使用占位符进行清单合并。我在我的应用程序中实现了它,在大多数情况下,我对结果非常满意 -

以前,您必须使用一个字符串创建一个新的稀疏 String Resource,该字符串具有您想要在不同资源(如风格和构建类型)上更改的属性。就个人而言,我认为该方法不是很干净。简单易行,是的,但不干净。

据此,我创建了以下有效的方法。但是,我不喜欢重复代码,我的 Flavor 中的每个人都会有重复代码。难道这东西是可以抽象出来的?也许可以把它放在 defaultConfig?

例如,在下面的代码中,有没有什么方法可以移动每个 Flavor 中存在的 manifestPlaceholders 代码,并以某种方式将其抽象为 defaultConfig,这样我就可以定义它一次?

android {
    ...
    ext {
        APP_NAME = "App Name"
        HUB_NAME = "Hub"
    }

    defaultConfig {
        ...
    }

    productFlavors {
        entity_1 {
            versionNameSuffix ' - Entity_1'
            applicationIdSuffix 'entity_1'
            manifestPlaceholders = [ applicationLabel: APP_NAME + versionNameSuffix ]
        }
        entity_2 {
            versionNameSuffix ' - Entity_2'
            applicationIdSuffix 'entity_2'
            manifestPlaceholders = [ applicationLabel: APP_NAME + versionNameSuffix ]
        }

        ...

        entity_n {
            versionNameSuffix ' - Entity_n'
            applicationIdSuffix 'entity_n'
            manifestPlaceholders = [ applicationLabel: APP_NAME + versionNameSuffix ]
        }
        hub {
            versionNameSuffix ' - Hub'
            applicationIdSuffix 'hub'
            manifestPlaceholders = [ applicationLabel: HUB_NAME ]
        }
    }

    buildTypes {
        release {
            ...
        }
    }

    applicationVariants.all { variant ->

        if (variant.buildType.name != "release") return

        if (variant.flavorName != "hub") {
            variant.mergedFlavor.manifestPlaceholders = [applicationLabel: APP_NAME]
        } 
    }

顺便说一句,上面的代码会给我 applicationLabels 以下内容。我使用这些来确定主屏幕上应用程序的名称。

想要

Flavor     Debug App Name             Release App Name
--------   --------------             ----------------
entity_1   App Name - Entity_1_name   App Name
entity_2   App Name - Entity_2_name   App Name
...        ...                        ...
entity_n   App Name - Entity_n_name   App Name
hub        Hub                        Hub

想做就做。

问题中的当前实现是尝试首先创建最具体的名称,然后删除特定风格中不​​需要的信息。

与思路相反,我需要先创建最通用的名称,然后添加特定口味所需的信息。

android {
    ...
    ext {
        APP_NAME = "App Name"
        HUB_NAME = "Hub"
    }

    defaultConfig {
        ...
        manifestPlaceholders = [ applicationLabel: APP_NAME ]
    }

    productFlavors {
        entity_1 {
            versionNameSuffix ' - Entity_1'
            applicationIdSuffix 'entity_1'
        }
        entity_2 {
            versionNameSuffix ' - Entity_2'
            applicationIdSuffix 'entity_2'
        }

        .
        .
        .

        entity_n {
            versionNameSuffix ' - Entity_n'
            applicationIdSuffix 'entity_n'
        }

        hub {
            versionNameSuffix ' - Hub'
            applicationIdSuffix 'hub'
            manifestPlaceholders = [ applicationLabel: HUB_NAME ]
        }
    }

    buildTypes {
        release {
            ...
        }
    }

// BEFORE the applicationVariants.all { ... } code runs, this is what all the applicationLabel look like. We are close, but need to ADD to them...

//     Flavor     Debug App Name             Release App Name
//    --------   --------------             ----------------
//    entity_1   App Name                   App Name
//    entity_2   App Name                   App Name
//    ...        ...                        ...
//    entity_n   App Name                   App Name
//    hub        Hub                        Hub

    applicationVariants.all { variant ->

        // Don't modify the release build or the hub flavor. They are good already.
        if (variant.buildType.name == "release" || variant.flavorName == "hub") return
        variant.mergedFlavor.manifestPlaceholders = [applicationLabel: APP_NAME + variant.mergedFlavor.versionNameSuffix]
    }

// AFTER the applicationVariants.all { ... } code runs, this is what all the applicationLabel look like. We are done!

//     Flavor     Debug App Name             Release App Name
//    --------   --------------             ----------------
//    entity_1   App Name - Entity_1_name   App Name
//    entity_2   App Name - Entity_2_name   App Name
//    ...        ...                        ...
//    entity_n   App Name - Entity_n_name   App Name
//    hub        Hub                        Hub