寫教學的最大目的是教會未來的自己

在Eclipse透過JNI 跑C/C++ for android-Hello_world篇

繼上一篇(http://www.pupuliao.info/2013/05/在eclipse透過jni-跑cc-for-android-基礎安裝篇/) 安裝好後

要開始進行測試和設定,這部分比較麻煩,(在這裡我卡了很久)

因為是事後結圖,所以有些地方跳過

 

  1. 先開啟一個android專案
    2013-05-26_165735

     

     

     

  2. 在專案上右鍵選擇 Android Tools->add Native Support
    2013-05-26_170111
  3. 就會要你輸入so 名稱
    2013-05-26_170346
  4. 確認後就會產生jni 和lib 資料夾
    2013-05-26_170358
  5. 其中Android.mk 是 編譯的設定檔,包括要編譯哪些東西,現在用預設的就可以了
    LOCAL_PATH := $(call my-dir) 
    include $(CLEAR_VARS) 
    LOCAL_MODULE := HelloNDK 
    LOCAL_SRC_FILES := HelloNDK.cpp 
    include $(BUILD_SHARED_LIBRARY
  6. 之後開始編輯我們的JAVA 程式
     

     

     

    package com.example.hellondk;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    	private TextView testString;
    	static
    
    	{
    
    		System.loadLibrary("HelloNDK");
    
    	}
    
    	public native String helloString();
    
    	public native int plus(int a, int b);
    
    	public native int multiply(int a, int b);
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		testString = (TextView) findViewById(R.id.test);
    		String tempString;
    		tempString = helloString() + "\n3+5=" + String.valueOf(plus(3, 5))
    				+ "\n3*5=" + String.valueOf(multiply(3, 5));
    		testString.setText(tempString);
    		// testString.setText(helloString());
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		getMenuInflater().inflate(R.menu.activity_main, menu);
    		return true;
    	}
    }

      其中所有用native 宣告的function 都是等一下要透過C/C++ 來撰寫的程式,所以只需要宣告不需要寫code

  7. 打開剛剛系統生成的在jni 下的cpp檔
    #include <jni.h>
    #include <string.h>
    
    extern "C"
    {
    JNIEXPORT jstring Java_com_example_hellondk_MainActivity_helloString(JNIEnv* env,
    		jobject thiz) {
    	return env->NewStringUTF("My first JNI!");
    }
    JNIEXPORT jint Java_com_example_hellondk_MainActivity_plus(JNIEnv* env,
    		jobject thiz, jint a, jint b) {
    	jint total = a + b;
    
    	return total;
    }
    JNIEXPORT jint Java_com_example_hellondk_MainActivity_multiply(JNIEnv* env,
    		jobject thiz, jint a, jint b) {
    	jint total = a * b;
    	return total;
    }
    
    }
    

    先簡單說明我搞懂的部分
    首先因為檔案是使用cpp 所以當在裡面寫C code 的時候 要把程式碼用extern "C"{…} 包起來,如果是.C檔就不需要,不過寫法部台一樣 這部分請自行翻資料
    再來是資料型態的部分,所有JAVA的基本變數名稱 都是j+原先變數
    例如 int->jint 這部分在http://j796160836.pixnet.net/blog/post/31583827-%5Bandroid%5D-安裝ndk與使用jni呼叫系統底層native的c- 有更近一步的說明
    另外在function 名稱的命名上就是 JAVA_[package名稱]_[class名稱]_[function名稱] 所有的 . 改成 _
    在function () 中的變數加上預設的兩個變數,這應該是用來在JAVA 和C之間的變數轉換使用

  8. 接下來開始麻煩的設定
  9. 專案上按右鍵->內容->建制器->新建->程式->確定
    2013-05-26_173313
  10. 依照參考下圖設定,其中名稱隨意,位置和工作目錄就是你安置裝cygwin 的位置,其中引數的內容是重點
    –login -c "cd [專案位置] &&  rm -r obj && $NDK/ndk-build" 
    要注意的是 位置的斜線和windows 提供的是相反的 請注意
    這短程式碼簡單說明就是,移動到專案->刪除前一次編譯資料->開始編譯 ,其中&&  rm -r obj 是我自己加進去的

    2013-05-26_173713

  11. 接下來請參考這兩張圖設定
    2013-05-26_1742402013-05-26_174220
  12. 這樣設定後 每次修改後存檔系統將會自動編譯
  13. 之後存檔後出現下圖,那就成功了,如果跳出錯誤訊息….去翻資料吧,看看gcc 編譯錯誤說明
    2013-05-26_174427
  14. 開始測試吧
    2013-05-26_174556

Post to Twitter Post to Plurk Post to Facebook Send Gmail

4 Responses to 在Eclipse透過JNI 跑C/C++ for android-Hello_world篇

發表迴響

Copyright © 2024. All Rights Reserved.

歡迎光臨
初音