2013-05-27 6 views
7

tôi mới trong android ndk.ndk xây dựng và .so tạo tập tin android

tôi đang làm việc trên một ứng dụng mà cần mã java cũng c/C++ mã

Vì vậy, cho rằng tôi cần ndk android.

Nhưng tôi bị kẹt vào thời điểm này mà tôi không thể chạy ndk-build tạo kết nối giữa java và c/C++.

So. xin vui lòng ai đó giúp tôi ra để giải quyết vấn đề này.

Tôi đã thử trên cả hai cửa sổ và linux nhưng có cùng lỗi.

Tôi gặp lỗi này khi tôi sử dụng 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 

Trả lời

21

Trước hết bạn nhận được lỗi này vì bạn không khai báo tham số là phải tạo conection giữa java và c/C++.

SO, tôi gửi cho bạn mã của tôi cho sự cố của bạn

1. Trước hết hãy tạo dự án android trong nhật thực.

  1. tạo thư mục theo nhấp chuột dự án -> nhấp mới -> rồi đến thư mục và đặt tên là jni.

  2. tạo một thư mục khác dưới tên jni bao gồm.

  3. tạo lớp java.

  4. mã cho lớp java nameing- (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. mở command prompt hoặc nhấn cửa sổ + R.

  6. chuyển đến thư mục- (không gian làm việc-> tên dự án -> jni -> bao gồm).

  7. lệnh chạy trong thư mục này.

    javah -classpath <project-name>/bin/classes;<ANDROID_SDK_HOME>\platforms\android-<xx>\android.jar -o HelloJNI.h com.example.test.MainActivity 
    
  8. sau này chúng ta có thể thấy tệp "HelloJNI.h" trong thư mục bao gồm.

  9. kiểm tra "HelloJNI.h" có những dòng này trong đó

    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. tạo file mới dưới JNI đặt tên test.c (sử dụng này 2 điểm trong pont 10 trong tập tin test.c này)

    #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. tạo tệp mới dưới tên jni đặt tên 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. tạo các tập tin mới 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. chạy ndk-build trong lệnh Promt

đi đến dự án Danh bạ> sau đó, viết lệnh này <android-ndk-directory>\ndk-build.cmd và nhấn enter

sau khi chúng tôi có thể kiểm tra .so tệp u nder obj folder

  1. tệp xml cho 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

cảm ơn Vikram, nó đã giải quyết được vấn đề của tôi –

+0

@Vikram ... khi tôi đang cố gắng chạy javah-classpath /bin/classes; \ platforms \ android- \ android.jar - o HelloJNI.h com.example.test.MainActivity ...... Tôi nhận được ngoại lệ này trong giao diện điều khiển "chính" java.lang.NullPointerException \t tại sun.launcher.LauncherHelper.getMainClassFromJar (Nguồn không xác định) \t lúc mặt trời .launcher.LauncherHelper.checkAndLoadMain (Nguồn không xác định) – DJhon

+0

câu trả lời hay! cảm ơn bạn!!! chỉ là một sửa chữa cho dự án gradle. trong điểm 7 chúng ta nên substitude /bin/classes với \ build \ intermediates \ classes \ debug (hoặc release) – GrafOrlov

0

Có vẻ như đây là chức năng so khớp sai trong tệp .h và tệp .cpp. Bạn đã đề cập một số tham số trong các hàm trong tệp .h bị thiếu trong triển khai trong tệp native.cpp.

+0

Hey Kirti, tôi đã tìm kiếm các thông số của tôi, nhưng m không thể tìm thấy. Thanx trả lời –