【问题标题】:Use different VersionCode for Debug/Release android gradle build使用不同的版本代码进行调试/发布 android gradle build
【发布时间】:2015-02-24 08:27:59
【问题描述】:

我想应用 不同的 VersionCode 来制作 apk 文件。 仅用于调试将其修复为1,并用于发布 defaultConfig 中指定的任何数字。

下面的代码将 mypackage-release-1.apk 文件作为 assembleRelease 工件,这不是预期的。我期待mypackage-release-10111.apk

为什么debug { defaultConfig.versionCode=1 } 行会影响 assembleRelease 工件?

defaultConfig {
    versionCode 10111
    versionName '2.5.4'
    minSdkVersion 10
    targetSdkVersion 21
}
signingConfigs {
    debug {
        project.ext.loadSign = false
        defaultConfig.versionCode = 1 // Why this value applied to assembleRelease?
    }
    release {
        project.ext.loadSign = true
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def file = output.outputFile
                output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionCode + ".apk"))
            }
        }
    }
}
buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        signingConfig signingConfigs.release
    }
}

【问题讨论】:

    标签: android android-gradle-plugin build.gradle


    【解决方案1】:

    晚会...

    在执行任何任务之前评估整个 gradle 文件,因此您在声明 debug 配置时基本上是在更改默认的 versionCode。没有直接的方法可以从 buildType 重置 versionCode,但另一个答案上的链接通过在构建变体上声明任务来解决问题。

    android {
        ...
        defaultConfig {
             ...
        }
        buildTypes {
             ...
        }
        applicationVariants.all { variant ->
            def flavor = variant.mergedFlavor
            def versionCode = flavor.versionCode
            if (variant.buildType.isDebuggable()) {
                versionCode += 1
            }
            flavor.versionCode = versionCode
        }
    }
    

    【讨论】:

    • 遗憾的是,它在所有配置/执行阶段分离的 AGP 3+ 中不再起作用。也可以用它来签署配置。
    【解决方案2】:

    这是一个更新的版本:

    android {
      defaultConfig { ... }
    
      applicationVariants.all { variant ->
        if (variant.name == 'debug') {
          variant.outputs.each { output ->
            output.versionCodeOverride = 1
          }
        }
      }
    }
    

    【讨论】:

    • 有没有办法覆盖 VersionName?
    • @serv-inc 不确定,如果您找到了覆盖versionName 的解决方案,但如果您没有找到,则需要使用:output.versionNameOverride = "1.0.1-mySpecialVersionName"
    【解决方案3】:

    我也是,但我认为 defaultConfig.versionCode 是在 build.gradle 编译时设置的。它是全局静态变量,在编译时分配,而不是运行时分配。

    我认为我们可以拦截 gradle 任务执行,并在运行时修改defaultConfig.versionCode


    在 goooooooogle 之后,我发现这个对我有用:https://gist.github.com/keyboardsurfer/a6a5bcf2b62f9aa41ae2

    【讨论】:

      【解决方案4】:

      与香精一起使用:

      applicationVariants.all { variant ->
          def flavor = variant.mergedFlavor
          def name = flavor.getVersionName()
          def code = flavor.getVersionCode()
      
          if (variant.buildType.isDebuggable()) {
              name += '-d'
              code = 1
          }
      
          variant.outputs.each { output ->
              output.versionNameOverride = name
              output.versionCodeOverride = code
          }
      }
      

      【讨论】:

        【解决方案5】:

        最简单的解决方案是将 versionCode 和 versionName 变量分别从 defaultConfig 移动到 debug 和 release。

        android {
            ...
            defaultConfig {
                 // without versionCode and versionName
                 ...
            }
            buildTypes {
                debug {
                    defaultConfig.versionCode X
                    defaultConfig.versionName 'X.Y.Z'
                }
                release {
                    defaultConfig.versionCode A
                    defaultConfig.versionName 'A.B.C'
                }
            }
            ...
        }
        

        【讨论】:

          【解决方案6】:
          applicationVariants.all { variant ->
              variant.outputs.each { output ->
                  if (variant.buildType.isDebuggable()) {
                      output.versionCodeOverride = 26
                      output.versionNameOverride = "2.2.6"
                  }
              }
          }
          

          放到安卓中{}

          【讨论】:

            【解决方案7】:

            所以最近我不得不处理相同的场景,并且我能找到的所有示例都使用了 applicationVariants 属性,该属性在 imo 中记录不充分。

            所以在对源代码进行了一些挖掘之后,我意识到最终来自 ProductFlavorversionCodeversionName 属性被合并到 AndroidManifest 中,这让我想到:我们不能只通过我们自己,因为我们在 ProductFlavor 和 BuildType DSL 对象上有 manifestPlaceholders 属性,所以我想出了这个 - 不要犹豫,提供反馈并告诉我为什么它是错误的

            build.gradle(app)

            android {
                ...
                buildTypes {
                    debug {
                        manifestPlaceholder = [versionCode: X, versionName: "X.Y.Z"]
                    }
                    release {
                        manifestPlaceholder = [versionCode: A, versionName: "A.B.C"]
                    }
                }
                ...
            }
            

            AndroidManifest.xml

            <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                package="..."
                android:versionCode="${versionCode}"
                android:versionName="${versionName}">
                ...
            </manifest>
            

            【讨论】:

              猜你喜欢
              • 2017-05-28
              • 2012-03-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-05
              • 2018-09-08
              • 1970-01-01
              相关资源
              最近更新 更多