android - 覆盖 flavor 和构建类型之外的应用程序 ID

标签 android gradle

我有一个应用程序有多种风格(A,B,C)和两种构建类型(debug,release)

在构建类型 debug 中,我向应用程序 ID 添加一个后缀,如下所示:

debug {
    applicationIdSuffix '.debug'
}

这对风格 AB 没问题,但我不能将 .debug 附加到风格 C的应用程序 ID。

我已经考虑过覆盖变体,就像我对 versionCode 所做的那样,但没有成功。

    applicationVariants.all { variant ->
        def changedVersionCode = variant.versionCode
        variant.
        variant.outputs.each { output ->
            if (variant.buildType.name != "debug") {
                output.setVersionCodeOverride(project.ext.versionCode)
                changedVersionCode = project.ext.versionCode
            } 
        }
        changeApkFileName(variant,changedVersionCode)
    }

是否可以根据风格覆盖变体应用程序 ID。例如,我的计划是做这样的事情:

variant.buildType.name.replace('.debug','')

最佳答案

Is it possible to override a variants application ID depending on the flavor

是的,这是可能的。

您看不到预期 ID 的原因是:

Because Gradle applies the build type configuration after the product flavor, the application ID for the "C" build variant will be "<your-applicaion-id>.debug".

因此,如果您希望它因不同的口味而有所不同,那么您必须隔离 applicationIdSuffix对于不同的口味并将其从 debug {} 中删除如下:

android {
    defaultConfig {
        applicationId "<your-application-id>"
    }
    productFlavors {
        A {
            applicationIdSuffix ".debug"
        }
        B {
            applicationIdSuffix ".debug"
        }
        C {
            applicationIdSuffix ""
        }
    }
}

更多详情引用官方documentation .

关于android - 覆盖 flavor 和构建类型之外的应用程序 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019932/

相关文章:

android - 如何旋转 GamesClient 的弹出窗口

Android 对底层 API 的涟漪效应

android - 当 Activity 在前台时接收明确的 Intent

java - Android Studio 上 Kotlin(非 Android)项目的 ClassNotFoundException 错误

gradle - 从库中获取 gradle 变量

android - IMEI码/01说明

android - 操作栏中的 ShareActionProvider 图标

java - Gradle 脱离 Bash for 循环

build - Gradle:如何为需要相同 Activity 但具有不同意图过滤器的不同 buildType 合并 Android list 文件

java - 如何解决 OutOfBounds BuildTime 错误 - api 28