如何使用Gradle从3个子模块构建1个jar [英] How to build 1 jar from 3 sub-modules by using gradle

查看:249
本文介绍了如何使用Gradle从3个子模块构建1个jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

  • Android Studio 3
  • 评分4.1
  • 分级工具3:classpath 'com.android.tools.build:gradle:3.0.1'
  • Android Studio 3
  • gradle 4.1
  • gradle tools 3: classpath 'com.android.tools.build:gradle:3.0.1'

当我有一个模块并使用gradle工具2时,我使用了:

When I had one module and used gradle tools 2, I used:

task makeJar(type: Copy) {
    def releaseFolder = new File('build/intermediates/bundles/release/')
    if (releaseFolder.exists()) {
        from('build/intermediates/bundles/release/')
    } else {
        from('build/intermediates/bundles/default/')
    }
    into('build/outputs/jar/')
    include('classes.jar')
    rename ('classes.jar', 'MY-Android-SDK.jar')
}


现在有3个模块:


Now have 3 modules:

              MainModule (com.my)
                 /   \
(com.my.m1) Module1   Module2 (com.my.m2)

我想从所有3个模块创建MY-Android-SDK.jar文件.

到目前为止,我将能够为每个模块创建3个jar文件,并以某种方式合并它们,但我认为这样做是不正确的.

So far I will be able to create 3 jar files per module and somehow merge them but I believe its not right way to do that.

有什么想法吗?

推荐答案

首先,如果要创建android库,请创建AAR,而不是JAR. AAR文件遵循android SDK结构,并且可以包含清单文件,因此除其他事项外,还需要权限和xml资源.

First of all, if you are creating android libraries, create AARs, not JARs. AAR files follow the android SDK structure and can have manifest files, and therefore, require permissions, and xml resources, among other things.

第二,如果要从几个不同的模块创建框架(实际上是一种非常合理的方法!),则应创建不同的工件(AAR/JAR文件).这样可以更轻松地处理传递依赖关系,SDK级别的冲突,仅消耗所需的内容(在某些情况下,您不需要一半的SDK,而在这种情况下,丢弃gradle文件中的一些条目比摆弄Gradle文件更容易Proguard删除所有未使用的类),提供其他实现或可插拔的系统(例如,您为DFP广告管理系统创建合同模块和双击模块.然后,通过控制反转,您可以使用double填充合同点击,下个月,他们会要求您用亚马逊广告替换双击.您在合同模块之后创建了一个包含亚马逊广告的模块,并用亚马逊替换了注入的双击引用.)

Second, if you are creating a framework from several different modules (a pretty reasonable approach in fact!), you should create different artifacts (AAR/JAR files). That makes easier to handle transitive dependencies, SDK level conflicts, consuming only what you need (there will be cases when you don't need half of your SDK and in that case discarding a few entries in the gradle files is way easier than fiddling with proguard to delete all the unused classes), providing alternative implementations or pluggable systems (for example, you create a contract module for, let's say, DFP, and a double click module. Then, by Inversion of Control, you fill the contract using double click, next month, they ask you to replace double click with amazon ads. You create a module with amazon ads following the contract module and replace the injected double click reference by amazon.)

无论如何,请记住传递性.根据您带来依赖项的方式,可以通过在build.gradle文件中使用单个依赖项将所有内容添加到路径中.如果您使用"api":

Regardless of that, remember transitivity. Depending on how you bring your dependencies, everything can be added to the path by using a single dependency in the build.gradle file. If you use 'api':

api 'com.myname:moduleX:xxx'

在模块com.myname:moduleY中,moduleX也将在路径中.首先使用 Gradle maven插件创建工件.这将允许您使用安装"任务创建一个android AAR文件,并将其部署到本地maven存储库(gradle中的mavenLocal()).如果使用"api"包含所有子模块,则它们将被添加到路径中.我不建议这样做,而是只导入每个模块中必需的内容,并为每个模块创建单独的AAR文件,然后在您的应用中使用它们.

In module com.myname:moduleY, moduleX will be in the path as well. Use the Gradle maven plugin for creating the artifacts in the first place. That will allow you to create an android AAR file using the "install" task and will deploy it to your local maven repo (mavenLocal() in gradle). If you include all the submodules using "api", they will be added to the path. I don't recommend doing that, instead, import only what's necessary in each module and create separate AAR files for each module and consume them in your apps.

现在,如果由于某种原因,您需要创建一个胖/Uber JAR,那么该过程称为阴影".因此,您需要gradle的shade插件.幸运的是,这里有很少.只需选择最适合您的情况(或使用Maven和Maven阴影插件)即可.无论如何,作为具有一定开发库和框架经验的人,请使您的模块保持原子性和尽可能独立.最后,如果您需要Gradle Maven插件的工作示例,请尝试,它很旧,但是可以使用举个例子.祝你好运!

Now, if for some reason, you need to create a fat/Uber JAR, well, that process is called "shading". So, you'll need a shade plugin for gradle. Fortunately, there's a few. Just pick the best for your case (or use maven, and the maven shade plugin). Regardless, as someone with some experience developing libraries and frameworks, keep your modules as atomic and separate as possible. Lastly, if you need a working example for the gradle maven plugin, try this, it's old, but works as an example. good luck!

这篇关于如何使用Gradle从3个子模块构建1个jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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