Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

本文详细介绍了在Android Studio中遇到的“Cannot set the value of read-only property 'outputFile'”错误,并提供了针对不同Gradle版本的解决方案。在旧版本的Gradle中,应使用output.outputFile设置APK输出路径;而在新版本中,则应使用output.outputFileName。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

平台:AndroidStudio
错误提示:Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

错误时代码

    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            def fileName
            def projectName = "Client"
            def tabCount = 4
            def buildType = variant.buildType.name
            def tempTime = new Date().format("yyyyMMddHHmm")
            def versionName = defaultConfig.versionName
            def suffix = applicationId.contains("X") ? "X" : ""

            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                fileName = projectName + "_" + buildType + "_" + tabCount + "_" +
                        versionName + "_" + tempTime + suffix + ".apk"
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }

正确时代码

    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            def fileName
            def projectName = "Client"
            def tabCount = 4
            def buildType = variant.buildType.name
            def tempTime = new Date().format("yyyyMMddHHmm")
            def versionName = defaultConfig.versionName
            def suffix = applicationId.contains("X") ? "X" : ""

            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                fileName = projectName + "_" + buildType + "_" + tabCount + "_" +
                        versionName + "_" + tempTime + suffix + ".apk"
                output.outputFileName = fileName
            }
        }
    }

旧版本的Gradle, output.outputFile接收File类型。
而新版本的Gradle, 要使用output.outputFileName,接收String类型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值