本文介绍了使用不同的VersionCode进行Debug/Release Android Gradle构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to apply different VersionCode to make apk file.For debug only fix it to 1, and for release whatever number specified in defaultConfig.

下面的代码将mypackage-release-1.apk文件作为 assembleRelease 工件提供,这是不期望的.我为此期望mypackage-release-10111.apk.

Below code gives mypackage-release-1.apk file as assembleRelease artifact, which is not expected. I expected mypackage-release-10111.apk for that.

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

why the line debug { defaultConfig.versionCode=1 } affects assembleRelease artifact?

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

推荐答案

聚会晚会...

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

The entire gradle file evaluated before any task execution, so you are basically changing the default versionCode while declaring debug configs. There is no direct way to reset versionCode from buildType, but the link on the other answer do the trick by declaring a task on build variants.

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

这篇关于使用不同的VersionCode进行Debug/Release Android Gradle构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 22:46