Creating a Jar file in Android Studio

I have a library project in Android Studio. I want to create a .jar file for this library. I need jar file contain/include all the dependencies this library project.

The problem is that I could not find a way to create the jar file. Is there a way to create a jar file for my project?

Best wishes

+5
source share
2 answers

Here is an example of a Gradle task that creates .jar from classes.jar .

classes.jar file can be found in the /build/intermediates/exploded-aar/ .

Edit: Create a jar file for each Android Librar y project and distribute them separately. Regarding the dependencies required by library projects, add these dependencies to the final project where your library will be used.

For example: you are an Android Library project MyLibProject , which has the following dependencies in build.gradle

 dependencies { compile "com.android.support:support-v4:19.+" } 

When you create the project, you will get classes.jar for MyLibProject , and those classes.jar will contain only the class from the library project, and not from com.android.support:support-v4:19.+ .

After that, in another project, you add classes.jar as a library dependency in build.gradle , and also define the same dependencies as in MyLibProject .

And if you still need fatJar , see here how to create fatJar using Gradle .

+1
source

If you configured the code as a simple Java module in Gradle, it is really easy to have Gradle provide you with a jar file with the contents. This jar file will only have your code, not other Apache libraries. I would recommend distributing it this way; linking dependencies inside your library is a little strange, and it is more common for users of these libraries to include these dependencies on their own (since otherwise conflicts between these projects already link copies of the library, possibly of different versions). What's more, you avoid potential licensing issues when redistributing other people's code if you want to publish your library.

Take the code that also needs to be compiled into a jar and move it to a separate simple Java module in Android Studio:

 File menu > New Module... > Java Library Set up the library, Java package name, and class names in the wizard. (If you don't want it to create a class for you, you can just delete it once the module is created) In your Android code, set up a dependency on the new module so it can use the code in your new library: File > Project Structure > Modules > (your Android Module) > Dependencies > + > Module dependency. See the screenshot below: 

enter image description here

Select your module from the list in the dialog box that appears:

enter image description here

I hope your project should be built normally. After creating the assembly, the jar file for your Java library will be placed in the build / libs directory in your modules directory. If you want to create the jar file manually, you can start the task of creating the jar file from the Gradle window:

enter image description here

Oroginal article here: How to make .jar from an Android Studio project

+1
source

Source: https://habr.com/ru/post/1214141/


All Articles