版权声明:本文为博主原创文章,未经博主允许不得转载。 - long for us https://blog.csdn.net/longforus/article/details/80598138
error
错误信息:
A problem occurred configuring project ':app'.
> Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=release, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
原因
为了能更好的区分构建出的apk版本,有的人按照网上的方法在gradle中自定义了apk输出名称.在以前是没有问题的,但是在gradle3.+的版本以后构建会报这个错误.我也帮人看过这个问题.因为当时我没使用这个功能,也没有深究.现在略闲我也按照教程添加了这个功能,也遇到了一样的问题.通过分析android gradle plugin的源码,找到了解决方法.
解决方法
不修改outputFile,懒得细讲,看代码吧
applicationVariants.all {
def buildType = it.buildType.name
if(buildType=='release'){
it.getPackageApplication().outputDirectory = new File(rootProject.rootDir,"releaseApk")
it.outputs.each { out ->
def outputFile = out.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName =appid+"${new Date().format("yyyy-MM-dd-HH-mm-ss", TimeZone.getTimeZone("GMT+08:00"))}Release${android.defaultConfig.versionName}.apk"
// out.outputFile = new File(outputFile.parentFile,fileName) //这里报错
out.apkData.outputFileName = fileName
}
}
}
}
这个过程中还遇到一个问题就是文件名中的中文乱码,通过修改gradle编码,new String(string,”utf-8”)都没有解决.望知道的仁兄指导.