Jolopy
Jolopy

Reputation: 92

Android NDK - Error Linking Shared library and JNI Wrapper

I'm trying to link a Shared Library that I generated with the NDK-Standalone toolchain on a different build machine. Then using that specific .so I put it on Android Studio. From there I created a jni.h file using javah which then helped me write the .c JNI for the function calls.

Followed this example How do I compile any native (C, C++) library using NDK in the form of shared libraries .

ndk-build does compile and seems to work correctly but when trying to run the application on the phone I get a error at

static { 
    System.loadLibrary("testLib")
}

Saying that could not find testLib.so even though it is generated and in the libs/armeabi-v7a/testLib.so directory

CURRENT ERROR:

 01-31 14:41:53.779 19024-19024/com.jolopy.testing_02 E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.jolopy.testing_02, PID: 19024
        java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.jolopy.testing_02-2/base.apk"],nativeLibraryDirectories=[/data/app/com.jolopy.testing_02-2/lib/arm64, /vendor/lib64, /system/lib64]]] couldn't find "testLib.so"
            at java.lang.Runtime.loadLibrary(Runtime.java:367)
            at java.lang.System.loadLibrary(System.java:1076)
            at com.jolopy.testing_02.TestLib.<clinit>(TestLib.java:6)
            at com.jolopy.testing_02.MainActivity.onCreate(MainActivity.java:18)

How I built the .so file:

arm-linux-androideabi-gcc -c -fPIC testLib.c -o test.o

arm-linux-androideabi-gcc test.o -o testing.so

From there I wrote a JNI wrapper class using javah which generated the testing_Android.h file. Which from there generated the testing_Android.c JNI wrapper that I'm using to call functions from my testLib.c library:

#include "testLib.h"
//Including Machine Generated Header
#include "testing_Android.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_com_jolopy_testing_102_TestLib_testinglib_1Initialize
  (JNIEnv *env, jobject obj){
    (void)env;
    (void)obj;

    testing_Initialize();
}


JNIEXPORT jint JNICALL Java_com_jolopy_testing_102_TestLib_testinglib_1Get_1Count
  (JNIEnv *env, jobject obj){
    (void)env;
    (void)obj;

    return(testing_Get_Count());
}


JNIEXPORT jint JNICALL Java_com_jolopy_testing_102_TestLib_testinglib_1Get_1CurrentName
  (JNIEnv *env, jobject obj, jlong ptr, jint x){
    (void)env;
    (void)obj;

    return (testing_Get_CurrentName((char *)ptr , (int)x));

}

From there I have 5 files in the jni folder in Android which is where I run the ndk-build command from:

 testing.so | testing_Android.h | testing_Android.c | Application.mk | Android.mk

Android.mk:

LOCAL_PATH :=$(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := testLib
LOCAL_SRC_FILES := testing.so
LOCAL_EXPORT_C_INCLUDES := testing_Android.c
include $(PREBUILT_SHARED_LIBRARY)

Application.mk:

APP_PLATFORM := android-19
APP_ABI := armeabi-v7a

Any suggestions or faults in my progress that you might see that I don't would be greatly appreciated.

-Cheers!

Upvotes: 0

Views: 1460

Answers (2)

Jolopy
Jolopy

Reputation: 92

Instead of using the ndk standalone toolchain, I used the ndk-build on my build machine which then generated the libs file I needed. From there I added the libs file with the contents of:

  1. arm64-v8a
  2. armeabi-v7a
  3. x86
  4. x86_64

Into my Android directory under src>main>jniLibs which then stored all the files and just added the command to my app build.gradle:

sourceSets.main{
        jniLibs.srcDir 'src/main/jniLibs'
    }

For ndk-build to work properly I had to include the jniWrapper.c file.

Android.mk:

LOCAL_PATH :=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := testing
LOCAL_SRC_FILES := jniWrapper.c \
                    testLib.c
include $(BUILD_SHARED_LIBRARY)

Application.mk (I was testing with "all" but its unneeded):

APP_PLATFORM := android-23
APP_ABI := all

I know this is a work around solution but for some reason I could not figure out the toolchain to work correctly.

Upvotes: 0

shizhen
shizhen

Reputation: 12583

Change below

APP_ABI := armeabi-v7a 

to

APP_ABI := arm64-v8a

Because from your error logs, your device is arm64 ABI.


If you are starting a new Android NDK project, I would like to suggest you start from Android Studio + CMake tool chains, see here for my personal JniExample project based on Android Studio and CMake:

Upvotes: 2

Related Questions