I am using the following simplified configuration in an Android application project.
android {
compileSdkVersi
Android Plugin for Gradle 3.0.0
You can use like this
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${variant.name}-${variant.versionName}.apk"
}
}
you can get more on the features and new changes in android documentation https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#update_gradle
Building on the answer from Larry Schiefer you can change the script to something like this:
android {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name.replace('.apk', "-${versionName}.apk")
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
The complete code snippet looks like that one:
// Customize generated apk's name with version number
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def manifestParser = new com.android.builder.core.DefaultManifestParser()
def fileName = outputFile.name.replace(".apk", "-DEBUG-" + manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) + ".apk")
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
The build variant output API has changed in the latest Android Gradle plugin. It now allows multiple output files (or directories), which is why this method has been marked as deprecated. If you use variant.outputs
instead, it will give you a Collection
you can then iterate over and get each output file. You'll have to verify the file object is non-null and that it matches your criteria (e.g. has a '.apk' extension.) Then you can create a new File
object and add it to the output within the collection.