且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Gradle实验性插件中的任务访问versionCode

更新时间:2022-06-21 01:38:35

创建方法在你的 build.gradle

def getVersionCode = { ->
    // calculate versionCode code or use hardcoded value
    return 1
}

defaultConfig 中使用此方法:
$ b

Use this method in defaultConfig:

android {
    defaultConfig {
        versionCode getVersionCode()
    }
}

在您的自定义任务中使用相同的方法:

Use the same method in your custom task:

task printVersions{
    print "Version code is " + getVersionCode()
}

替代解决方案是访问 versionCode 属性:

Alternative solution is to access versionCode property directly:

task printVersions{
    print "Version code is $project.android.defaultConfig.versionCode"
}