关于build.gradle和自定义任务的问题

3npbholx  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(167)

In order to manage my build pipeline for Android app, I have added a task and custom functions in build.gradle to I can trigger an increment of the build version.
For now, I have this file version.properties in the root project dir and I have added at the end of the build.gradle in the project dir, the below code:
Version.properties

major = 0
minor = 4
patch = 0
build = 1

build.gradle

def readVersion() {
    def versionFile = new File(project.rootDir, 'version.properties')
    def version = new Properties()
    def stream

    try {
        stream = new FileInputStream(versionFile)
        version.load(stream)
    } catch (FileNotFoundException ignore) {
    } finally {
        if (stream != null) stream.close()
    }

    if(!version['major']) version ['major'] = "0"
    if(!version['minor']) version ['minor'] = "0"
    if(!version['patch']) version ['patch'] = "0"
    if(!version['build']) version ['build'] = "0"

    return version
}

def getVersionCode() {
    def version = readVersion()
    def major = version['major']
    def minor = version['minor']
    def patch = version['patch']
    def build = version['build']

    return major * 1000000 + minor * 10000 + patch * 100 + build
}

def getVersionName() {
    def version = readVersion()
    return "v${version['major']}" +
            ".${version['minor']}" +
            ".${version['patch']}" +
            "-rc${version['build']}"
}

def incrementBuildNumber() {
    def versionFile = new File(project.rootDir, 'version.properties')
    def version = new Properties()

    def build = version['build']
    build = build+1
    version['build'] = build.toString

    def stream = new FileOutputStream(versionFile)
    try {
        version.store(stream, null)
    } finally {
        if (stream != null) stream.close()
    }
    //return build
}

task doBuildNumberIncrement() {
    incrementBuildNumber()
}

in the same build.gradle, I have:

ext {

    //Version
    versionCode = getVersionCode()
    versionName = getVersionName()

and I have added in the Fastfile the lane:

desc "Build and release on firebase"
    lane :build_and_release_to_firebase do
        increment_version()
        develop_build(flavor: "MyFlavor")

   desc "Manage build version"
    lane :increment_version do
      gradle(task: "doBuildNumberIncrement")
    end

When I running locally the command with the cmd line fastlane build_and_release_to_firebase I am having the error below:

* Where:
Build file '/MyProject/build.gradle' line: 155

the line 155 is when I do build = build+1 in the incrementBuildNumber
Any idea why ? I try builld++ but same issue.
Thanks

tsm1rwdh

tsm1rwdh1#

因为version对象为空,因此您将在155处获得NullPointerException(null+1)。
您可以调用之前编写的readVersion()函数来创建版本对象(或使用versionFile从FileInputStream加载)
解决方案如下

def incrementBuildNumber() {
    
        def versionFile = new File(project.rootDir, 'version.properties')
        def version = readVersion()
    
        def build = version['build']
        build = build + 1
        version['build'] = build.toString()
    
        def stream = new FileOutputStream(versionFile)
        try {
            version.store(stream, null)
        } finally {
            if (stream != null) stream.close()
        }
        //return build
    }

相关问题