其他分享
首页 > 其他分享> > Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

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

作者:互联网

在做多渠道打包的时候出现了这个错误,在高版本的gradle出现。

 

具体错误为: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.
我的代码为下:

debug {
    //混淆开关
    minifyEnabled false
    //是否zip对齐
    zipAlignEnabled true
    //是否打开debuggable开关
    debuggable true
    //是否打开jniDebuggable开关
    jniDebuggable true
    // 移除无用的resource文件
    shrinkResources false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    //更改APK名称
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk') && 'debug'.equals(variant.buildType.name)) {
                def fileName = "novel_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }
}

 

这个在低版本的gradle是行得通的,但是高版本就不行。

高版本的应该这样:

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
            'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "novel_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
                        outputFileName = fileName
                    }
                }
            }
        }
    }

variant.outputs.each改为variant.outputs.all

output.outputFile = new File(outputFile.parent, fileName)改为outputFileName = fileName

标签:outputFile,name,read,variant,fileName,proguard,set,output
来源: https://blog.csdn.net/Dreamj1991/article/details/104791842