Android NDK-错误链接共享库和JNI包装器 [英] Android NDK - Error Linking Shared library and JNI Wrapper

查看:127
本文介绍了Android NDK-错误链接共享库和JNI包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将使用NDK-Standalone工具链生成的共享库链接到另一台构建机器上.然后使用特定的.so将其放到Android Studio中.从那里,我使用javah创建了jni.h文件,然后该文件帮助我为函数调用编写了.c JNI.

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.

遵循此示例 ndk-build确实可以编译并且似乎可以正常工作,但是尝试在电话上运行该应用程序时,我在

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")
}

说即使生成testLib.so并在libs/armeabi-v7a/testLib.so目录中也找不到testLib.so

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

当前错误:

 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)

我如何构建.so文件:

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

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

从那里,我使用javah编写了一个JNI包装器类,该类生成了testing_Android.h文件.从那里生成了我用来从testLib.c库调用函数的testing_Android.c JNI包装器:

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));

}

从那里我在Android的jni文件夹中有5个文件,这是我从以下位置运行ndk-build命令的地方:

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:

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:

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.

-干杯!

推荐答案

我不是在使用ndk独立工具链,而是在构建机器上使用了ndk-build,然后生成了我需要的libs文件.从那里,我添加了libs文件,其内容为:

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

进入src> main> jniLibs下的Android目录,然后存储所有文件,并将命令添加到我的应用程序build.gradle:

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'
    }

要使ndk-build正常工作,我必须包括jniWrapper.c文件.

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

Android.mk:

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(我正在测试全部",但不需要):

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.

这篇关于Android NDK-错误链接共享库和JNI包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆