Creating JAR with dependencies using ShadowJAR.

󰃭 2024-08-21

~

Alt text for my gif

Background:

Lets get something out of the way, I work as Android Developer full-time. And because of that I am currently juggling different stacks, React Native, Flutter, Native, you name it.

Everything is quite smooth ☠️, NOT.

But I particularly faced a few challenges while working with Flutter. Let me explain why, It is because of the fact that I am working in building an SDK for Flutter that internally interacts with Android Native SDK.

The PROBLEM:

HINT: Everything is a problem!

I have to make changes in 3 separate projects:

  • The native SDK for Android
  • The Flutter SDK
  • The project where Flutter SDK is integrated

You might be thinking; YEA, right, what’s a big deal in that? Allow me to elaborate.

I have written about the flow here in this post please read it before (or after) moving forward. Those steps are hard to keep on doing everytime I change my native SDK’s files. This was really tiring as it took me 5+ minutes for all that because of my stupid ADD brain.

I wrote a bash script and a Java CLI program to help me accomplish, I will write a post about it very soon. That help me automate everything and it does all the tasks faster than I do, amazing! Let’s dive into the actual portion of this post.

Enter ShadowJAR:

I developed my Java program in IntelliJ IDEA and it was time for building a release. I decided to use ShadowJAR because why not? It will help me put almost all realted dependencies in the same jar file. Saves me a ton of headaches too.

First, we have to inlcude the dependency in our project in root_dir > build.gradle file.

// add the code below in your plugins
plugins {
    id 'com.gradleup.shadow' version '8.3.0'
}

Then, we need to add the following block in the same file.

shadowJar {
    archiveBaseName.set('release') // rename this to whatever name you prefer my-release-file, but don't add .jar
    archiveClassifier.set('')
    archiveVersion.set('')
}

Final, you have to declare your main class in the manifest file. Again, add the block below in the same file:

tasks.jar {
    // replace org.example.MainKt with your own class that has your main method
    manifest.attributes["Main-Class"] = "org.example.MainKt" 
}

Voila!, we’ve successfully integrated the ShadowJAR in our project. Next, let us build the jar file.

Navigate to the root>build>libs, becasue this is where you’ll find the file named `release.jar or whatever name you’ve set in the steps above.

What’s next?

THE OFFICIAL DOC: https://gradleup.com/shadow/introduction/ (please, RTFM)

Keep CODING!