Problems with using a jar file as a library in android studio

Issue

I intend to extract some part of my project into a jar file in order to reuse it for another project. But before doing this, I have made a .jar file only of a package in my project. Right now i’m struggling to reuse the jar file in my project.

The jar file is generated through cmd:

jar cf filename.jar <folder>

The jar file is then added as a library into the folder libs.

Now I want my current project to use the package inside the jar file in order to compile and not the package that already is in my project. But before even managing to do so, I encounter the following issue in every class inside the jar file:

enter image description here

enter image description here

jarred being the jar file containing the package.

I’ve been googling and trying to find whats causing this but there’s nothing i’ve found yet. I appreciate any help!

Solution

If I understand it right, the problem here is that you are not build a library.

A jar file is basically just a ZIP file with some meta data.
So if yo do a jar cf filename.jar src/main/java/my/package, what you get is basically a jar file containing your source code. But those jar file you cant directly use as dependency in your project (AFAIK).

If you want to share code with multiple projects, the easiest way would be to setup an Gradle library project, move the code you want to share into it and then let gradle build your jars using ./gradlew jar.

Answered By – Björn Schramke

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published