在插件的gradle应用变种获取包名变种、插件、gradle

2023-09-07 15:39:27 作者:゛mm、绅绅绅绅、绅士

我建立一个插件的Gradle,增加了一个新的任务为每个应用程序变种。这个新的任务需要应用变种的包名。

I'm building a gradle plugin that adds a new task for every application variant. This new task needs the package name of the application variant.

这是我目前的code,它停止使用最新版本的Andr​​oid插件的gradle工作:

This is my current code, which stopped working with the most recent version of the android gradle plugin:

private String getPackageName(ApplicationVariant variant) {
    // TODO: There's probably a better way to get the package name of the variant being tested.
    String packageName = variant.generateBuildConfig.packageName

    if (variant.processManifest.packageNameOverride != null) {
        packageName = variant.processManifest.packageNameOverride
    }
    packageName
}

本已经停止了最新版本的Andr​​oid插件的工作,因为更改构建配置处理。这似乎是一个黑客之前,无论如何,所以我并不感到惊讶它停止工作。有没有去取包名称规范的方式?

This has stopped working in the most recent version of the android plugin because of changes to the build config processing. It seemed like a hack before anyway, so I'm not surprised it stopped working. Is there a canonical way to fetch the package name?

推荐答案

我用这样的:

// Return the packageName for the given buildVariant
def getPackageName(variant) {
    def suffix = variant.buildType.packageNameSuffix
    def packageName = variant.productFlavors.get(0).packageName
    if (suffix != null && !suffix.isEmpty() && suffix != "null") {
        packageName += suffix
    }
    return packageName
}