android - 如何从 Gradle 设置 AAR 输出的名称

标签 android gradle android-library

我有一个项目,其中有几个模块,其中一个是一个名为(糟糕)为 sdk 的 Android 库。当我构建项目时,它会输出一个名为 sdk.aar.

的 AAR

我无法在 Android 或 Gradle 文档中找到任何允许我更改 AAR 输出名称的内容。我希望它像 Jar 任务那样有一个基本名称 + 版本号,但我不知道如何为 AAR 做同样的事情,因为它的所有配置似乎都隐藏在 android.library 插件中。

在这个阶段重命名模块不是一个选项,并且仍然不会将版本号添加到最终的 AAR。

如何在 Gradle 中更改 com.android.library 生成的 AAR 的名称?

Gradle 解决方案

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

最佳答案

正如下面的评论和另一个答案中提到的,这里的原始答案不适用于 Gradle 3+。根据 docs ,类似以下的东西应该可以工作:

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"
    }
}

旧答案:

我无法使用 Android Studio 0.8.13/Gradle 2.1 获得适合我的 archivesBaseName 和版本。虽然我可以在我的 defaultConfig 中设置 archivesBaseName 和 version,但它似乎不会影响输出名称。最后,将以下 libraryVariants block 添加到我的 android {} 范围最终对我有用:

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)
        }
    }
}

关于android - 如何从 Gradle 设置 AAR 输出的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24728591/

相关文章:

带有背景的android波纹按钮

gradle - 如何使用 Gradle 和 JUnit 并行运行测试(每个测试)

android - 为什么不在我的自定义库中添加 support-v4 依赖项运行时

email - Android Studio Gradle javax.mail activation.jar依赖关系问题

android - 如何在 Android Studio 中并行开发库和应用程序?

android - 如何为 HoloColorPicker 不透明度栏设置默认颜色?

java - 当我在 androidX 中导入 Migrating to the New Places SDK Client 时。无法访问 getPhotoMetadatas() 方法

用于 Gradle 'sourceCompatibility' 和 'targetCompatibility' 的 Java 版本

java - 使用过零率区分浊音/清音语音

android - 依赖于 'aar' 的 Gradle Android 单元测试