简体   繁体   English

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

[英]How to set name of AAR output from Gradle

I have a project with several modules in it one of which is a Android Library named (poorly) as sdk .我有一个包含多个模块的项目,其中一个是名为(糟糕)为sdk的 Android 库。 When I build the project it outputs an AAR named sdk.aar .当我构建项目时,它会输出一个名为sdk.aar的 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.我在 Android 或 Gradle 文档中找不到任何允许我更改 AAR 输出名称的内容。 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.我希望它像 Jar 任务那样有一个基本名称 + 版本号,但我不知道如何为 AAR 做同样的事情,因为它的所有配置似乎都隐藏在 android.library 插件中。

Renaming the module isn't an option at this stage and that still wouldn't add the version number to the final AAR.在此阶段重命名模块不是一个选项,并且仍然不会将版本号添加到最终的 AAR。

How can I change the name of the AAR generated by com.android.library in Gradle?如何在 Gradle 中更改com.android.library生成的 AAR 的名称?

Gradle solution摇篮解决方案

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

As mentioned in comments below and another answer, the original answer here doesn't work with Gradle 3+.正如下面的评论和另一个答案中提到的,这里的原始答案不适用于 Gradle 3+。 Per the docs , something like the following should work:根据docs ,类似以下的内容应该可以工作:

Using the Variant API to manipulate variant outputs is broken with the new plugin.新插件破坏了使用 Variant API 来操作变体输出。 It still works for simple tasks, such as changing the APK name during build time, as shown below:它仍然适用于简单的任务,例如在构建期间更改 APK 名称,如下所示:

// 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 .我无法使用Android Studio 0.8.13/Gradle 2.1使 archivesBaseName & version 为我工作。 While I can set archivesBaseName and version in my defaultConfig, it doesn't seem to affect the output name.虽然我可以在我的 defaultConfig 中设置 archivesBaseName 和 version,但它似乎不会影响输出名称。 In the end, adding the following libraryVariants block to my android {} scope finally worked for me:最后,将以下libraryVariants块添加到我的 android {} 范围终于对我libraryVariants

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

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:对于带有 Gradle 4 的Android Studio 3和用于 Gradle 3.0.0 的 Android 插件,您必须将qix的答案更改为以下内容:

android {
...    

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

使用 build-plugin 1.5.0 现在可以在 defaultConfig 中使用 archivesBaseName

In my case, ${version} result in "unspecified", finnally I found ${defaultConfig.versionName} works.就我而言, ${version} 导致“未指定”,最后我发现 ${defaultConfig.versionName} 有效。

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

In addition to qix answer here the info that you can add multiple output paths by this method by an regular string as well:除了在这里 qix 回答之外,您还可以通过此方法通过常规字符串添加多个输出路径的信息:

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). (Upvotes属于qix - 由于可读性,我只是将其写为答案)。

For the latest version of Gradle 5+, this is the best answer following @frouo answer:对于最新版本的 Gradle 5+,这是@frouo 回答后的最佳回答:

defaultConfig {
    ...
    setProperty("archivesBaseName", "${archivesBaseName}-$versionName")
    ...
}

AAR extension will be added automatically. AAR 扩展将自动添加。

Using Gradle 6+ and AGP 4+, an alternative answer that allows full control of the name is:使用 Gradle 6+ 和 AGP 4+,允许完全控制名称的替代答案是:

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

For Gradle 5+:对于 Gradle 5+:

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

For Gradle 4+:对于 Gradle 4+:

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

For Gradle 2+ and Gradle 3+:对于 Gradle 2+ 和 Gradle 3+:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM