Android rename output of library project

android, android-gradle-plugin

Solution

Renaming the output file of a com.android.library module differs only slightly from the output of an com.android.application module.

In the com.android.application gradle plugin you can put

android.applicationVariants.all { variant ->
    def file = variant.outputFile
    variant.outputFile = 
        new File(file.parent, 
                 file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}

But in a com.android.library gradle plugin you use:

android.libraryVariants.all { variant ->
    def file = variant.outputFile
    variant.outputFile = 
        new File(file.parent, 
                 file.name.replace(".aar", "-" + defaultConfig.versionName + ".aar"))
}

If you wanted to only do this to specific variants you could so something like this:

if(variant.name == android.buildTypes.release.name) {
}

Problem

I'm assuming that my google-fu is just failing me but I can't figure out how to add the version number to the output of my library project. I'm using Android Studio (gradle) to build the library and I include it in other projects. I'd like to be able to add a version to the file to track which version of the library a given project is using so I would like the version number to be in the .aar that is generated. I can't figure that out. Any pointers?

Original source