Android Studio:Cannot set the value of read-only property 'outputFile'

原文:http://blog.csdn.net/uu00soldier/article/details/78440953

今天升级了Android Studio 3.0以后,在项目编译的时候发现Gradle中报错了,错误如下:

Error:(60, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=xiaomiRelease, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
<a href="openFile:E:\Studio\MyApplication\CodeBook\build.gradle">Open File</a>

经过一番折腾,网上找大牛的解读,弄明白了output.outputFile变成了只读属性,不能再往里面写东西了,以下是3.0之前的配置:

applicationVariants.all { variant ->    //批量修改Apk名字
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.apk') && 'release'.equals(variant.buildType.name)) {
            def fileName = outputFile.name.replace("${variant.flavorName}", "V${defaultConfig.versionName}-${variant.flavorName}")
            fileName = fileName.replace('.apk', "-${buildTime()}.apk")
            output.outputFile = new File(outputFile.parent, fileName)
        }
    }
}

下面是经过修改之后3.0里面批量修改APK名字的配置:

applicationVariants.all { variant ->    //批量修改Apk名字
    variant.outputs.all { output ->
        if (!variant.buildType.isDebuggable()) {
            //获取签名的名字 variant.signingConfig.name
            //要被替换的源字符串
            def sourceFile = "-${variant.flavorName}-${variant.buildType.name}"
            //替换的字符串
            def replaceFile = "_V${variant.versionName}_${variant.flavorName}_${variant.buildType.name}_${buildTime()}"
            outputFileName = output.outputFile.name.replace(sourceFile, replaceFile);
            //遗留问题:如何获取当前module的name,如CodeBooke这个名字怎么获取到
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容