5

I have an Android project with an Android application that depends on a pure Java library (and the Java library uses some others compiled jars libraries).
I added the dependencies, I can build the project, but at run time I have a ClassNotFoundException error.
I had to add to theCLASSPATHenvironment variable the path to the jars.
Is there a way to set the classpath locally for the project only, like using the command line option

java –classpath <path to the jars>

in the Android studio Run/Debug Configurations?

Stefano Piovesan
  • 1,185
  • 3
  • 19
  • 37
  • possible duplicate of [How do I add a library project to the Android Studio?](http://stackoverflow.com/questions/16588064/how-do-i-add-a-library-project-to-the-android-studio) – Omer Sonmez Oct 23 '14 at 05:51

3 Answers3

18

First off all, be sure there's a "libs" subfolder in the "app" folder of your project. If there's not, create one. Next, in the app/build.gradle file, add this line of code:

dependencies {
    ...
    compile fileTree(dir:'libs', include: ['*.jar'])
    ...
}

Place all of your .jar files in the "libs" folder, and voila, you're done

Arnold Layne
  • 194
  • 1
  • 11
  • and locate the .jar file from step 1) this line are not clear.. where .jar file in my project. – Abhinav singh Sep 28 '15 at 10:20
  • This does not work for me. Whenever I try this and build, I get errors saying that there is no resource file matching any color( for example colorAccent or colorPrimary) and it can't find any parent for item Theme.appCompat etc. – Essej Apr 11 '16 at 16:32
  • Concerning #8 "if module is listed"...what If I followed all the preceding steps and it is not? – Legato Jun 08 '16 at 03:37
3

Thanks to Arnold Layne, I created a folder named libs.

Then, I went to

Files -> Project Structure (new menu opens) -> Modules (on the left) -> app -> Dependencies

There, I could add the library with the + Button.

This created this entry:

dependencies {
    implementation files('libs/commons-lang3-3.7.jar')
}
User
  • 14,131
  • 2
  • 40
  • 59
0

Add this lines to build.gradle of app module.

dependencies {
    ...
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation files('libs/mysql-connector-java-8.0.16')
    ...
}
dniHze
  • 2,162
  • 16
  • 22