天天看點

Cannot set the value of read-only property ‘outputFile‘ for ApkVariantOutputImpl_Decorated{apkData=M

android studio更新到3.1.4之後gradle裡的很多配置也相應發生了一些改變。在打包的時候我就遇到了這樣的問題。

報錯為: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.

截圖如下:

大家可以注意看一下,AS更新到3.0以上版本後,截圖上的紅框處的代碼都要改動,否則是無法正常打包的。那要改成什麼樣呢,如下圖所示:

改成上圖所示的樣子就可以正常打包了,另外我也把代碼貼出來友善大家複制。

 //打包後應用名稱

applicationVariants.all { variant ->
         variant.outputs.all { output ->
                def outputFile = output.outputFile
                def fileName
                if (outputFile != null && outputFile.name.endsWith('.apk')) {
                    if (variant.buildType.name.equals('release')) {//如果是release包
                        fileName = "anjian_release_v${defaultConfig.versionName}.apk"
                    } else if (variant.buildType.name.equals('debug')) {//如果是debug包
                        fileName = "anjian_debug_v${defaultConfig.versionName}.apk"
                    }
                    outputFileName = fileName
                }
            }
        }
           

本文轉載自:https://blog.csdn.net/qq_36317441/article/details/81625936