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

时光总嘲笑我的痴心妄想 提交于 2019-11-30 03:06:34

Use output.outputFileName instead of output.outputFile

Solution for Studio 3.0+ and Gradle 3.0+

To change the name of the APK in Android

applicationVariants.all { variant ->
   variant.outputs.each { output ->
      output.outputFileName = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", "Your_name.apk"))
   }
}  

For Gradle 3.4 +

android {
   ......
   applicationVariants.all { variant ->
       variant.outputs.all {
           def flavor = variant.name
           def versionName = variant.versionName
           outputFileName = "prefix_${flavor}_${versionName}.apk"
       }
   }
}

The result would be like this,

prefix_release_1.0.1.apk

Try this code :

buildTypes {

       applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def name = "myapp_v${variant.versionName}(${variant.versionCode}).apk"
                output.outputFileName = name
            }
        }

    }

In or After gradle 3.1.0

try below code

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = new File(
                    "release_build", // here you can change the name
                    output.outputFile.name)
        }
}

In my case I resolved it by just refusing to update to Gradle 4.4 (in my case).So when Studio asks (when you open your project for the first time) you to update Gradle to support instant run,etc just simply refuse and you should be fine.

change your code into:-

    applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(output.outputFile.parent, "${variant.applicationId}-${variant.versionName}.apk")
    }
}

What worked for me was to 1. change each to "all" 2. change output.outputFile to "outputFileName" 3. Run $./gradlew clean build in terminal

For example, if your artifacts.gradle settings were this:

android.applicationVariants.all { variant ->

variant.outputs.each { output ->
    def finalVersionCode = 10000 + versionCode
    output.versionCodeOverride = finalVersionCode
    output.outputFile = new File(
         output.outputFile.parent,       output.outputFile.name.replace(".apk","-${finalVersion}.apk"))
}

}

Then you would want to change it to this:

android.applicationVariants.all { variant ->

variant.outputs.all { output ->
    def finalVersionCode = 10000 + versionCode
    output.versionCodeOverride = finalVersionCode
    outputFileName = new File(
         output.outputFile.parent,
         outputFileName.replace(".apk", "-${finalVersionCode}.apk"))
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!