Reputation: 3002
I have a library project in Android Studio. I want to generate a .jar file for this library. I need the jar file contain/include all the dependencies
this Library project has.
The problem is I couldn't find a way of generating a jar file. Is there any way I can generate a jar file for my project?
Best Regards
Upvotes: 6
Views: 12372
Reputation: 63
If you set up the code as a plain Java module in Gradle, then it's really easy to have Gradle give you a jar file with the contents. That jar file will have only your code, not the other Apache libraries it depends on. I'd recommend distributing it this way; it's a little weird to bundle dependencies inside your library, and it's more normal for users of those libraries to have to include those dependencies on their own (because otherwise there are collisions of those projects are already linking copies of the library, perhaps of different versions). What's more, you avoid potential licensing problems around redistributing other people's code if you were to publish your library.
Take the code that also needs to be compiled to a jar, and move it to a separate plain 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:
Choose your module from the list in the dialog that comes up:
Hopefully your project should be building normally now. After you do a build, a jar file for your Java library will be placed in the build/libs directory in your module's directory. If you want to build the jar file by hand, you can run its jar build file task from the Gradle window:
Oroginal article here : How to make a .jar out from an Android Studio project
Upvotes: 1
Reputation: 4301
Here is an example of Gradle
task which creates .jar
from classes.jar
.
classes.jar
file can be found in /build/intermediates/exploded-aar/
folder.
Edit:
Create jar file for every Android Librar
y project and distribute them standalone. What about dependencies required by library projects - add those dependencies in final project, where your library will be used.
For example: you Android Library
project MyLibProject
, which has following dependencies in build.gradle
dependencies {
compile "com.android.support:support-v4:19.+"
}
When you'll build your project you'll get classes.jar
for MyLibProject
, and those classes.jar
will contain just class from library project, but not from com.android.support:support-v4:19.+
.
After that in another project you add classes.jar
as library dependency in build.gradle
and also define same dependencies as in MyLibProject
.
And if you still need fatJar
, look here how to create fatJar
with Gradle
.
Upvotes: 1