安卓自定义输出apk名报Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decor..

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”)都没有解决.望知道的仁兄指导.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值