[转载]How to set name of AAR output from Gradle

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.all { output ->
        if (outputFile != null && outputFileName.endsWith('.aar')) {
            outputFileName = "${archivesBaseName}-v${versionName}.aar"
        }
    }
}

https://stackoverflow.com/questions/24728591/how-to-set-name-of-aar-output-from-gradle

生成带混淆配置的aar库

通过将 ProGuard 配置文件添加到包含其 ProGuard 指令的库,您可以在自己的库上启用代码压缩。构建工具会为库模块将此文件嵌入到生成的 AAR 文件中。在您将库添加到应用模块时,库的 ProGuard 文件将附加至应用模块的 ProGuard 配置文件 (proguard.txt)。

通过将 ProGuard 文件嵌入到您的库模块中,您可以确保依赖于此库的应用模块不必手动更新其 ProGuard 文件即可使用库。当 ProGuard 在 Android 应用模块上运行时,它会同时使用来自应用模块和库的指令,因此您不应当只在库上运行 ProGuard。

要指定您的库的配置文件名称,请将其添加到 consumerProguardFiles 方法中,此方法位于您的库的 build.gradle 文件的 defaultConfig 块内。例如,以下片段会将 lib-proguard-rules.txt 设置为库的 ProGuard 配置文件:

android {
    defaultConfig {
        consumerProguardFiles 'lib-proguard-rules.txt'
    }
    ...
}

默认情况下,应用模块会使用库的发布构建,即使在使用应用模块的调试构建类型时亦是如此。要使用库中不同的构建类型,您必须将依赖项添加到应用的 build.gradle 文件的 dependencies 块中,并在库的 build.gradle 文件中将 publishNonDefault 设置为 true。例如,您应用的 build.gradle 文件中的以下代码段会使应用在应用模块于调试模式下构建时使用库的调试构建类型,以及在应用模块于发布模式下构建时使用库的发布构建类型:

dependencies {
    debugCompile project(path: ':library', configuration: 'debug')
    releaseCompile project(path: ':library', configuration: 'release')
}

您还必须在自己库的 build.gradle 文件的 android 块内添加以下代码行,以便将此库的非发布配置展示给使用它的项目:

android {
    ...
    publishNonDefault true
}

android 官方文档:https://developer.android.com/studio/projects/android-library
https://blog.csdn.net/lihenair/article/details/51671803
https://www.jianshu.com/p/14af4a474d55