前言
在 app_module对应的build.gradle中给apk设置打包后的名称时报错了,下面就来讲讲这个问题。
今天涉及知识点:
问题点
解析
更多精彩内容,请关注微信公众号 "Android进击",大家一起来学习进步吧
Android进击.jpg
一. 问题点
我在 app_module对应的build.gradle中给apk设置打包后的名称, app_module对应的build.gradle相关代码如下:
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
//....
}
buildTypes {
release {
//其他代码省略
//......
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为 test_v1.0_-01-15_wandoujia.apk
def fileName = "test_${defaultConfig.versionName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
//....
}
结果output.outputFile = new File(outputFile.parent, fileName)这行代码报如下错误:
* What went wrong:
A problem occurred configuring project ':app'.
> groovy.lang.GroovyRuntimeException: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[], versionCode=1, versionName=1.0.0}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
二. 解析
这是因为gradle版本3.0`以后做出的改变,大家需要将相应代码做出改变,截图对比如下: