Error:(93, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl

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

Error:(93, 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.

Open File

经过一番折腾,网上找大牛的解读,弄明白了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)

        }

    }

}

我们需要把 each() 和 output.outputFile 方法修改为 all() 和 outputFileName

下面是经过修改之后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);

            

        }

    }

}

你可能感兴趣的:(技术,Android,studio3.0,更新,AS,3.0更新问题,编译报错)