使用不同的VersionCode进行调试/释放android gradle构建

You*_*jae 14 android build.gradle android-gradle-plugin

我想应用不同的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
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 11

最简单的解决方案是将 versionCode 和 versionName 变量从 defaultConfig 分别移至调试和发布。

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'
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)


fis*_*ees 7

我也是,但我认为defaultConfig.versionCodebuild.gradle编译时已设定.它是全局静态变量,在编译时分配,而不是运行时.

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


goooooooogle之后,我发现这个适用于我:https://gist.github.com/keyboardsurfer/a6a5bcf2b62f9aa41ae2


Fil*_*dio 7

晚会......

在任何任务执行之前评估整个gradle文件,因此您versionCode在声明debug配置时基本上更改了默认值.有重置没有直接的方法versionCodebuildType,但对方的回答链接通过声明对构建变种任务做的伎俩.

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


Ego*_*uba 5

这是一个更新的版本:

android {
  defaultConfig { ... }

  applicationVariants.all { variant ->
    if (variant.name == 'debug') {
      variant.outputs.each { output ->
        output.versionCodeOverride = 1
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • @serv-inc 不确定,如果您找到了覆盖 `versionName` 的解决方案,但如果没有,则需要使用: `output.versionNameOverride = "1.0.1-mySpecialVersionName"`。 (2认同)

小智 5

与风味一起使用:

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
    }
}
Run Code Online (Sandbox Code Playgroud)