Android build with Gradle Release plugin

断了今生、忘了曾经 提交于 2020-01-11 04:17:28

问题


I want to use Gradle Release Plugin in Android Project.

I configured it and everything is okay, except one thing:

Gradle Release Plugin changes project version in gradle.properties file in one of its tasks, but this change does not affect Android Project's versionName, because as I found with using println() gradle initialising it before running any tasks.

Is there a way to change Android Project versionName in some gradle task after it was initialised?

Here is part of my build.gradle

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 213
        versionName version // to change it, please visit gradle.properties file
        println('Project version: ' + version) // this line executed BEFORE any gradle task :(
    }

    ...
}

I can see only one solution: different gradle executions

  1. Will change version in gradle.properties
  2. Will execute build process, but this is bad workaround especially for Gradle Release Plugin

回答1:


Since version 2.1.0 the gradle-release plugin is executing the build in a separate process. This ensures the version is correct throughout the build process and for all other plugins which rely on the version being set from the beginning.

2.1.0

...

COMMON: Refactored the build process during release to run in separate process. This will hopefully resolve all the issues with certain other plugins like the new maven-publish or the bintray plugin.

COMMON: Added beforeReleaseBuild and afterReleaseBuild hook, which both run in the same process as the build itself.

...

see full changelog




回答2:


Insert this functions in your build.gradle

task('increaseVersionCode') << {
    def manifestFile = file("src/main/AndroidManifest.xml")
    def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
    def manifestText = manifestFile.getText()
    def matcher = pattern.matcher(manifestText)
    matcher.find()
    def versionCode = Integer.parseInt(matcher.group(1))
    def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"")
    manifestFile.write(manifestContent)
}

task('incrementVersionName') << {
    def manifestFile = file("src/main/AndroidManifest.xml")
    def patternVersionNumber = Pattern.compile("versionName=\"(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\"")
    def manifestText = manifestFile.getText()
    def matcherVersionNumber = patternVersionNumber.matcher(manifestText)
    matcherVersionNumber.find()
    def majorVersion = Integer.parseInt(matcherVersionNumber.group(1))
    def minorVersion = Integer.parseInt(matcherVersionNumber.group(2))
    def pointVersion = Integer.parseInt(matcherVersionNumber.group(3))
    def buildVersion = Integer.parseInt(matcherVersionNumber.group(4))
    def mNextVersionName = majorVersion + "." + minorVersion + "." + pointVersion + "." + (buildVersion + 1)
    def manifestContent = matcherVersionNumber.replaceAll("versionName=\"" + mNextVersionName + "\"")
    manifestFile.write(manifestContent)
}


tasks.whenTaskAdded { task ->
    if (task.name == 'generateReleaseBuildConfig') {
        task.dependsOn 'increaseVersionCode'
        task.dependsOn 'incrementVersionName'
    }
}

Disclaimer: I've created this gist.

The complete gist. https://gist.github.com/ascariandrea/9991499




回答3:


Here is a workaround I used which splits the execution of tasks into multiple gradle executions. It works for svn but can be adapted:

https://stackoverflow.com/a/26659974/256770



来源:https://stackoverflow.com/questions/24818755/android-build-with-gradle-release-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!