如何使用 Android Studio Arctic Fox 将 Android 库 aar 作为模块导入? [英] How to import an Android library aar as a module with Android Studio Arctic Fox?

查看:398
本文介绍了如何使用 Android Studio Arctic Fox 将 Android 库 aar 作为模块导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经能够将库 aar 文件作为模块导入,并且它们运行良好.我不知道如何使用最新版本的 Android Studio Arctic Fox.有人可以提供有关它的提示吗?

我可以按照官方说明添加aar作为依赖项通过在 build.gradle 中添加以下内容:

实现文件('libs/myLibrary-release.aar')

不幸的是,这将需要依赖的应用程序(即在其 build.gradle 中使用上述行来使用库的应用程序)知道 myLibrary 使用了哪些外部库并添加所有依赖项.例如,如果 myLibrary 有 30 个依赖项,例如实现 'joda-time:joda-time:2.10.5'",则每个依赖的应用程序都必须具有这 30 个依赖项.如果 myLibrary 更新了一个新的依赖项,所有依赖的应用程序也需要添加它.最糟糕的是该应用程序可以在没有这些依赖项的情况下正常构建和启动,但会在需要缺少依赖项时的运行时.

解决方案

我建议将 myLibrary 的所有依赖项嵌入 myLibrary 的 AAR 文件中.

嵌入非常简单 - 下载 myLibrary 的所有依赖项的所有 AAR 和 JAR,将它们保存在 lib 文件夹中,然后将新的本地依赖项添加到形式:

实现文件('libs/some-dependency.jar')

您确实需要担心消耗 AAR 的应用中的重复类定义.例如,如果您使用joda-time 库并且您的使用者也使用joda-time,则使用者的构建将失败,因为joda-time 库编译两次.

解决方案是通过将所有类的类路径更改为一个不会与您的消费者应用的依赖项冲突的唯一类路径来隐藏您的依赖项.

例如,类 org.joda.time.DateTime 将被转换为 just.a.unique.prefix.org.joda.time.DateTime.>

我已经看到阴影在起作用,但我缺乏这方面的经验.但请查看以下指南以帮助您:https://imperceptiblethoughts.com/shadow/

I used to be able to import library aar files as modules and they have been working perfectly. I cannot figure out how to do it with Android Studio Arctic Fox, its latest version. Could anyone offer a tip on it?

I can follow the official instructions to add an aar as a dependency by adding the following in build.gradle:

implementation files('libs/myLibrary-release.aar')

Unfortunately, this will require the dependent app (i.e., the app that uses the above line in its build.gradle to use the library) to know which external libraries are used by myLibrary and add all the dependencies too. For example, if myLibrary has 30 dependencies such as "implementation 'joda-time:joda-time:2.10.5'", every dependent app will have to have these 30 dependencies. If myLibrary updates with a new dependency, all the dependent apps will need to add it too. The worst thing is the app can build and start fine without these dependencies but will crash at runtime when a missing dependency is needed.

解决方案

I recommend embedding all of myLibrary‘s dependencies inside myLibrary’s AAR file.

Embedding is quite easy - download all of the AARs and JARs of all of myLibrary’s dependencies, save them in lib folder and add the new local dependencies in the form of:

implementation files('libs/some-dependency.jar')

You do need to worry about duplicate class definitions in the app that consumes your AAR. For example, if you usejoda-time library and your consumer also use joda-time, the consumer’s build will fail because joda-time library is compiled twice.

The solution is to shadow your dependencies by changing all classes’ class path to a unique class path which cannot collide with you consumer app’s dependencies.

For example, class org.joda.time.DateTime will be transformed to just.a.unique.prefix.org.joda.time.DateTime.

I’ve seen shadowing in action, but I’m lack of experience with it. But check out the following guide to help you out: https://imperceptiblethoughts.com/shadow/

这篇关于如何使用 Android Studio Arctic Fox 将 Android 库 aar 作为模块导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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