Android 如何从 Gradle 设置 AAR 输出的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24728591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set name of AAR output from Gradle
提问by Affian
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
.
我有一个包含多个模块的项目,其中一个是名为(较差)为sdk
. 当我构建项目时,它会输出一个名为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.
我在 Android 或 Gradle 文档中找不到任何允许我更改 AAR 输出名称的内容。我希望它像 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?
如何更改com.android.library
Gradle 中生成的 AAR 的名称?
Gradle solution
摇篮解决方案
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
versionCode 4
versionName '1.3'
testFunctionalTest true
project.archivesBaseName = "Project name"
project.version = android.defaultConfig.versionName
}
回答by qix
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:
正如下面的评论和另一个答案中提到的,这里的原始答案不适用于 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:
新插件破坏了使用 Variant API 来操作变体输出。它仍然适用于简单的任务,例如在构建期间更改 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. 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:
我无法使用Android Studio 0.8.13/Gradle 2.1使 archivesBaseName & version 为我工作。虽然我可以在我的 defaultConfig 中设置 archivesBaseName 和 version,但它似乎不会影响输出名称。最后,将以下libraryVariants
块添加到我的 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)
}
}
}
回答by devz
For Android Studio 3with Gradle 4 and Android Plugin for Gradle 3.0.0 you have to change the answer of qixto 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"
}
}
}
}
回答by ligi
with the build-plugin 1.5.0 it is now possible to use archivesBaseName in the defaultConfig
使用 build-plugin 1.5.0 现在可以在 defaultConfig 中使用 archivesBaseName
回答by Hunter
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"
}
}
}
回答by Martin Pfeffer
In addition to qix answer here the info that you can add multipleoutput 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 - 由于可读性,我只是将其写为答案)。
回答by Spettacolo83
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 扩展将自动添加。