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.
Related
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
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 am having my android studio project with four library modules and one application module in it.
I am having six jar files which I have kept in one directory.
Each of these modules are using some of the jar files.
So I have added dependencies of these jars in build.gradle file of respective module with relative path using "compile files('./../../deliverables/xyz.jar')".
Application module is dependent on all these library modules.
When I create release build of my application module, it fails with "duplicate zip entry" error of proguard as I have enabled proguard in gradle build script.
This error is for six jar files.
The debug build of this project is working fine.
I tried below solutions:
1. injar option in proguard-rules.pro file
2. dontwarn option in proguard-rules.pro file
3. keep option in proguard-rules.pro file
Please help me.
Thanks.
One solution is to create a libraries module whose sole purpose is to contain the .jar files. Once all the jar files that you need are in this module, you can remove them from the other modules and reference your library module in your other modules.
For example, let's say we have the app module depend on these four modules:
app
|---module1
|---module2
|---module3
|---module4
We can create a libraries module that you will reference in the modules that use at least one of the libraries contained in the module, by adding the following to the module's build.gradle:
compile project(':libraries')
I am using a thirdparty jar file in my android project. When I add the jar to the build paths, via eclipse project -> properties -> java build path -> Libraries -> add external jar.
The project compiles fine until that line of code runs which uses any class from that jar and I get class not found exception at runtime and the program crashes.
I solved it by copying the third party jar file to the libs folder of my project. The class not found exception goes away.
Question:
Can someone please shed some light why this is required and why does it not just work with a reference from the project properties despite the fact it compiles fine.. Why do I have to explicitly copy it to libs folder ?
When I add the jar to the build paths, via eclipse project -> properties -> java build path -> Libraries -> add external jar.
That is not the proper procedure for use with the Android build tools.
I solved it by copying the third party jar file to the libs folder of my project. The class not found exception goes away.
Correct.
Can someone please shed some light why this is required and why does it not just work with a reference from the project properties despite the fact it compiles fine.. Why do I have to explicitly copy it to libs folder ?
Because only the JARs in libs/ are added to the APK file. The Java build path is pure a compile-time thing, not tied into Android's packaging of APK files.
All the libraries in the folder “libs” are found by the Android Eclipse plugin and are added to the “Android Dependencies” item of the project. A better illustration here
:http://android.foxykeep.com/dev/how-to-fix-the-classdefnotfounderror-with-adt-17
I would like to include this plist parser module in my Android application, ideally without just copying the entire module source into my source tree (if that would even work).
I have successfully added the module as a project into Eclipse (3.7.0) and resolved errors by fixing the build path to include Android 2.1, which is what I am using. However, now I'm stuck. All of the information on using external libraries with your Android project I can find expect you to have a JAR of the library, but I only have this source code. I can run the plist parser module as an Android application, which appears to compile an .apk, but that doesn't actually do anything because it's not a standalone application. Any options to just build the module without running it are greyed out in the Eclipse interface.
How can I either build this module into a .jar for inclusion, or include it in some other way?
Edit: In order to clear the errors in the module after I added it to Eclipse, I followed the instructions in this answer.
You can either convert the whole thing to a library project or simply include the source code in your app's project. To create a library project, you can import the project from github, and after you get it compile, remove any activities, go to the project's Properties->Android and check the 'Is a library' check box. Then add it as a dependency to your own project.