Gradle 6.5 AS 4.1 设置 outputFileName got The value for this 属性 cannot be changed any further error

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

我有一个 gradle 任务,当我发布 apk 时调用它,我这样调用它:

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

任务本身是:

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"
            }
        }
    }
}

我已更新至 Gradle 6.5,但此版本不再有效,出现此错误:

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

在 gradle 任务 运行 期间更新 versioName versionCode 和 outputFileName 的正确方法是什么?

有同样的问题

      def fileNameCreate = { variant ->
            variant.outputs.all { output ->
                outputFileName = "epromo-${variant.versionName}(${variant.versionCode})-${variant.baseName}.apk"
            }
        }

outputFileName - Gradle 错误:无法进一步更改此 属性 的值。

我已经这样解决了

使任务成为一个函数并在 buildTypes 块中调用:

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

并在函数 relaseTask 中做一些事情:

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"
        }
    }
}