Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

gradle

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?

like image 330
Argyle Avatar asked Nov 21 '14 19:11

Argyle


3 Answers

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"))
        }
    }
}
like image 102
Kevin Coppock Avatar answered Oct 31 '22 21:10

Kevin Coppock


When using version 3.0.0 of the plugin or higher, you have to make a slight change:

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        outputFileName = "myapp-${variant.versionName}-${variant.name}.apk"
    }
}

The 3.0.0 plugin migration documentation says:

If you use each() to iterate through the variant objects, you need to start using all(). That's 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.

like image 20
gnuf Avatar answered Oct 31 '22 21:10

gnuf


I copied your answer with some modification:

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

the word "-assistant" is in my filename before the variant name. Example I have 3 variants

ax, bx, cx

The filenames were in my example:

  • the-assistant-ax-debug.apk
  • the-assistant-bx-debug.apk
  • the-assistant-cx-debug.apk

I wanted the version number in there before the variant name. So, after the above they are now called

  • the-assistant-1.0-ax-debug.apk
  • the-assistant-1.0-bx-debug.apk
  • the-assistant-1.0-cx-debug.apk

So, gradle build is adding the variant name in the filename for us.

The ${versionName} is simply "1.0". And I noticed that whether it is ${versionName} or ${variant.versionName}, both produced the same result.

That is to say, if had defaultConfig { versionName "a.b" } and productFlavors { ax { versionName "1.0" } }, the $versionName or $variant.versionName in the loop in this above code will contain the variant version name "1.0"

Hope this helps someoen.

like image 2
James Barwick Avatar answered Oct 31 '22 20:10

James Barwick