1. 程式人生 > >Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

升級AS3.0之後很多根據不同情況打包的apk名字就會遇到這個情況,這個錯誤度娘一下會有很多解釋,你要是檢視幾篇之後也能解決,由於搜尋之後倍感效率低下,就在這裡簡單修改一下,爭取一步到位。

大部分的部落格是這樣寫的把each改為all,原因outputFile 是可讀的,本質上沒錯,但是沒有一個具體的描述,原來的each怎麼用呢,基本沒有寫的特別明白的,我自己的例子:

applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def date = new Date()
            def formattedDate = date.format('yyyyMMdd')
            def fileName
            if (variant.buildType.name == "release") {
                // 輸出apk名稱為app_100_release.apk
                fileName = "xxx_V${defaultConfig.versionName}.apk"
            } else if (variant.buildType.name == "debug") {
                // 輸出apk名稱為app_100_debug.apk
                fileName = "xxx_debug_V${defaultConfig.versionName}_${formattedDate}.apk"
            } else {
                // 輸出apk名稱為app_v1.0.0_2017-12-11_101_beta.apk
                fileName = "xxx_Beta_V${defaultConfig.versionName}.apk"
            }
            output.outputFile = new File(output.outputFile.parent, fileName)
        }
    }

改完後是:
android.applicationVariants.all { variant ->
        variant.outputs.all {//這裡是all,下面是根據自己的情況來定製
            def date = new Date()
            def formattedDate = date.format('yyyyMMdd')
            def fileName
            if (variant.buildType.name == "release") {
                // 輸出apk名稱為app_100_release.apk
                fileName = "xxx_V${defaultConfig.versionName}.apk"
            } else if (variant.buildType.name == "debug") {
                // 輸出apk名稱為app_100_debug.apk
                fileName = "xxx_debug_V${defaultConfig.versionName}_${formattedDate}.apk"
            } else {
                // 輸出apk名稱為app_v1.0.0_2017-12-11_101_beta.apk
                fileName = "xxx_Beta_V${defaultConfig.versionName}.apk"
            }
            //outputFileName = "HuaxiaSign_${defaultConfig.versionName}_${formattedDate}.apk"
            //很多部落格只是寫了上面這句話,沒有具體賦值,自己來選擇賦值給outputFileName
            outputFileName = fileName
        }
    }