如何从Gradle设置AAR输出的名称 [英] How to set name of AAR output from Gradle

查看:1068
本文介绍了如何从Gradle设置AAR输出的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中包含多个模块,其中一个是Android库(较差地命名为sdk).当我构建项目时,它会输出一个名为sdk.aar的AAR.

我在Android或Gradle文档中找不到任何可以更改AAR输出名称的内容.我希望它像Jar任务一样具有基本名称+版本号,但是我无法解决如何对AAR进行相同的操作,因为它的所有配置似乎都隐藏在android.library插件中./p>

重命名模块在此阶段是不可行的,并且仍然不会将版本号添加到最终的AAR中.

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

分级解决方案

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

解决方案

如下面的评论和另一个答案中所述,此处的原始答案不适用于Gradle 3+.根据文档,像下面这样应该可以工作:

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

旧答案:

我无法获取存档BaseName&适用于我的版本,带有 Android Studio 0.8.13/Gradle 2.1 .虽然可以在defaultConfig中设置archivesBaseName和版本,但它似乎不会影响输出名称.最后,将以下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)
        }
    }
}

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
}

解决方案

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

这篇关于如何从Gradle设置AAR输出的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆