找不到JNI so文件

时间:2012-07-30 12:55:11

标签: android java-native-interface

我想用一些代码创建jni示例,但它在logcate中显示错误

07-30 18:34:48.720: W/dalvikvm(3484): Exception Ljava/lang/UnsatisfiedLinkError; thrown during Lcom/org/HelloNDK/HelloNDK;.<clinit>
07-30 18:34:48.740: W/dalvikvm(3484): Class init failed in newInstance call (Lcom/org/HelloNDK/HelloNDK;)
07-30 18:34:48.740: D/AndroidRuntime(3484): Shutting down VM
07-30 18:34:48.760: W/dalvikvm(3484): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-30 18:34:48.810: E/AndroidRuntime(3484): FATAL EXCEPTION: main
07-30 18:34:48.810: E/AndroidRuntime(3484): java.lang.ExceptionInInitializerError
07-30 18:34:48.810: E/AndroidRuntime(3484):     at java.lang.Class.newInstanceImpl(Native Method)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at java.lang.Class.newInstance(Class.java:1429)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at android.os.Looper.loop(Looper.java:123)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at java.lang.reflect.Method.invoke(Method.java:521)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at dalvik.system.NativeStart.main(Native Method)
07-30 18:34:48.810: E/AndroidRuntime(3484): Caused by: java.lang.UnsatisfiedLinkError: Library libndkfoo not found
07-30 18:34:48.810: E/AndroidRuntime(3484):     at java.lang.Runtime.loadLibrary(Runtime.java:461)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at java.lang.System.loadLibrary(System.java:557)
07-30 18:34:48.810: E/AndroidRuntime(3484):     at com.org.HelloNDK.HelloNDK.<clinit>(HelloNDK.java:12)
07-30 18:34:48.810: E/AndroidRuntime(3484):     ... 15 more

代码是:

主要活动(java文件)

package com.org.HelloNDK;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

public class HelloNDK extends Activity {
    /** Called when the activity is first created. */

      static {
            System.out.println("Lib 1");
            System.loadLibrary("libndkfoo");
            System.out.println("Lib 2");
          }

      private native String invokeNativeFunction();

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

        try {

             // this is where we call the native code
            String hello = invokeNativeFunction();
            new AlertDialog.Builder(this).setMessage(hello).show();

        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("Error in On Create == >"+e.toString());
        }
    }
}

Android.mk文件代码

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_MODULE    := ndkfoo
LOCAL_SRC_FILES := ndkfoo.c

include $(BUILD_SHARED_LIBRARY)

ndkfoo.c文件代码

#include <string.h>
#include <jni.h>
JNIEXPORT jstring JNICALL Java_org_pielot_hellondk_HelloNDK_sayHello(JNIEnv *env, jobject obj) 
{
     return (*env)->NewStringUTF(env, "Hello from native code!");
}

使用

创建.SO文件
infoware@infoware-PC /cygdrive/d/SecondWorkspace/HelloNDK
$  /cygdrive/d/android-ndk-r8b/ndk-build
Compile thumb  : ndkfoo <= ndkfoo.c
SharedLibrary  : libndkfoo.so
Install        : libndkfoo.so => libs/armeabi/libndkfoo.so

结构的项目屏幕

enter image description here

1 个答案:

答案 0 :(得分:1)

  1. 在您的活动中:重命名private native String invokeNativeFunction();
    private native String sayHello();
  2. 你的c文件中的
  3. :重命名
    JNIEXPORT jstring JNICALL Java_org_pielot_hellondk_HelloNDK_sayHello(JNIEnv *env, jobject obj)

    JNIEXPORT jstring JNICALL Java_com_org_hellondk_HelloNDK_sayHello(JNIEnv *env, jobject obj)