How to use JNI with AAR library?

I am creating an Android library (.aar file) and I need to use JNI. (I am very well aware that Google does not encourage the use of JNI / NDK, if possible, but in this case it is not possible).

I started with a separate hello-jni APP example (for the first study of JNI) with the following files:

HelloJni.java

public class HelloJni extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText( stringFromJNI() ); setContentView(tv); } public native String stringFromJNI(); static { System.loadLibrary("hello-jni"); } } 

Android.mk

 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FILES := hello-jni.c include $(BUILD_SHARED_LIBRARY) 

Application.mk

 APP_ABI := all 

Hi-jni.c

 #include <string.h> #include <jni.h> jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { return (*env)->NewStringUTF(env, "Hello from JNI!"); } 

The following builds are great as an application (apk), and I can run it on my device, which prints "Hello from JNI!". as was expected.

Now I have done the same, but instead of apk, I created a library project to create aar. I saved all the files the same except HelloJni.java, which I changed to the following:

HelloJni.java

 public class HelloJni { public native String stringFromJNI(); static { System.loadLibrary("hello-jni"); } } 

Aar builds fine, but when I import aar into a separate application project and try to run it on my device, it crashes when the application starts, and I get the following logcat error message:

com.test.sample.mysampleapplication E / AndroidRuntime: FATAL EXCEPTION: main Process: com.test.sample.mysampleapplication, PID: 20047 java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader [DexPathList [[zip file] "/ data / app / com.test.sample.mysampleapplication-1 / base.apk "], nativeLibraryDirectories = [/ data / app / com.test.sample.mysampleapplication-1 / lib / arm, / vendor / lib, / system / lib] ]] could not find "libhello-jni.so" in java.lang.Runtime.loadLibrary (Runtime.javahaps66) in java.lang.System.loadLibrary (System.java:988) ...

What in the world is this file "libhello-jni.so"? And why do I need this? I was able to handle it perfectly as an apk. But can anyone explain why this does not work when I do this in aar and import it into the application project in order to use it? Did I skip some of the extra steps needed to contribute to the library (and use it)? Thanks!

EDIT:

This is how I imported my aran into my application. 1. Click "File" → "Create" → "New Module". 2. Select "Import .JAR or .AAR Package" as the module type. 3. Define my aar file as a new module. 4. And then open “File” → “Project Structure”, 5. On the “Dependencies” tab, add “module dependency” and select my aar file. If this is not a good way to import aar into my application, then please let me know . Thanks!

+4
source share
2 answers

Turns out I had to add some NDK configuration to the build.gradle file inside my AAR modules directory.

build.gradle:

 android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0" // This determines the .so filename ndk { moduleName "hello-jni" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } 

Not having what is added to build.gradle, you will get a .so file that defaults to the name of your project, in my case "libsample-aar.so". With this configuration above, the generated .so file will then be "libhello-jni.so".

+2
source

You must create your own code using the ndk-build command from the Android NDK.

This process will generate linhello-jni.so, which will be included in your assembly.

0
source

All Articles