一,開發環境配置
眾所周知,萬事開頭難,配置開發環境是所有開發項目最基本且必須要做的一步。
所用的所有軟體及開發平台:Win7+NetBeans+Vmware+ubuntu+NDK+android SDK
首先,在Win7平台下面配置好開發android的環境,具體細節參照部落格:http://blog.csdn.net/tianshuai11/article/details/7367700
然後,在Vmware中的ubuntu系統中下載android-ndk-r7b的NDK開發包http://developer.android.com/sdk/ndk/index.html 選擇Linux版本。下載後解壓到/usr檔案夾下面,接著配置環境變數:vi .bashrc 在最後添加:NDK=/usr/android-ndk-r7b export NDK---》然後讓更改生效:source
.bashrc---》最後在終端測試:echo $NDK 看是否輸出:/usr/android-ndk-r7b 如果輸出則代表配置成功
二,代碼編寫
在看接下來文章之前,希望你已經對Jni有所瞭解。如果不是很瞭解希望你能八這篇博文中的例子實現,http://blog.csdn.net/tianshuai11/article/details/7367881
1)在netBeans中建立工程:Project Name:hello ; Package Name:tianshuai.hello ;Activity Name :hello ;Target Platform 選 Android 2.3.3
2)在hello檔案中添加代碼如下:
import android.app.Activity;<br />import android.os.Bundle;<br />import android.widget.TextView;</p><p>public class hello extends Activity<br />{<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> TextView tv=(TextView)findViewById(R.id.tv);<br /> // TextView tv = new TextView(this);<br /> // tv.setText("sss");<br /> tv.setText(stringFromJNI());<br /> }<br /> public native String stringFromJNI();<br /> static {<br /> System.loadLibrary("hello");<br /> }<br />}<br /> main.xml中做相應更改
3)編譯C的標頭檔
右鍵->hello工程->產生,在hello\bin\classes\tianshuai\hello中產生 hello.class
然後在終端Dos視窗中 cd 到\hello\bin\classes 目錄下:輸入javah -classpath C:\Users\tianshuai\Desktop\hello\bin\classes -jni tianshuai.hello.hello
在classes檔案夾下會產生 tianshuai_hello_hello.h 檔案:
/* DO NOT EDIT THIS FILE - it is machine generated */<br />#include <jni.h><br />/* Header for class tianshuai_hello_hello */</p><p>#ifndef _Included_tianshuai_hello_hello<br />#define _Included_tianshuai_hello_hello<br />#ifdef __cplusplus<br />extern "C" {<br />#endif<br />/*<br /> * Class: tianshuai_hello_hello<br /> * Method: stringFromJNI<br /> * Signature: ()Ljava/lang/String;<br /> */<br />JNIEXPORT jstring JNICALL Java_tianshuai_hello_hello_stringFromJNI<br /> (JNIEnv *, jobject);</p><p>#ifdef __cplusplus<br />}<br />#endif<br />#endif<br />
PS:這裡假設你已經配置好java環境變數,具體參見博文http://blog.csdn.net/tianshuai11/article/details/7367700
4)手動更改工程
在hello工程的根目錄下建立 jni檔案夾,然後將tianshuai_hello_hello.h
檔案拷進去
添加介面實現檔案:tianshuai_hello_hello.c
#include <string.h><br />#include <jni.h><br />#include "tianshuai_hello_hello.h"<br />jstring<br />Java_tianshuai_hello_hello_stringFromJNI( JNIEnv* env,<br /> jobject thiz )<br />{<br /> return (*env)->NewStringUTF(env, "Hello from JNI !");<br />}<br /> 添加產生處理檔案:Android.mkLOCAL_PATH := $(call my-dir)</p><p>include $(CLEAR_VARS)</p><p>LOCAL_MODULE := hello<br />LOCAL_SRC_FILES := tianshuai_hello_hello.c</p><p>include $(BUILD_SHARED_LIBRARY)<br />
5)將所有以上工程及檔案儲存好,拷貝到vmware下的ubuntu 作業系統下面
cd /hello
./$NDK/ndk-build
在/hello/libs中會產生armeabi/libhello.so檔案
6)測試:
將Linux中更改好的工程檔案,拷貝到windows下,用Netbeans開啟,運行
三,總結
1)目標,通過java調用已經用C寫好的源檔案
2)步驟,1>已知寫好的c檔案
2>編寫java代碼,調用特定函數名的c檔案
3>將寫好的java代碼,編譯成.class檔案,然後產生 .h 檔案
4>ndk編譯一下 .h .c然後 通過java調用c目標實現
如果你經過千辛萬苦,看到以上結果的時候,相信你一定內牛滿面……
程式員的快樂就在這裡……
希望有什麼建議和意見給我留言,我一定積極聽取大家意見,並將博文疏漏之處改正。