Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: Link of external JAR in a multi modules project

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

like image 836
OlivierGrenoble Avatar asked Jan 02 '17 16:01

OlivierGrenoble


People also ask

Where is the jar file in Android Studio?

If you are unable to find the libs folder in Android studio then open your android project in “Project” mode If the project is already opened in the “Android” mode. Then go to Your Project Name > app > libs and right-click on it and paste the downloaded JAR files.

What is multi module Android project?

A project with multiple Gradle modules is known as a multi-module project. This guide encompasses best practices and recommended patterns for developing multi-module Android apps. Note: This page assumes a basic familiarity with the recommended app architecture.


1 Answers

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.

like image 118
OlivierGrenoble Avatar answered Oct 23 '22 05:10

OlivierGrenoble