使用 Gradle 在多个 Android 应用程序之间共享 Android 库 [英] Sharing an Android library between multiple Android apps using Gradle
问题描述
我有 Android 应用 A 和 B.我想从每个应用中提取重复的代码到共享库 L 中.如何使用 Gradle 执行此操作?我之前看过这个问题的一些排列,但答案很少.
I have Android apps A and B. I want to extract duplicate code from each into a shared library L. How can I do this using Gradle? I've seen a few permutations of this question asked before, but there were very few answers.
最接近/最好的问题是这个:
The closest/best already-asked question is this one:
多个 Android 应用,取决于带有 gradle 的 android 库一个>
第一个答案 建议一个包含应用程序和库模块的父项目.这种耦合是不可取的,部分是因为 A、B 和 L 都在它们自己的 Git 存储库中,而且还因为它会涉及到难以置于源代码控制中的父项目和构建文件(或将涉及手动复制其他项目).由于Android Studio默认喜欢单模块项目的父级构建......好吧,应该是一个简单的家庭,这只是很多父级.这看起来真的像是在完全不相关的项目之间进行了不必要的耦合.
The first answer suggests a parent project that encompasses both apps and the library module. This coupling is undesirable, in part because A, B, and L are in their own Git repository, but also because it would involve having a parent project and build files that would be difficult to put in source control (or would involve manual copying of the other projects). Since Android Studio likes a parent build for single-module projects by default...Well, it's just a lot of parents for what should be a simple family. It really seems like an unecessary coupling between projects that are otherwise completely unrelated.
第二个答案涉及将 AAR 发布到存储库并在每个项目中引用远程库.我会接受这种方法,因为我们有一个本地 Nexus 存储库,但似乎没有一种简单的方法可以在没有手动版本控制/命名和上传文件的情况下执行此操作(这是不可持续的).我正在尝试使用 Sonatype gradle-release 插件,但它似乎适用于 JAR(而不是 AAR).
The second answer involves releasing an AAR to a repo and referencing the remote library in each project. I would be fine with this approach as we have a local Nexus repository, but there does not seem to be a simple way to do this without manually versioning/naming and uploading files (this is unsustainable). I'm trying to use the Sonatype gradle-release plugin, but it seems to be for JARs (not AARs).
对于 Android 应用程序之间最基本的代码共享形式来说,这似乎是一个移动部分.有人有更好的解决方案吗?
It just seems like a lot of moving parts for what amounts to the most basic form of code sharing between Android apps. Does anyone have a better solution?
推荐答案
根据Android官方文档(https://developer.android.com/studio/projects/android-library.html):通过导入模块,库模块被复制到您的项目中,因此您可以实际编辑库代码.如果您想维护单一版本的库代码,那么这可能不是您想要的,您应该按照上述方式导入已编译的 AAR 文件.
According to official document of Android(https://developer.android.com/studio/projects/android-library.html): By importing module, the library module is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead import the compiled AAR file as described above.
还有另一种选择,git submodule.您可以让您的应用程序引用您图书馆的 SHA.所以图书馆只有一份.每个应用程序仅使用一个 SHA.你应该多注意图书馆的分支管理.详情请查看:https://github.com/blog/2104-working-with-子模块
There is another option, git submodule. You can make your app refer to a SHA of your library. So there is only one copy for the library. Each app is using only a SHA. You should pay little bit more attention for branch management of the library. Check this for detail: https://github.com/blog/2104-working-with-submodules
这篇关于使用 Gradle 在多个 Android 应用程序之间共享 Android 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!