NDK 配置

来源:互联网 发布:网络保密整改报告 编辑:程序博客网 时间:2024/05/17 08:49

 android NDK  Native Develpment Kit,是一套基于c和C++底层开发API的集成工具组件。

 

废话不多说。

首先配置好ECLIPSE +ADT

第二步是下载 Android NDK 这里用的版本是r5

第三部安装cygwin最好去官网上下载www.cygwin.com

找个近的服务器如:台湾的。

找到devel 改为install

shell 改为install

然后搜索 awk

开始安装

安装完成后找到cygwin cygwin/etc/defaults/etc/skel下的.bash_profile文件注意要用ultraedit打开,而且不要转换格式。否则据说会出错在最后一行添加 ANDROID_NDK_ROOT=/cygdrive/c/android-ndk-r5 (这里是我的安装路径)

export ANDROID_NDK_ROOT 

打开cygwin

输入cd $ANDROID_NDK_ROOT

回车后看到NDK的安装目录

输入命令:ls -a 会显示目录下的所有文件已验证目录配置正确。

 

eclipse下调试 hello-jni

选择new --> project --> android project -->

create project from existing source 选择hello-jni项目路径。

右键点击hello-jni.c 或者工程目录

hellojni-->Build path --> config build path--> Builders --> new

新建一个Program

配置

名称:

Main选项:

location 选择 cygwin安装目录/bin/bash.exe

working Directory : cygwin安装目录/bin

arguments: 

--login -c "cd /cygdrive/c/android-ndk-r5/samples/hello-jni && $ANDROID_NDK_ROOT/ndk-

build"

 

 

配置refresh选项:勾选 Refresh resources upon completion.

勾选:The entire workspace

勾选:Recursively include sub-folders

 

然后配置Build Options 选项:

勾选:Allocate Console (necessary for input)

Run the builder:

勾选:After a "Clean"

勾选:During manual builds

勾选:During auto builds

勾选:During a "Clean"

勾选:Specify working set of relevant resources

打开Specify Resources

特别要注意选择 specify resources 选择jni目录打钩。

上面配置无误会自动编译出libs/.so文件

 

android hello jni 转自android123

 

package com.example.hellojni;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;

public class HelloJni extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() ); //如果调用失败将会抛出 java.lang.UnsatisfiedLinkError异常
        setContentView(tv);
    } //注意下面的native关键字

    public native String  stringFromJNI(); 
    public native String  unimplementedStringFromJNI();

     static {
        System.loadLibrary("hello-jni"); //载入hello-jni库
    } 
}

需要注意的是这里必须设置SDK版本为1.5或以上版本即在androidmanifest.xml文件中指明<uses-sdk android:minSdkVersion="3" /> 这里我们看到JNI调用无需特殊的权限。

 而在Native C/C++中我们可以直接使用C++库,不过这里包含了jni这个头文件。

 #include <string.h>
#include <jni.h>

其中下面的返回类型jstring是VM String,在jni.h头文件中有定义

jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI, Android123 Test!");
}

其实很好理解的,仅仅是换个了别名而以,定义如下

typedef uint8_t         jboolean;       /* unsigned 8 bits */
typedef int8_t          jbyte;          /* signed 8 bits */
typedef uint16_t        jchar;          /* unsigned 16 bits */
typedef int16_t         jshort;         /* signed 16 bits */
typedef int32_t         jint;           /* signed 32 bits */
typedef int64_t         jlong;          /* signed 64 bits */
typedef float           jfloat;         /* 32-bit IEEE 754 */
typedef double          jdouble;        /* 64-bit IEEE 754 */
#else
typedef unsigned char   jboolean;       /* unsigned 8 bits */
typedef signed char     jbyte;          /* signed 8 bits */
typedef unsigned short  jchar;          /* unsigned 16 bits */
typedef short           jshort;         /* signed 16 bits */
typedef int             jint;           /* signed 32 bits */
typedef long long       jlong;          /* signed 64 bits */
typedef float           jfloat;         /* 32-bit IEEE 754 */
typedef double          jdouble;        /* 64-bit IEEE 754 */
#endif


typedef jint            jsize;

还有一些调用平时注意的声明

#define JNI_FALSE   0
#define JNI_TRUE    1

#define JNI_VERSION_1_1 0x00010001
#define JNI_VERSION_1_2 0x00010002
#define JNI_VERSION_1_4 0x00010004
#define JNI_VERSION_1_6 0x00010006

#define JNI_OK          (0)         /* no error */
#define JNI_ERR         (-1)        /* generic error */
#define JNI_EDETACHED   (-2)        /* thread detached from the VM */
#define JNI_EVERSION    (-3)        /* JNI version error */

#define JNI_COMMIT      1           /* copy content, do not free buffer */
#define JNI_ABORT       2           /* free buffer w/o copying back */

#define JNIIMPORT
#define JNIEXPORT
#define JNICALL