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?
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) {
}
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With