失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > android 自定义 打包文件类型 Android设置apk打包文件名报错

android 自定义 打包文件类型 Android设置apk打包文件名报错

时间:2023-08-03 04:52:54

相关推荐

android 自定义 打包文件类型 Android设置apk打包文件名报错

前言

在 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`以后做出的改变,大家需要将相应代码做出改变,截图对比如下:

如果觉得《android 自定义 打包文件类型 Android设置apk打包文件名报错》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。