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
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
Related
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
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.
I built one android studio project. I wanted to use it as a library in another project. I followed below steps for same:
Changed apply plugin: 'com.android.application' to 'com.android.library'
Removed applicationId from defaultConfig{}
Clicked on sync
This gave me .aar file in build-->outputs-->aar-->myapp.aar
I imported this .aar file in my other test project. I followed below steps for this:
File-->New-->New module-->Import .jar/.aar package-->myapp.aar
In build.grade of test application added compile project(path: ':myapp')
Clicked on sync.
This produced one folder inside external libraries called myapp-unspecified.
There i get all res files but i did not get the class files inside classes.jar.
Inside classes.jar I only have MANIFEST.MF.
Am I doing something wrong or am I missing something??
Experienced the same problem. My issue was that I had removed a dependency from my library project (in this case, testCompile 'junit:junit:4.12'), but did not delete the directory and classes that referenced the APIs from that dependency. An aar file was output to the build folder, but I was not able to access its contents when I included it in my application project.
After deleting the unused classes and directories and re-syncing my library project, I was able to produce a working aar.
Ran into the same issue. The classes are actually there or at least were for me. All that I needed to do was right click the application I was importing my library into. Click open module settings. Then go to dependencies on the right side. Add your library there as well so that the java will compile will your application.
I have a jar file called PhoneGestures.jar which contains a bunch of java files I want to use in my project, which I have added to my project as a module using New->Module->Import .Jar :
I then added this jar module to my project dependencies like so:
and as you can see it is included in my project's build.gradle:
But now when I go to use one of the classes from my jar file, like "MobXRotate", for example, Android Studio does not recognise it:
Does anyone have any ideas why this might not be working? Thanks.
Make sure you have imported the class if the class isn't been found either,
try build your project first, that gradle copies the source code in your project structure!
There's no need to create a new module for a jar file.
Putting the jar file in libs folder will do the trick.
I am trying to add this library to my project because I want to change something in a class there. That is why I adding it as a dependencies in my gradile setting is not enough.
I know that in the library it says "If you want use this library, you only have to download MaterialDesign project, import it into your workspace and add the project as a library in your android project settings.". But what does that mean, should I copy all files and put them in my libs folder ?
Quoting the documentation:
If you want use this library, you only have to download MaterialDesign project, import it into your workspace and add the project as a library in your android project settings.
Step #1: Download the project, whether via the ZIP file that GitHub gives you or by using git to clone the repository
Step #2: Move the MaterialDesign/ directory into your project directory, as a peer of your existing app/ module
Step #3: Modify your settings.gradle in your project directory to include :MaterialDesign in the list of modules to build
Step #4: In your app module's build.gradle file, add compile project(':MaterialDesign') to your dependencies
I have made an android project in eclipse. This project has a library project:
the library project (marked with "is library") has included a jar file.
this library project does a lot of stuff with the use of the jar file.
in code everything is fine.
The problem is that the reference to the jar file doesn't exist anymore when it is compiled and installed to my phone.
When i add the jarfile also to my base project. Then the program works.
However, i don't believe that this is the right solution.
So what does i do wrong?
You should add a libs folder to your project
Then, add the jar to the folder and right click on the .jar file and go to Build Path, and select Add To Path or Build To Path. This way the library will be a part of your project. This should help.
The library jar you used in your Android Library Project must be manually added to the dependent application project's build path, not the Android library project build path itself, check out API here