How do I add a version number to my APK files using 0.14+ versions of the Android Gradle plugin?

android, gradle

Solution

You need one more loop now that each variant can have multiple outputs:

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

Problem

I want to have the `versionName` included in the name of the output APK files from my Android build. There's an another answer that works with pre-0.14.x plugin versions, but they changed some of the data model so that doesn't work anymore, and I couldn't figure out how to fix it. As far as I can tell, the below chunk of code should work, but that last `set()` call appears to have no effect. It doesn't throw an error, but the value isn't replaced either. ``` buildTypes { applicationVariants.all { variant -> def oldFile = variant.outputs.outputFile.get(0) def newFile = new File( oldFile.parent, oldFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk")) variant.outputs.outputFile.set(0, newFile) } ``` Can someone help me out with this?

Original source

Related problems