Search code examples
androidgradleandroid-gradle-pluginbuild.gradle

Gradle 6.5 AS 4.1 Setting outputFileName got The value for this property cannot be changed any further error


I have a gradle task that is called when I do release apk, I call it in this way:

tasks.whenTaskAdded { task ->
    if (task.name == 'preDeployBuild') {
        task.dependsOn deployTask
    }
}

Task it self is:

task deployTask {
    doFirst {
        versionCode = versionJSON.buildNumber
        latestVersionCode = versionCode
        println "$versionCode"
        versionFile.write(new JsonBuilder(versionJSON).toPrettyString())
        def versionNameRelease = getVersionNameRelease()
        ext.latestVersionName = versionNameRelease
        android.applicationVariants.all { variant ->
            variant.outputs.all {
                println "$versionNameRelease"
                println "$versionCode"
                versionNameOverride = versionNameRelease
                versionCodeOverride = versionCode
                outputFileName = "${appName}_${latestVersionName}.${new Date().format('yyyyMMdd')}.apk"
            }
        }
    }
}

I updated to Gradle 6.5 and this not work any more, got this error:

> The value for this property cannot be changed any further.

Which is the correct way to update versioName versionCode and outputFileName during gradle task running?


Solution

  • I have resolved in this way

    Make task a function and call inside buildTypes block:

        releaseMajor {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                def startTask =  project.gradle.startParameter.taskNames[0]
                if(startTask == ':app:assembleReleaseMajor') {
                    increaseMajor()
                    relaseTask()
                }
            }
    

    and in function relaseTask do stuff:

    def relaseTask() {
    ....
        android.applicationVariants.all { variant ->
            variant.outputs.all {
                println "$versionNameRelease"
                println "$versionCode"
                versionNameOverride = versionNameRelease
                versionCodeOverride = versionCode
                outputFileName = "${appName}_${latestVersionName}.${new Date().format('yyyyMMdd')}.apk"
            }
        }
    }