7

我是android ndk的新手。

我正在开发一个需要 java 代码以及 c/c++ 代码的应用程序

所以,为此我需要android ndk。

但我停留在这一点上,我无法运行 ndk-build,它在 java 和 c/c++ 之间建立了连接。

所以。请有人帮我解决这个问题。

我在windows和linux上都试过了,但得到了同样的错误。

使用 ndk-build 时出现此错误。

/home/kamal/android-ndk-r8e/build/core/add-application.mk:128: Android NDK:      
Compile thumb : ndk <= native.c
jni/native.c: In function 'Java_com_example_demo_MainActivity_hello':  
jni/native.c:4:3: error: parameter name omitted
jni/native.c:4:3: error: parameter name omitted
jni/native.c:5:10: error: 'env' undeclared (first use in this function)
jni/native.c:5:10: note: each undeclared identifier is reported only once for each              function it appears in
jni/native.c: In function 'Java_com_example_demo_MainActivity_add':
jni/native.c:9:3: error: parameter name omitted
jni/native.c:9:3: error: parameter name omitted
jni/native.c:9:3: error: parameter name omitted
jni/native.c:9:3: error: parameter name omitted
jni/native.c:10:9: error: 'value1' undeclared (first use in this function) 
jni/native.c:10:18: error: 'value2' undeclared (first use in this function)
make: *** [obj/local/armeabi/objs/myjni/native.o] Error 1 
4

2 回答 2

21

首先,您收到此错误是因为您没有声明参数是必须在 java 和 c/c++ 之间创建连接。

所以,我正在向您发送我的代码以解决您的问题

1.首先在eclipse中创建android项目。

  1. 在项目下创建文件夹单击-> 单击新建-> 然后文件夹并将其命名为 jni。

  2. 在 jni 命名 include 下再创建一个文件夹。

  3. 创建java类。

  4. java类命名代码-(MainActivity.java)->

     package com.example.ndk;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
     public class MainActivity extends Activity {
    
     static {
         System.loadLibrary("myjni");
        }
    
    /**
    * Adds two integers, returning their sum
    */
    public native int add( int v1, int v2 );
    
    /**
    * Returns Hello World string
    */
    public native String hello();
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
    
        }
    
  5. 打开命令提示符或按 window+R。

  6. 移动到目录-(工作区-> 项目名称-> jni-> 包含)。

  7. 在这个目录下运行命令。

        javah -classpath <project-name>/bin/classes;<ANDROID_SDK_HOME>\platforms\android-<xx>\android.jar -o HelloJNI.h com.example.test.MainActivity
    
  8. 在此之后,我们可以在包含文件夹下看到“HelloJNI.h”文件。

  9. 检查“HelloJNI.h”中有这几行

    JNIEXPORT jint JNICALL Java_com_example_ndk_MainActivity_add(JNIEnv *, jobject, jint, jint);
    
    JNIEXPORT jstring JNICALL Java_com_example_ndk_MainActivity_hello (JNIEnv *, jobject);
    
  10. 在 jni 命名 test.c 下创建新文件(在此文件 test.c 中使用 pont 10 中的这 2 个点)

       #include <jni.h>
       #include "include/HelloJNI.h"
    
    JNIEXPORT jstring JNICALL Java_com_example_ndk_MainActivity_hello
        (JNIEnv *env, jobject javaThis) {
        return (*env)->NewStringUTF(env, "Hello");
    }
    
      JNIEXPORT jint JNICALL Java_com_example_ndk_MainActivity_add
          (JNIEnv *env, jobject javaThis, jint value1, jint value2){
    return (value1 + value2);
        }
    
  11. 在 jni 命名 Android.mk 下创建新文件

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE    := myjni       // from point 5 
     LOCAL_SRC_FILES := test.c     //from point 10 that we creare test.c
    
     include $(BUILD_SHARED_LIBRARY)
    
  12. 创建新文件 NDKActivity.java

      package com.example.ndk;
    
      import android.app.Activity;
      import android.view.View.OnClickListener;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;
    
      public class NDKActivity extends Activity{
    
      Button buttonCalc;
      TextView result;
      EditText value1,value2;
      /** Called when the activity is first created. */
      MainActivity nativeLib;
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
     nativeLib = new MainActivity();
      String helloText = nativeLib.hello();
    
    result = (TextView) findViewById(R.id.result);
    value1 = (EditText) findViewById(R.id.value1);
    value2 = (EditText) findViewById(R.id.value2);
    
    // Update the UI
    TextView outText = (TextView) findViewById(R.id.textOut);
    outText.setText(helloText);
    
     // Setup the UI
    buttonCalc = (Button)this.findViewById(R.id.buttonCalc);
    
    buttonCalc.setOnClickListener(new OnClickListener() {
    
    
    public void onClick(View v) {
     int v1, v2, res = -1;
     v1 = Integer.parseInt(value1.getText().toString().trim());
     v2 = Integer.parseInt(value2.getText().toString().trim());
    
     res = nativeLib.add(v1, v2);
     result.setText(new Integer(res).toString());
     }
    
    
    
     });
     }
         }
    
  13. 在命令提示符中运行 ndk-build

转到项目目录-> 然后,编写此命令<android-ndk-directory>\ndk-build.cmd并按 Enter

之后我们可以检查 obj 文件夹下的 .so 文件

  1. NDKActivity 的 xml 文件。

     <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Vikram"
    android:textSize="22sp"/>
    <TextView android:id="@+id/textOut"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Output"/>
    
    <EditText
    android:id="@+id/value1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Value 1"
    android:inputType="numberDecimal" />
    
     <TextView android:id="@+id/TextView01"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="+"
         android:textSize="36sp" />
    
          <EditText
       android:id="@+id/value2"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:hint="Value 2"
       android:inputType="numberDecimal" />
    
    <Button android:id="@+id/buttonCalc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="=" />
    <TextView android:id="@+id/result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="result"
       android:textSize="36sp" />
    
    
       </LinearLayout>
    
于 2013-05-27T10:57:32.817 回答
-1

这似乎是 .h 文件和 .cpp 文件中的功能不匹配。您在 .h 文件中的函数中提到了一些参数,这些参数在 native.cpp 文件中的实现中缺失。

于 2013-05-27T10:34:44.117 回答