Jar, Jni and .so files
Great question! This touches on the core of Android's native interface.
Let’s break this down step-by-step from the very basics to how .so files, jniLibs, and JNI work in an Android project — especially in the context of using a .jar file that relies on native code.
🔹 What is JNI?
JNI stands for Java Native Interface.
It is a bridge that allows Java/Kotlin code to call functions written in native code — typically C or C++.
This is useful when:
You need high performance (e.g., image processing, cryptography).
You want to reuse existing C/C++ libraries in your Android app.
You're interfacing with hardware or OS-level features.
🔹 What is a .so file?
.so file?.so= Shared Object file (Linux equivalent of.dllin Windows).It is a compiled binary that contains native (C/C++) functions.
Android apps use
.sofiles for native libraries.
For example:
libmynative.sois a native library your app might need to run some C/C++ code from Java/Kotlin using JNI.
🔹 What is jniLibs/?
jniLibs/?jniLibs/ is a special directory in Android projects where you put your native libraries (.so files).
Structure:
Each subfolder corresponds to a CPU architecture (ABI). Android supports multiple ABIs, including:
armeabi-v7a→ 32-bit ARMarm64-v8a→ 64-bit ARMx86→ Intel (emulators)x86_64→ 64-bit Intel
So you need to provide .so files for each ABI your app supports.
🔄 How JAR + .so Works with JNI
.so Works with JNILet’s say:
You import a JAR file:
libnativeclient.jarInside the JAR, it contains Java code like this:
This means:
System.loadLibrary("nativeclient")tells the JVM to loadlibnativeclient.sogetSerialNumber()is a native function implemented in C/C++
You must ensure:
The
.sofilelibnativeclient.sois present in your project’sjniLibs/<abi>/directory.The method is implemented in your
.cppfile (e.g.,nativeclient.cpp).
🔥 What Happens at Runtime
At app launch:
The app loads the class.
System.loadLibrary("nativeclient")executes → looks forlibnativeclient.soin:/data/app/<your.package>/lib/<abi>/Which comes from the
jniLibsdirectory during the build.
The native function
getSerialNumber()is now callable.
If the .so file is missing:
You’ll get this error:
🔧 How to Add .so Files to Your Project
.so Files to Your ProjectStep 1: Place .so files in the right folder
.so files in the right folderStep 2: Update build.gradle
build.gradleGradle picks up native libraries automatically from jniLibs, but make sure you don't strip them:
Optional (but recommended) in build.gradle:
✅ Summary Table
JNI
A bridge to call C/C++ code from Java/Kotlin
.so file
Shared library file with compiled native code
jniLibs/
Folder to place .so files for Android builds
System.loadLibrary("x")
Loads libx.so from native libs at runtime
Error if missing
UnsatisfiedLinkError: dlopen failed
Would you like a small working example of Java + native C/C++ JNI code in Android? Or how to link it with Gradle and verify loading works?
Last updated