开发环境:ubuntu 11.04 + Android NDK r5b + eclipse +ADT0.9.9
android sdk 过程比较简单,这里就不介绍了。
1、下载android-ndk r5b
$wget http://dl.google.com/android/ndk/android-ndk-r5b-linux-x86.tar.bz2
2、将下载的ndk解压到某个目录:
$tar -xvf android-ndk-r5b-linux-x86.tar.bz2
3、修改当前用户的.profile,添加PATH
NDK=/home/lql/android-ndk-r5b
export NDK
PATH=$NDK:$PATH
export PATH
然后注销当前用户,重新登录,echo $PATH,看看PATH中有没有包含NDK
4、编写一个简单的例子
在eclipse里面新建一个hello android项目:
public class Hello extends Activity { public native String hello(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String hello = hello(); final TextView view = (TextView)findViewById(R.id.view); view.setText(hello); } static{ System.loadLibrary("hello"); } }
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_lql_Hello */ #ifndef _Included_com_lql_Hello #define _Included_com_lql_Hello #ifdef __cplusplus extern "C" { #endif /* * Class: com_lql_Hello * Method: hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_lql_Hello_hello (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
#include <jni.h> #include <string.h> JNIEXPORT jstring JNICALL Java_com_lql_Hello_hello (JNIEnv *env, jobject thiz) { return (*env)->NewStringUTF(env,"Hello JNI"); }
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello LOCAL_SRC_FILES := com_lql_Hello.c include $(BUILD_SHARED_LIBRARY)