Set Android versionCode after Gradle task complete

2019-05-30 14:33发布

I have a task that updates my app's version code, called changeVersionCode. This task runs before my Android build tasks (assembleRelease), but obviously it happens after the android { } closure. This seems to mean that versionCode is set and cannot be changed even when changeVersionCode runs.

Here's a simplified build script that demonstrates how I have tried to approach this problem:

// .version is a file that starts at "1" the first time I call this

def loadVersionCode() {
    // fetch version code from a file, '.version'
    return loadVersionCodeFromFile('.version')
}

def incrementVersionCode() {
    // fetch version code, update it, and save it back to a file
    def newVersion = loadVersionCode() + 1
    saveVersionCodeToFile('.version', newVersion)
}

apply plugin: 'com.android.application'

android {
    // ... snip ...
    defaultConfig {
        // Set the version code to contents of .version (eg. 1)
        versionCode loadVersionCode()
        // ...
    }
}

task incrementVersionCode << {
    println "Old version code: " + android.defaultConfig.versionCode // prints 1

    incrementVersionCode()
    def newVersion = loadVersion() // this now returns 2

    android.defaultConfig.versionCode = loadVersionCode()
    // Also tried:
    // android.defaultConfig.versionCode loadVersionCode()

    println "New version code: " + android.defaultConfig.versionCode // prints 2
    // android.defaultConfig.versionCode is now 2, but APK still has version 1 (until next time I run gradle)
}

Then:

# Build an APK with versionCode 1
$ ./gradlew assembleRelease

# This seems to change versionCode to 2, but still builds an APK with versionCode 1
#
# Prints:
#     Old version code: 1
#     New version code: 2
$ ./gradlew incrementVersionCode assembleRelease

I am using:

  • Gradle 2.5
  • Groovy 2.3.10
  • Ant 1.9.3
  • Java 1.8.0_45
  • Mac OS X 10.10.5
  • Android build tools 22.0.1

Is there any way I can change my version code from a task before invoking Android build tasks?

1条回答
该账号已被封号
2楼-- · 2019-05-30 15:01

How to configure versionCode before a task is launched

You can use the DSL tasks.whenTaskAdded. You can read the official doc, chapter 58.6.2. Task creation.

You can receive a notification immediately after a task is added to a project. This can be used to set some default values or add behaviour before the task is made available in the build file.

You can define a task:

task incrementVersionCode << {
    //do something
}

Then define the dependency :

tasks.whenTaskAdded { task ->
    if (task.name == 'xxxxx') {
        task.dependsOn incrementVersionCode 
    }
}

In your case you can do somenthing like this:

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

How to configure versionCode with a function

In the top-level file you can configure a function like this:

ext {

     buildVersionCode = {
       //...
    }
}

In your module/build.gradle you can do somehing like this:

defaultConfig {
    versionCode buildVersionCode()
    //....
}

Otherwise you can do in your build.gradle something like:

defaultConfig {
    //...
    versionCode getMyNumber()
}


def getMyNumber() {
    return //.... ;
} 
查看更多
登录 后发表回答