Jar Files

A JAR file (Java ARchive) is a compressed package file format that bundles:

  • Compiled Java bytecode (.class files),

  • Resources (images, config files, etc.),

  • and metadata (like a MANIFEST.MF file),

into a single .jar file — similar to a .zip file — so that it can be distributed and reused easily.


🧠 What Exactly Does a JAR File Do?

A JAR file serves two primary purposes:

  1. Library or Dependency: It provides reusable code (like utility functions, SDKs, or frameworks) that you can use in other Java/Kotlin projects.

  2. Executable Application: It can be run directly with the java -jar command if it has a Main-Class specified in its manifest.


📦 Contents of a Typical JAR

Inside a JAR file, you’ll typically find:

com/
└── example/
    └── MyLibrary.class
resources/
└── config.properties
META-INF/
└── MANIFEST.MF

🔗 What Happens When You Import a JAR into an Android Kotlin Project?

✅ You Can:

  1. Reuse Precompiled Java/Kotlin Code

    • Example: SDKs from third parties (like payment SDKs, analytics, etc.).

    • Add .jar to libs/ folder → Gradle can include it in the build.

  2. Call Its Functions and Classes in Kotlin or Java

    • You can import and use classes from the JAR as if they were part of your app.

  3. Include Native Libraries (Optional) If the JAR uses JNI (.so files), and those .so files are in the right ABI folders (like armeabi-v7a), they can be packaged too.

  4. Access Resources (Sometimes) If the JAR uses properties/config files or assets and exposes methods to read them internally, you can use those as well.


❌ Limitations / Caveats in Android:

  1. Android Doesn't Use Java SE Fully

    • JARs relying on desktop Java APIs (like java.awt, javax.swing, or java.nio.file) won’t work.

    • Android has its own APIs and limited Java class support.

  2. No Resource Merging

    • Android projects use AAR files (Android ARchives) to include layouts, drawables, etc.

    • A JAR can’t provide Android resources (XML layouts, drawables) like an AAR can.

  3. Potential ProGuard/R8 Issues

    • JARs may get obfuscated or stripped if not configured in proguard-rules.pro.


🔧 How to Import a JAR in Android (Kotlin)

Step 1: Place the JAR

Place it in the libs/ directory (create if it doesn’t exist):

Step 2: Add in build.gradle (Module: app)

Step 3: Sync Gradle and Use

You can now import its classes:


🔮 Possibilities When You Import a JAR

Possibility
Description

✅ Use 3rd-party SDKs

Many older SDKs were distributed as .jar (like ZXing QR scanner, analytics SDKs, etc.)

✅ Share common business logic

Teams may write shared logic (e.g. validators, formatters) in .jar files

✅ Reduce duplication

Centralize code used in multiple apps

⚠️ JNI use

If the JAR relies on native libraries, you need matching .so files in jniLibs/

❌ No XML layouts

If it needs Android layouts, prefer .aar instead


🆚 JAR vs AAR (Android-specific)

Feature
JAR
AAR

Java/Kotlin Code

Android Resources (XML, images)

Android Manifest

Native Libraries (.so)

❌ (indirect)

Best for

Pure code libraries

Full Android libraries


Would you like a working example where we import a .jar in an Android Kotlin project and use a function from it?

Last updated