How to resoleve Cannot set the value of read-only property 'outputFile'

更新Android Studio 3.0之后,出现Cannot set the value of read-only property ‘outputFile’报错,也就是用来打包后重命名的gradle出现了问题
旧版本

AS 2.3时用的代码:

applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName = "driver_${variant.productFlavors[0].name}_v${defaultConfig.versionName}.apk"
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }

修改之后:

    flavorDimensions 'api'
    productFlavors {
        ...
    }
    android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "driver_${variant.productFlavors[0].name}_v${variant.versionName}.apk"
        }
    }

关于flavorDimensions可查看官方文档

https://developer.android.google.cn/studio/build/build-variants.htm

参考资料

答案来源:https://stackoverflow.com/questions/44239235/android-gradle-3-0-0-alpha2-plugin-cannot-set-the-value-of-read-only-property

官方文档解析:https://developer.android.google.cn/studio/build/gradle-plugin-3-0-0-migration.html#variant_api

你可能感兴趣的:(How to resoleve Cannot set the value of read-only property 'outputFile')