android ndk开发-环境搭建

android ndk开发-环境搭建(1)

2011-5-11 11:34|发布者: annmax|查看: 3356|评论: 0

摘要: 开发环境:ubuntu 11.04 + Android NDK r5b + eclipse +ADT0.9.9android sdk 过程比较简单,这里就不介绍了。1、下载android-ndk r5b$wget http://dl.google.com/android/ndk/android-ndk-r5b-linux-x86.tar.bz22、 ...
开发环境: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项目:
Java代码 
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");  
    }  

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");
    }

5、到项目的bin目录下:
执行: javah -jni com.lql.Hello
会在该目录下生成一个.h文件:
com_lql_Hello.h 内容如下:
C代码 
/* DO NOT EDIT THIS FILE - it is machine generated */ 
#include   
/* 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 
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* 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
6、在该目录下编写一个com_lql_Hello.c文件,内容如下:
C代码 
#include   
#include   
JNIEXPORT jstring JNICALL Java_com_lql_Hello_hello  
  (JNIEnv *env, jobject thiz)  
{  
    return (*env)->NewStringUTF(env,"Hello JNI");  

#include
#include
JNIEXPORT jstring JNICALL Java_com_lql_Hello_hello
  (JNIEnv *env, jobject thiz)
{
return (*env)->NewStringUTF(env,"Hello JNI");
}
7、将com_lql_Hello.c拷贝到jni目录,同时在该目录下新建Android,mk文件,内容如下:
C代码 
LOCAL_PATH:= $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_MODULE    := hello  
LOCAL_SRC_FILES := com_lql_Hello.c  
include $(BUILD_SHARED_LIBRARY) 
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := hello
LOCAL_SRC_FILES := com_lql_Hello.c
include $(BUILD_SHARED_LIBRARY)
8、在终端下编译该项目:
$cd  hello/jni
$ndk-build
结果如下:
lql@lql-laptop:~/workspace/hello/jni$ ndk-build
Compile thumb  : hello <= com_lql_Hello.c
SharedLibrary  : libhello.so
Install        : libhello.so => libs/armeabi/libhello.so
9、编译之后,会在hello下面生成libs目录,该目录下包armeabi/libhello.so
在eclipse中刷新项目,然后在模拟器中运行:
会在activity中显示如下内容:
Hello JNI
到此,第一个ndk项目完成!

你可能感兴趣的:(android ndk开发-环境搭建)