Android NDK模拟器崩溃

时间:2014-09-24 21:40:09

标签: android android-ndk

我是Android新手,尤其是NDK。我尝试加载像hello-jni这样的样本并且它工作得很好。但是,当我尝试编写自己的代码时,当我使用ndk-build命令构建它时,它没有任何问题。但是当我尝试从我的java代码中调用该类并对其进行编译时,它会在模拟器上崩溃。我不知道是什么问题,因为我所做的是从hello-jni样本中复制代码并将其替换为我的代码,但它无法正常工作。我真的需要你的帮助。

我的Android.mk

    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)
    LOCAL_MODULE    := AndroidProject
    LOCAL_SRC_FILES := AndroidProject.c

    include $(BUILD_SHARED_LIBRARY)

我的AndroidProject.c

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

   int s_ButtonPressCounter = 0;

   jstring
   Java_com_example_AndroidProject_AndroidProject_invokeNativeFunction()( JNIEnv* env,
                                              jobject thiz )
   {
    char szBuf[512];
    sprintf(szBuf, " You have pressed this huge button %d times", s_ButtonPressCounter++);

   jstring str = (*env)->NewStringUTF(env, szBuf);
   return str;
   }

我的AndroidProject.java

  package com.example.androidproject;

  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;

  public class AndroidProject extends Activity {


  public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* Create a Button and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( invokeNativeFunction() );
        setContentView(tv);

    }

    public native String  invokeNativeFunction();

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

    }

Logcat:

这是LogCat:

09-24 22:19:00.369: E/Trace(823): error opening trace file: No such file or directory (2)
09-24 22:19:00.819: D/dalvikvm(823): Trying to load lib /data/app- lib/com.example.androidproject-2/libndktest.so 0x40cdf458
09-24 22:19:00.839: D/dalvikvm(823): Added shared lib /data/app-lib/com.example.androidproject-2/libndktest.so 0x40cdf458
09-24 22:19:00.839: D/dalvikvm(823): No JNI_OnLoad found in /data/app-lib/com.example.androidproject-2/libndktest.so 0x40cdf458, skipping init
09-24 22:19:01.529: W/dalvikvm(823): No implementation found for native Lcom/example/androidproject/AndroidProject;.invokeNativeFunction:()Ljava/lang/String;
09-24 22:19:01.529: D/AndroidRuntime(823): Shutting down VM
09-24 22:19:01.539: W/dalvikvm(823): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-24 22:19:01.559: E/AndroidRuntime(823): FATAL EXCEPTION: main
09-24 22:19:01.559: E/AndroidRuntime(823): java.lang.UnsatisfiedLinkError: Native method not found: com.example.androidproject.AndroidProject.invokeNativeFunction:()Ljava/lang/String;
09-24 22:19:01.559: E/AndroidRuntime(823):  at com.example.androidproject.AndroidProject.invokeNativeFunction(Native Method)
09-24 22:19:01.559: E/AndroidRuntime(823):  at com.example.androidproject.AndroidProject.onCreate(AndroidProject.java:27)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.app.Activity.performCreate(Activity.java:5104)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.os.Looper.loop(Looper.java:137)
09-24 22:19:01.559: E/AndroidRuntime(823):  at android.app.ActivityThread.main(ActivityThread.java:5041)
09-24 22:19:01.559: E/AndroidRuntime(823):  at java.lang.reflect.Method.invokeNative(Native Method)
09-24 22:19:01.559: E/AndroidRuntime(823):  at java.lang.reflect.Method.invoke(Method.java:511)
09-24 22:19:01.559: E/AndroidRuntime(823):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-24 22:19:01.559: E/AndroidRuntime(823):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-24 22:19:01.559: E/AndroidRuntime(823):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

基于你的logcat:

09-24 22:19:01.559: E/AndroidRuntime(823): java.lang.UnsatisfiedLinkError: Native method not found:
com.example.androidproject.AndroidProject.invokeNativeFunction:()Ljava/lang/String;

首先,看起来invokeNativeFunction是您之前项目的复制粘贴/错误。由于您没有使用它,请删除其声明。

其次,看起来您需要在已编译的Java代码上运行javah以生成具有正确函数名称的标头。为此:

  1. 执行 Build All 让Java生成类文件

  2. 打开终端

  3. 转入您的bin目录

  4. 在AndroidProject上运行javah

    javah -o ../jni/AndroidProject.h classes/com/example/AndroidProject/AndroidProject
    
  5. AndroidProject.h

  6. 中加入AndroidProject.c
  7. 执行 全部构建 以确保构建和打包新的共享对象

答案 1 :(得分:0)

您的Java需要C函数名称

Java_com_example_androidproject_AndroidProject_invokeNativeFunction

因此它看起来像一个简单的命名问题。