简体   繁体   English

带有Gradle Release插件的Android构建

[英]Android build with Gradle Release plugin

I want to use Gradle Release Plugin in Android Project . 我想在Android Project使用Gradle Release Plugin

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. Gradle Release Plugin在其任务之一中更改了gradle.properties文件中的项目版本,但是此更改不会影响Android Project's versionName ,因为正如我在运行任何任务之前使用println() gradle对其进行初始化所发现的那样。

Is there a way to change Android Project versionName in some gradle task after it was initialised? 初始化后,是否可以在某些gradle任务中更改Android Project versionName

Here is part of my build.gradle 这是我的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 我只能看到一个解决方案:不同的Gradle执行

  1. Will change version in gradle.properties 将更改gradle.properties中的版本
  2. Will execute build process, but this is bad workaround especially for Gradle Release Plugin 将执行构建过程,但这是不好的解决方法,尤其是对于Gradle Release Plugin

Since version 2.1.0 the gradle-release plugin is executing the build in a separate process. 从版本2.1.0开始, gradle-release插件将在单独的过程中执行构建。 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 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. 这有望解决某些其他插件(如新的maven-publish或bintray插件)中的所有问题。

COMMON: Added beforeReleaseBuild and afterReleaseBuild hook, which both run in the same process as the build itself. 常见:添加了beforeReleaseBuild和afterReleaseBuild挂钩,它们都与构建本身在同一进程中运行。

... ...

see full changelog 查看完整的变更日志

Insert this functions in your build.gradle 将此函数插入​​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 https://gist.github.com/ascariandrea/9991499

Here is a workaround I used which splits the execution of tasks into multiple gradle executions. 这是我使用的一种变通方法,它将任务的执行分为多个gradle执行。 It works for svn but can be adapted: 它适用于svn,但可以进行调整:

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

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

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