没有找到原生android的实现

时间:2014-04-15 08:46:28

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

我搜索了很多,但没有找到为什么这个错误出现在我的项目中。 我做了一个简单的hello world类型项目。这是我的第一个项目。我按照

中的所有步骤操作

[http://code.google.com/p/awesomeguy/wiki/JNITutorial][1]

[1]:http://JNITutorial

错误:

            04-15 14:06:31.610: I/MainActivity(22766): onCreate
            04-15 14:06:31.610: W/dalvikvm(22766): No implementation found for native
Lcom/useofjniwithjava/MainActivity;.someFunction:(I)V
            04-15 14:06:31.610: W/dalvikvm(22766): threadid=1: thread exiting with uncaught exception (group=0x40ed8438)
            04-15 14:06:31.610: E/AndroidRuntime(22766): FATAL EXCEPTION: main
            04-15 14:06:31.610: E/AndroidRuntime(22766): java.lang.UnsatisfiedLinkError: Native method not found:
com.useofjniwithjava.MainActivity.someFunction:(I)V
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at com.useofjniwithjava.MainActivity.someFunction(Native Method)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at com.useofjniwithjava.MainActivity.onCreate(MainActivity.java:26)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.app.Activity.performCreate(Activity.java:5010)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2102)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.app.ActivityThread.access$600(ActivityThread.java:137)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.os.Handler.dispatchMessage(Handler.java:99)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.os.Looper.loop(Looper.java:137)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at android.app.ActivityThread.main(ActivityThread.java:4838)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at java.lang.reflect.Method.invokeNative(Native Method)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at java.lang.reflect.Method.invoke(Method.java:511)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            04-15 14:06:31.610: E/AndroidRuntime(22766):    at dalvik.system.NativeStart.main(Native Method)

example.c

            #include <jni.h>
            #include <stdio.h>
            #include <conio.h>
            #include <com_useofjniwithjava_MainActivity.h>

                JNIEXPORT jint JNICALL Java_com_useofjniwithjava_MainActivity_someFunction__(JNIEnv * env,
jobject  obj)
                {
                    return (jint)3;
                }

                JNIEXPORT void JNICALL Java_com_useofjniwithjava_MainActivity_someFunction__I(JNIEnv * env,
jobject  obj, jint x)
                {
                    int xx = x;
                    printf("%d\n",xx);
                    return;
                }
                JNIEXPORT void JNICALL Java_com_useofjniwithjava_MainActivity_someFunction__Ljava_lang_String_2(JNIEnv
* env, jobject  obj, jstring x)
                {
                    return;
                }

javah命令创建的头文件

com_useofjniwithjava_MainActivity.h

        /* DO NOT EDIT THIS FILE - it is machine generated */
            #include <jni.h>
            /* Header for class com_useofjniwithjava_MainActivity */

            #ifndef _Included_com_useofjniwithjava_MainActivity
            #define _Included_com_useofjniwithjava_MainActivity
            #ifdef __cplusplus
            extern "C" {
            #endif
            /*
             * Class:     com_useofjniwithjava_MainActivity
             * Method:    someFunction
             * Signature: ()I
             */
            JNIEXPORT jint JNICALL Java_com_useofjniwithjava_MainActivity_someFunction__
              (JNIEnv *, jobject);

            /*
             * Class:     com_useofjniwithjava_MainActivity
             * Method:    someFunction
             * Signature: (Ljava/lang/String;)V
             */
            JNIEXPORT void JNICALL Java_com_useofjniwithjava_MainActivity_someFunction__Ljava_lang_String_2
              (JNIEnv *, jobject, jstring);

            /*
             * Class:     com_useofjniwithjava_MainActivity
             * Method:    someFunction
             * Signature: (I)V
             */
            JNIEXPORT void JNICALL Java_com_useofjniwithjava_MainActivity_someFunction__I
              (JNIEnv *, jobject, jint);

            #ifdef __cplusplus
            }
            #endif
            #endif

Android.mk文件

            LOCAL_PATH:= $(call my-dir)

            include $(CLEAR_VARS)

            LOCAL_MODULE    := example
            LOCAL_CFLAGS    := -Werror
            LOCAL_SRC_FILES := example.c
            LOCAL_LDLIBS    := -llog 

            include $(BUILD_SHARED_LIBRARY)

MainActivityCode:我在这里调用这些原生方法。

            public class MainActivity extends Activity {

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

                public native int someFunction();

                public native void someFunction(String str);
                public native void someFunction(int x);

                private final String TAG = "MainActivity";

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
    //setting content view
                    setContentView(R.layout.activity_main);
                    Log.i(TAG, "onCreate");
                        //calling native method with argument int
                    someFunction(2);
                    Log.i(TAG, "void method called succesfully.");
                        //calling native method
                    int x = someFunction();
                    Log.i(TAG, "x = " + x);

                }
            }

0 个答案:

没有答案