How to set name of AAR output from Gradle

AndroidGradleAndroid Library

Android Problem Overview


I have a project with several modules in it one of which is a Android Library named (poorly) as sdk. When I build the project it outputs an AAR named sdk.aar.

I haven't been able to find anything in the Android or Gradle documentation that allows me to change the name of the AAR output. I would like it to have a basename + version number like the Jar tasks do, but I can't work out how to do the same for the AAR because all the config for it seems to be hidden in the android.library plugin.

Renaming the module isn't an option at this stage and that still wouldn't add the version number to the final AAR.

How can I change the name of the AAR generated by com.android.library in Gradle?

Gradle solution

defaultConfig {
    minSdkVersion 9
    targetSdkVersion 19
    versionCode 4
    versionName '1.3'
    testFunctionalTest true
    project.archivesBaseName = "Project name"
    project.version = android.defaultConfig.versionName
}

Android Solutions


Solution 1 - Android

As mentioned in comments below and another answer, the original answer here doesn't work with Gradle 3+. Per the docs, something like the following should work:

> Using the Variant API to manipulate variant outputs is broken with the > new plugin. It still works for simple tasks, such as changing the APK > name during build time, as shown below:

// 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.
android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

OLD ANSWER:

I am unable to get archivesBaseName & version to work for me w/ Android Studio 0.8.13 / Gradle 2.1. While I can set archivesBaseName and version in my defaultConfig, it doesn't seem to affect the output name. In the end, adding the following libraryVariants block to my android {} scope finally worked for me:

libraryVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('.aar')) {
            def fileName = "${archivesBaseName}-${version}.aar"
            output.outputFile = new File(outputFile.parent, fileName)
        }
    }
}

Solution 2 - Android

For Android Studio 3 with Gradle 4 and Android Plugin for Gradle 3.0.0 you have to change the answer of qix to the following:

android {
...    

    libraryVariants.all { variant ->
    	variant.outputs.all { output ->
    		if (outputFile != null && outputFileName.endsWith('.aar')) {
    			outputFileName = "${archivesBaseName}-${version}.aar"
    		}
    	}
    }
}

Solution 3 - Android

In my case, ${version} result in "unspecified", finnally I found ${defaultConfig.versionName} works.

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

Solution 4 - Android

with the build-plugin 1.5.0 it is now possible to use archivesBaseName in the defaultConfig

Solution 5 - Android

For the latest version of Gradle 5+, this is the best answer following @frouo answer:

defaultConfig {
    ...
    versionName "some-version-name-or-number"
    setProperty("archivesBaseName", "${archivesBaseName}-$versionName")
    ...
}

AAR extension will be added automatically.

Solution 6 - Android

In addition to qix answer here the info that you can add multiple output paths by this method by an regular string as well:

libraryVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.aar')) {
                def fileName = "${archivesBaseName}-${version}.aar"
                output.outputFile = new File(outputFile.parent, fileName)
                output.outputFile = new File("/home/pepperonas/IdeaProjects/Libraries/Base/testapp/libs", fileName)
            }
        }
    }

(Upvotes belong to qix - I just wrote this as an answer because of the readability).

Solution 7 - Android

Using Gradle 6+ and AGP 4+, an alternative answer that allows full control of the name is:

afterEvaluate {
    android.libraryVariants.all { variant ->
        variant.variantData.outputFactory.apkDataList.each { apkData ->
            if (apkData.outputFileName.endsWith('.aar')) {
                apkData.outputFileName = "${project.name}-${buildType.name}-anything-you-want.aar"
            }
        }
    }
}

Solution 8 - Android

For Gradle 5+:

android.libraryVariants.all { variant ->
    variant.variantData.outputFactory.output.apkDatas.each { apkData ->
        apkData.outputFileName = "YourNewFileName.aar"
    }
}

For Gradle 4+:

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFileName = "YourNewFileName.aar"
    }
}

For Gradle 2+ and Gradle 3+:

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(file.parent, "YourNewFileName.aar")
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAffianView Question on Stackoverflow
Solution 1 - AndroidqixView Answer on Stackoverflow
Solution 2 - AndroiddevzView Answer on Stackoverflow
Solution 3 - AndroidHunterView Answer on Stackoverflow
Solution 4 - AndroidligiView Answer on Stackoverflow
Solution 5 - AndroidSpettacolo83View Answer on Stackoverflow
Solution 6 - AndroidMartin PfefferView Answer on Stackoverflow
Solution 7 - Androidwill.quastView Answer on Stackoverflow
Solution 8 - Androidbo yinView Answer on Stackoverflow