Android Studio: Link of external JAR in a multi mo

2020-07-14 12:29发布

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.

This is working fine.

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 Caused by: java.lang.ClassNotFoundException: Didn't find class "XXXX" on path: DexPathList

I have seen that the APK doesn’t contain the JAR file so it is normal that this exception occurs.

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.

Thank you very much Olivier

1条回答
霸刀☆藐视天下
2楼-- · 2020-07-14 12:51

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

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.

Then, in moduleA’s gradle script, I have added this in the dependencies: compile project(path: ':name_of_the_jar_file')

I rebuilt all and it works. The JAR file is now present in the APK and there is no more crash at runtime.

查看更多
登录 后发表回答