2013-05-27 92 views
7

我是新的android ndk。ndk構建和.so文件創建android

我工作的這需要Java代碼以及C/C++代碼

因此,一個應用程序,爲我需要的Android NDK。

但我堅持在這一點上,我不能運行NDK建造這使得Java和C/C++之間的連接。

所以。請有人幫我解決這個問題。

我試過在Windows和Linux上,但得到同樣的錯誤。

我得到這個錯誤,當我使用ndk構建。

/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 

回答

21

首先你是因爲你沒有聲明參數是必須創建Java和C/C++之間的連接如收到此錯誤。

所以,我送你我的代碼爲您的問題

所有1.首先在Eclipse中創建Android項目。

  1. 創建項目文件夾點擊 - >點擊新建 - >然後文件夾並命名爲jni。

  2. 下JNI nameing再創建一個文件夾中包含。

  3. 創建java類。爲java類nameing-(MainActivity.java)

  4. 代碼 - >

    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. 開放命令提示或按窗口+ R。

  6. 移動到目錄 - (workspace->項目名稱 - > JNI - >包括)。

  7. 在此目錄下運行命令。

    javah -classpath <project-name>/bin/classes;<ANDROID_SDK_HOME>\platforms\android-<xx>\android.jar -o HelloJNI.h com.example.test.MainActivity 
    
  8. 此後我們可以在include文件夾下看到「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的使用蓬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. 創建命名的Android JNI下的新文件。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. 創建PROMT

在命令新文件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()); 
} 



}); 
} 
    } 
  • 運行NDK建造轉到項目目錄 - >然後,寫這個命令<android-ndk-directory>\ndk-build.cmd和點擊進入

    此後我們可以檢查.so文件nder obj文件夾

    1. xml file for NDKActivity。

      <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> 
      
  • +0

    感謝Vikram,它解決了我的問題 –

    +0

    @Vikram ...當我嘗試運行javah -classpath /bin/classes; \ platforms \ android- \ android.jar - HelloJNI.h com.example.test.MainActivity ......我在控制檯「main」中得到這個異常java.lang.NullPointerException \t at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source) \t at sun .launcher.LauncherHelper.checkAndLoadMain(Unknown Source) – DJhon

    +0

    不錯的答案!謝謝你,兄弟!!!只需要修改一次Gradle項目。在第7點,我們應該使用 \ build \ intermediates \ classes \ debug(或發佈)替換/bin/classes – GrafOrlov

    0

    看起來這是在.h文件和.cpp文件中的函數不匹配。 您已經提到了在.h文件中的某些參數,這些參數在native.cpp文件中的實現中缺少。所有的

    +0

    嘿格爾登,我搜索我的參數,但中號無法找到。 Thanx for reply –