Failed to change apk file name in gradle using gradle: 3.0.0-alpha4

I am trying to use the Android build tools "com.android.tools.build: gradle: 3.0.0-alpha4" in my project. In my build script, I will rename the apk output, which worked fine in the past, but seems to be no longer supported.

android {

   productFlavors {
        flavorUnsigned {
            applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    output.outputFile = new File(
                            output.outputFile.parent,
                            output.outputFile.name.replace("app-flavorUnsigned-release-unsigned.apk", "DemoApp-${variant.versionName}($variant.versionCode).apk"))
                    def mappingFile = "${rootDir}/app/build/outputs/mapping/${getCurrentFlavor()}/release/mapping.txt"
                    if (variant.getBuildType().isMinifyEnabled()) {
                        variant.assemble.doLast {
                            copy {
                                from "${mappingFile}"
                                into "${rootDir}/app/build/outputs/apk"
                            }
                        }
                    }
                }
            }
        }

    }
}

But now I get this error when creating my project

Error:Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=flavorUnsignedDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
+6
source share
2 answers

If you want to transfer your project to an Android plugin 3.0.0-alpha1or higher, you should do the following: Change the API in the output variant:

// If you use each() to iterate through the variant objects,
// you need to start using all(). That because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

, , .

+20

,

Use output.outputFileName instead of output.outputFile

...

0

All Articles