繁体   English   中英

使用不同的版本代码进行调试/发布 android gradle 构建

[英]Use different VersionCode for Debug/Release android gradle build

我想应用不同的 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
    }
}

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

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


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

晚会迟到...

在任何任务执行之前评估整个 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
    }
}

这是一个更新的版本:

android {
  defaultConfig { ... }

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

与风味一起使用:

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
    }
}

最简单的解决方案是将 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'
        }
    }
    ...
}
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        if (variant.buildType.isDebuggable()) {
            output.versionCodeOverride = 26
            output.versionNameOverride = "2.2.6"
        }
    }
}

将其放入 android{}

所以最近我不得不处理同样的场景,我能找到的所有例子都使用了applicationVariants属性,该属性在 imo 中没有得到很好的记录。

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

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM