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

前言

在 app_module对应的build.gradle中给apk设置打包后的名称时报错了,下面就来讲讲这个问题。

今天涉及知识点:

问题点

解析

更多精彩内容,请关注微信公众号 "Android进击",大家一起来学习进步吧

43ac9bd4471a

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值