Android Studio:多模块项目中外部JAR的链接 [英] Android Studio: Link of external JAR in a multi modules project
问题描述
我有一个Android应用程序(在Android Studio上运行)。它由2个模块组成:
- 有一个低级纯java模块(我们称之为模块A)。
- 除此之外,还有模块B,它是Android应用程序。它依赖于moduleA进行一些处理。
I have an Android application (running on Android Studio). It is made of 2 modules: - There is a low level pure java module (let’s call it module A). - On top of it, there is the module B which is the Android application. It relies on moduleA for some processings.
这个工作正常。
我现在需要模块A来调用外部库。我已经下载了JAR文件并将其放在moduleA / libs文件夹中。
此libs文件夹在moduleA的gradle依赖项中引用,因此编译正常。但是,运行时会出现异常:
I now need the module A to call an external Library. I have downloaded the JAR file and put it in moduleA/libs folder. This libs folder is referenced in gradle dependency of moduleA so the compilation is OK. However, there is an exception at runtime:
FATAL EXCEPTION引起:java.lang.ClassNotFoundException:在路径上找不到类XXXX:DexPathList
FATAL EXCEPTION Caused by: java.lang.ClassNotFoundException: Didn't find class "XXXX" on path: DexPathList
我看到APK不包含JAR文件,因此发生此异常是正常的。
I have seen that the APK doesn’t contain the JAR file so it is normal that this exception occurs.
如果我在moduleB / libs中复制相同的JAR文件,然后它工作正常,但我在项目中有两次JAR文件!处理这个问题的清洁解决方案是什么?
我猜它可以用gradle解决,但我不知道怎么做。
If I copy the same JAR file in moduleB/libs, then it works fine but I have the JAR file two times in the project! What is the clean solution to handle this? I guess that it can be solved with gradle but I don't know how.
非常感谢
Olivier
Thank you very much Olivier
推荐答案
我已经能够解决这个问题。关于Gradle的这篇文章对我帮助很大:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Creating-a-Library-Project
I have been able to fix this issue. This reading about Gradle helped me a lot: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Creating-a-Library-Project
以下是我所做的:
我没有将JAR文件放在moduleA / libs文件夹中,而是通过单击项目然后右键在Android Studio中导入JAR文件单击 - >新建 - >模块。然后我点击导入.JAR / .AAR包。
这创建了一个包含JAR文件+ gradle脚本的模块。
Here is what I have done: Instead of putting the JAR file in moduleA/libs folder, I have imported the JAR file in Android Studio by clicking on the project then right click -> new -> module. I then clicked on "Import .JAR/.AAR package". This created a module containing the JAR file + a gradle script.
然后,在moduleA的gradle脚本中,我在依赖项中添加了这个:
编译项目(路径:':name_of_the_jar_file')
Then, in moduleA’s gradle script, I have added this in the dependencies: compile project(path: ':name_of_the_jar_file')
我重建了所有并且它有效。 JAR文件现在存在于APK中,并且在运行时不会再发生崩溃。
I rebuilt all and it works. The JAR file is now present in the APK and there is no more crash at runtime.
这篇关于Android Studio:多模块项目中外部JAR的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!