天天看點

Fix Gradle sync failed: Cannot set the value of read-only property 'outputFile' for.....

在更新android studio到3.0.X版本時,出現同步錯誤。

Gradle sync failed: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
		Consult IDE log for more details (Help | Show Log) (1m 37s 807ms)
           

這是由于gradle更新至4.4版本後,對部分API進行了調整的緣故。

 each() 和 outputFile() 方法分别修改為all() 和 outputFileName。

在原有gradle檔案中,

variant.outputs.each { output ->
            output.outputFile = new File(output.outputFile.parent, "APPName" + "-" + buildType.name + "-v" +
                    defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk");
        }
    }
           

修改後的相應内容為

android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "cep" + "-" + buildType.name + "-" + releaseTime() + "-v" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk";
        }
    }