Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android rename output of library project

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?

like image 973
Jeff Avatar asked Aug 31 '25 05:08

Jeff


2 Answers

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) {
}
like image 186
Jeff Avatar answered Sep 02 '25 19:09

Jeff


Android Gradle Plugin v2.+

Newer (2.+) Android Gradle Plugin version do not have a variant.outputFile property. This is what worked for me:

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(
                output.outputFile.parent,
                output.outputFile.name.replace((".aar"), "-${version}.aar"))
    }
}

See the docs for complete description of the dsl for v2.3

Android Gradle Plugin v3.+

Version 3 plugin does not support the outputFile anymore. That's because variant-specific tasks are no longer created during the configuration stage. This results in the plugin not knowing all of its outputs up front, but it also means faster configuration times. Note that you need to use all instead of each because the object doesn't exist at configuration time with the new model.

android.libraryVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.aar"
    }
}

See the v3 migration guide for more detail.

like image 21
Patrick Favre Avatar answered Sep 02 '25 19:09

Patrick Favre