Android NDK应用无法加载库

时间:2012-08-18 23:18:20

标签: android android-ndk java-native-interface

我的本​​机库代码:

#include <string.h>
#include <jni.h>

jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
  return env->NewStringUTF("Hello from native code!");
}

Android.mk:

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

LOCAL_MODULE    := libpacman
LOCAL_SRC_FILES := main.cpp
LOCAL_CFLAGS := -DANDROID_NDK
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

MainActivity.java:

public class MainActivity extends Activity {

    static {
        System.loadLibrary("libpacman");
    }

    // declare the native code function - must match main.cpp
    private native String invokeNativeFunction();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // this is where we call the native code
        String hello = invokeNativeFunction();

        new AlertDialog.Builder(this).setMessage(hello).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

库已成功编译,libpacman.so位于libs / armeabi目录中,但是logcat说:

E/AndroidRuntime(13060): FATAL EXCEPTION: main
E/AndroidRuntime(13060): java.lang.ExceptionInInitializerError
E/AndroidRuntime(13060):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(13060):    at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime(13060):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
E/AndroidRuntime(13060):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
E/AndroidRuntime(13060):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime(13060):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime(13060):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime(13060):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(13060):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(13060):    at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(13060):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(13060):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(13060):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(13060):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(13060):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(13060): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load libpacman: findLibrary returned null
E/AndroidRuntime(13060):    at java.lang.Runtime.loadLibrary(Runtime.java:365)
E/AndroidRuntime(13060):    at java.lang.System.loadLibrary(System.java:535)
E/AndroidRuntime(13060):    at com.example.pacman.MainActivity.<clinit>(MainActivity.java:11)
E/AndroidRuntime(13060):    ... 15 more
W/ActivityManager(  315):   Force finishing activity com.example.pacman/.MainActivity

所以,系统找不到库,但是如果解压APK文件 - 它就在lib目录中。为什么不在图书馆?

编辑。 好的,现在我修复了包名:

jstring Java_com_wiagames_pacman_MainActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
  return env->NewStringUTF("Hello from native code!");
}

Android.mk和MainActivity.java是一样的。套餐到处都是com.wiagames.pacman。但是我收到了错误:

E/AndroidRuntime(23084): FATAL EXCEPTION: main
E/AndroidRuntime(23084): java.lang.ExceptionInInitializerError
E/AndroidRuntime(23084):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(23084):    at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime(23084):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
E/AndroidRuntime(23084):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
E/AndroidRuntime(23084):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime(23084):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime(23084):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime(23084):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(23084):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(23084):    at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(23084):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23084):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(23084):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(23084):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(23084):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(23084): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load libpacman: findLibrary returned null
E/AndroidRuntime(23084):    at java.lang.Runtime.loadLibrary(Runtime.java:365)
E/AndroidRuntime(23084):    at java.lang.System.loadLibrary(System.java:535)
E/AndroidRuntime(23084):    at com.wiagames.pacman.MainActivity.<clinit>(MainActivity.java:13)
E/AndroidRuntime(23084):    ... 15 more
W/ActivityManager(  315):   Force finishing activity com.wiagames.pacman/.MainActivity

3 个答案:

答案 0 :(得分:4)

您的班级名称明显不是NdkFooActivity。还要确保您的java包是com.mindtherobot.samples.ndkfoo。本机函数的名称和您调用它的Java上下文必须同步。通常,您将使用javah -jni生成本地标头,该标头在已编译的类上运行,而不是java源。

答案 1 :(得分:4)

您传递给System.loadLibrary()的图书馆名称应该只是pacman,而不是libpacman

答案 2 :(得分:2)

根据我在NDK的经验,您在Android App中的课程路径必须与

相同
Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction



Java_com_mindtherobot_samples_ndkfoo [package name]
NdkFooActivity [activity or class name]
invokeNativeFunction [function name]