当前位置: 首页 > 知识库问答 >
问题:

Android Gradle:在构建时动态更改版本名

邹学民
2023-03-14

我试图通过使用一个定制版本的gradle-release插件来模拟Android中的Maven release插件:https://github.com/townsfolk/gradle-release

    null

然而,生成的APK总是具有以前的版本(即1.0.0-Snapshot而不是1.0.0)。

版本号存储在gradle.properties中并正确更新,因此我假设我也需要更新数据模型中的版本以使更改生效。

我的android插件配置:

defaultConfig {
    versionCode versionCode as int  // taken from gradle.properties
    versionName versionName // taken from gradle.properties
    minSdkVersion 10
    targetSdkVersion 19
}
preBuild << {
    android.applicationVariants.each { variant ->
        variant.versionName = versionName
    }
}
preBuild << {
    android.buildTypes.each { type ->
        type.versionName = versionName
    }
}
preBuild << {
    android.productFlavors.each { flavor ->
        flavor.versionName = versionName
    }
}

我的另一种选择是在调用Gradle之前编写一个bash/bat脚本来逐步执行版本,这与使用Groovy改进构建定制的目的大相径庭。

如何在Android Gradle插件的执行阶段动态更新版本?

共有1个答案

陆翔飞
2023-03-14

这就是buildtypes的作用。您所描述的是发布构建,IMO。

下面是一个示例:当执行AssembleDebug时,它将给出一个快照构建,而执行AssembleRelease将给出一个干净的构建,没有任何后缀和递增的版本号。下一个调试生成也将使用递增的数字。

以下是在文件夹中创建文件时的完整功能构建。它也应该和口味一起工作,但这只是一个附带产品:).Gradle 2.2.1,Android插件1.1.3

apply plugin: 'com.android.application'
apply from: 'auto-version.gradle'

buildscript {
    repositories { jcenter() }
    dependencies { classpath 'com.android.tools.build:gradle:1.1.3' }
}

android {
    buildToolsVersion = "21.1.2"
    compileSdkVersion = "android-21"

    buildTypes {
        debug {
            versionNameSuffix "-SNAPSHOT"
        }
    }
}

println "config code: ${calculateVersionCode()}, name: ${calculateVersionName()}"
<manifest package="com.example" />
ext {
    versionFile = new File(project.rootDir, 'version.properties')
    calculateVersionName = {
        def version = readVersion()
        return "${version['major']}.${version['minor']}.${version['build']}"
    }
    calculateVersionCode = {
        def version = readVersion()
        def major = version['major'] as int // 1..∞
        def minor = version['minor'] as int // 0..99
        def build = version['build'] as int // 0..999
        return (major * 100 + minor) * 1000 + build
    }
}


Properties readVersion() {
    def version = new Properties()
    def stream
    try {
        stream = new FileInputStream(versionFile)
        version.load(stream)
    } catch (FileNotFoundException ignore) {
    } finally {
        if (stream != null) stream.close()
    }
    // safety defaults in case file is missing
    if(!version['major']) version['major'] = "1"
    if(!version['minor']) version['minor'] = "0"
    if(!version['build']) version['build'] = "0"
    return version
}

void incrementVersionNumber() {
    def version = readVersion()

    // careful with the types, culprits: "9"++ = ":", "9" + 1 = "91"
    def build = version['build'] as int
    build++
    version['build'] = build.toString()

    def stream = new FileOutputStream(versionFile)
    try {
        version.store(stream, null)
    } finally {
        stream.close()
    }
}

task incrementVersion {
    description "Increments build counter in ${versionFile}"
    doFirst {
        incrementVersionNumber()
    }
}

if (plugins.hasPlugin('android') || plugins.hasPlugin('android-library')) {
    android {
        defaultConfig {
            versionName = calculateVersionName()
            versionCode = calculateVersionCode()
        }

        afterEvaluate {
            def autoIncrementVariant = { variant ->
                if (variant.buildType.name == buildTypes.release.name) { // don't increment on debug builds
                    variant.preBuild.dependsOn incrementVersion
                    incrementVersion.doLast {
                        variant.mergedFlavor.versionName = calculateVersionName()
                        variant.mergedFlavor.versionCode = calculateVersionCode()
                    }
                }
            }
            if (plugins.hasPlugin('android')) {
                applicationVariants.all { variant -> autoIncrementVariant(variant) }
            }
            if (plugins.hasPlugin('android-library')) {
                libraryVariants.all { variant -> autoIncrementVariant(variant) }
            }
        }
    }
}

执行Gradle AssembleDebug以正常生成,Gradle AssemblereLease以递增和生成,Gradle IncrementVersion以仅递增。注意:使用Gradle Assemble时要小心,因为AssembleDebugAssembleRelease的顺序会产生不同的结果。

检查build目录中生成的文件,看看这些值是否符合您的要求。

gradle incrementVersion assembleFreeRelease assemblePaidRelease

(上面的手动执行是一个未经测试的想法。)

“检查未提交的更改”不包括在这个答案中,这是另一个游戏。如果我理解正确的话,您可以挂接到tasks.prebuild.doFirst{/*fail,如果未提交更改*/}。但这在很大程度上取决于您的版本控制。多问一个问题!

 类似资料:
  • 我想要这样的url baseURL/dealer/index.php?r=rest/packinfo 我开始犯错了 URL查询字符串“r=rest/{method}”不能有replace block。对于动态查询参数,请使用@query。

  • 我在Eclipse中有一个动态Web项目。使用带有Axis2的Tomcat7。在project facets中,我已经用Dynamic Web Module2.2配置了AXIS2(因为AXIS2不能使用Web Module3.0)。 但是当我尝试使用WebService wizard(new->Web Service)时,Eclipse尝试使用Web模块3.0创建Web服务,忽略origin项目的

  • 如您所见,其中没有。那么,当我的电脑上安装了27个版本时,为什么我的android工作室会寻找这个特定的版本(26)呢?另外,在哪里可以看到BuildToolsVersion“27.0.3”`?

  • 看起来好像什么都没有出问题,但我一定是错过了什么,因为我总是收到这个错误。 下面是my.settings/org.eclipse.wst.common.project.facet.core.xml的内容 在我试图压制这个看似不相关的错误消息时,任何帮助都非常感谢。 编辑:添加了pom.xml内容

  • 在我的项目中,有几个flyway迁移sql脚本文件。现在我的项目的数据库名称已经更改了,所以如果我要更改所有文件中的数据库名称,那么它将抛出这样的错误- 原因:javax.ejb.ejbException:org.flywaydb.core.api.flywayException:验证失败:迁移的迁移校验和不匹配2018.12.10.132421584->应用到数据库:988067673->本地解

  • 我想将嵌入式tomcat从8.5.6降级到8.5.51。 在我的日志中,我可以找到以下信息: 在哪里以及如何显式添加我想要的tomcat版本?