Add .jar dependency to Java Library Module Android Studio

I am trying to create a Java library in Android Studio. I want to use functionality of another Java library (third party available as a .jar) in it. So I placed the library in libs folder of my java library project and added the following code in build.gradle file:

apply plugin: 'java-library'
dependencies { 
    implementation files('libs/libraryname.jar')
}

The .jar reference was added successfully and I can use the library methods in my code. My project also compiles successfully and I am able to generate a .jar file of my own java library project.

However, when I am using the generated .jar file in another project and try to access the methods of third party library, I get the following exception:

java.lang.ClassNotFoundException: Didn't find class "com.packagename.classname" on path: DexPathList[[zip file "/data/app/com.packagename-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]

What I have tried

I have tried multiple solutions provided here and here but none of them works.

I have tried to clean and re-build the project and .jar files as well but that doesn't help as well.

I have added the .jar dependency as a module before referring it in my project but it still generates the same error.

Note:

I am using Android Studio 3.1.4

1 answer

  • answered 2018-11-08 13:16 Alesandro Giordano

    Copy .jar in your libs project folder: change the navigator to project view, than you are able to see libs directory. Right click on your jar folder inside your project and select add as library, this will automatically import the library on your gradle dependencies