您的位置:首页 > 移动开发 > Android开发

Android之NDK开发

2015-06-11 21:03 567 查看
NDK全称:Native Development Kit。

一、NDK产生的背景

Android平台从诞生起,就已经支持C、C++开发。众所周知,Android的SDK基于Java实现,这意味着基于Android SDK进行开发的第三方应用都必须使用Java语言。但这并不等同于“第三方应用只能使用Java”。在Android SDK首次发布时,Google就宣称其虚拟机Dalvik支持JNI编程方式,也就是第三方应用完全可以通过JNI调用自己的C动态库,即在Android平台上,“Java+C”的编程方式是一直都可以实现的。

不过,Google也表示,使用原生SDK编程相比Dalvik虚拟机也有一些劣势,Android SDK文档里,找不到任何JNI方面的帮助。即使第三方应用开发者使用JNI完成了自己的C动态链接库(so)开发,但是so如何和应用程序一起打包成apk并发布?这里面也存在技术障碍。比如程序更加复杂,兼容性难以保障,无法访问Framework API,Debug难度更大等。开发者需要自行斟酌使用。

于是NDK就应运而生了。NDK全称是Native Development Kit。

NDK的发布,使“Java+C”的开发方式终于转正,成为官方支持的开发方式。NDK将是Android平台支持C开发的开端。

二、为什么使用NDK

1.代码的保护。由于apk的java层代码很容易被反编译,而C/C++库反汇难度较大。

2.可以方便地使用现存的开源库。大部分现存的开源库都是用C/C++代码编写的。

3.提高程序的执行效率。将要求高性能的应用逻辑使用C开发,从而提高应用程序的执行效率。

4.便于移植。用C/C++写得库可以方便在其他的嵌入式平台上再次使用。

三、NDK简介

1.NDK是一系列工具的集合

NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk。这些工具对开发者的帮助是巨大的。

NDK集成了交叉编译器,并提供了相应的mk文件隔离CPU、平台、ABI等差异,开发人员只需要简单修改mk文件(指出“哪些文件需要编译”、“编译特性要求”等),就可以创建出so。

NDK可以自动地将so和Java应用一起打包,极大地减轻了开发人员的打包工作。

2.NDK提供了一份稳定、功能有限的API头文件声明

Google明确声明该API是稳定的,在后续所有版本中都稳定支持当前发布的API。从该版本的NDK中看出,这些API支持的功能非常有限,包含有:C标准库(libc)、标准数学库(libm)、压缩库(libz)、Log库(liblog)。

四、NDK开发环境的搭建

1.下载安装NDK

下载NDK压缩包,至于从哪里下载借助搜索引擎吧。



2.解压NDK压缩包,配置环境变量。将解压的地址写入环境变量PATH中(win7环境变量设置步骤:我的电脑--右键--属性--高级系统设置--环境变量--系统变量名Path,变量值ndk包完整路径)



3.在命令提示符下输入ndk-build如果弹出如下的错误,而不是说ndk-build not found,就说明ndk环境已经安装成功了。

Android NDK: Could not find application project directory !    

Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.    

/home/braincol/workspace/android/android-ndk-r5/build/core/build-local.mk:85: *** Android NDK: Aborting    .  Stop.



4.配置Eclipse

打开Eclipse,点Window--Preferences--Android--NDK,设置NDK路径,其为刚刚解压缩的工具包路径(例如:D:\software\Android\android-ndk-r9d)



5.运行安装包自带的Hello-jni测试





五、开发自己的NDK程序

1.打开Eclipse,新建一个Android工程

2.工程右键--Android Tools--Add Native Support...--输入lib.so文件名

这时候工程就会多一个jni的文件夹,jni下有Android.mk和**.cpp文件。Android.mk是NDK工程的Makefile,**.cpp就是NDK的源文件。

3.添加Application.mk文件到jni文件夹中(APP_ABI := all)



4.根据需要修改Android.mk

多个源文件使用空格隔开,例如:LOCAL_SRC_FILES := test.c NdkTest.cpp



5.工程右键--Properties--C/C++ Build--

A:Environment--Add--Name:NDKROOT,Value:NDK路径名(D:\software\Android\android-ndk-r9d)



B:Builder Settings--去掉 Use default build command, Build command输入:${NDKROOT}/ndk-build.cmd 



6.编写C/C++文件

函数名比较长但是完全按照:java_pacakege_class_mathod 形式来命名

7.在主程序申明并调用该函数

说明:

static:表明程序开始运行的时候会加载该NDK, static区声明的代码会先于onCreate方法执行。

native:这个关键字表示这个方法是本地方法,也就是说这个方法是通过本地代码(C/C++)实现的,在java代码中仅仅是声明。

8.结构布局



9.相关代码

Application.mk

APP_ABI := all


Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := AndroidNdkDemo
LOCAL_SRC_FILES := test.c AndroidNdkDemo.cpp

include $(BUILD_SHARED_LIBRARY)


test.c

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

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

jlong
Java_com_androidndkdemo_MainActivity_add1( JNIEnv* env, jobject thiz, jlong x, jlong y)
{
jlong value = x+y;
return x+y;
}


AndroidNdkDemo.cpp

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

extern "C"
jstring
Java_com_androidndkdemo_MainActivity_stringFromJNI2( JNIEnv* env,jobject thiz )
{
//	return (*env)->NewStringUTF(env, "Hello from JNI !");

//	NDK plugin默认为我们生成的是cpp文件,而C与C++调用函数的参数不一致,因此找不到函数,具体参考jni.h中的定义
//	cpp文件中形如(*env)->Method(env, XXX)改成env->Method(XXX)即可。
return env->NewStringUTF("Hello from JNI !");
}

extern "C"
jlong
Java_com_androidndkdemo_MainActivity_add2( JNIEnv* env, jobject thiz, jlong x, jlong y)
{
jlong value = x+y;
return x+y;
}


ndk_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="字符串1"/>

<View
android:layout_width="fill_parent"
android:layout_height="10dip"/>

<TextView
android:id="@+id/tv02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="字符串2"/>

<View
android:layout_width="fill_parent"
android:layout_height="10dip"/>

<TextView
android:id="@+id/tv03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加法1"/>

<View
android:layout_width="fill_parent"
android:layout_height="10dip"/>

<TextView
android:id="@+id/tv04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加法2"/>
</LinearLayout>


MainActivity.java

package com.androidndkdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity
{
private TextView tv01 = null;
private TextView tv02 = null;
private TextView tv03 = null;
private TextView tv04 = null;

private Button btn01 = null;
private Button btn02 = null;
private Button btn03 = null;
private Button btn04 = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ndk_layout);

tv01 =  (TextView)findViewById(R.id.tv01);
tv02 =  (TextView)findViewById(R.id.tv02);
tv03 =  (TextView)findViewById(R.id.tv03);
tv04 =  (TextView)findViewById(R.id.tv04);

btn01 = (Button)findViewById(R.id.btn01);
btn02 = (Button)findViewById(R.id.btn02);
btn03 = (Button)findViewById(R.id.btn03);
btn04 = (Button)findViewById(R.id.btn04);

btn01.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String string = stringFromJNI1();
tv01.setText(string);
}
});

btn02.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String string = stringFromJNI2();
tv02.setText(string);
}
});

btn03.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
double randomDouble = Math.random();
long x = Math.round(randomDouble*100);
randomDouble = Math.random();
long y = Math.round(randomDouble*100);
long z = add1(x,y);
String string = x+"+"+y+"="+z;
tv03.setText(string);
}
});

btn04.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
double randomDouble = Math.random();
long x = Math.round(randomDouble*100);
randomDouble = Math.random();
long y = Math.round(randomDouble*100);
long z = add2(x,y);
String string = x+"+"+y+"="+z;
tv04.setText(string);
}
});
}

public native String stringFromJNI1();
public native String stringFromJNI2();
public native long add1(long x, long y);
public native long add2(long x, long y);
static {
System.loadLibrary("AndroidNdkDemo");
}
}


10.运行程序,查看效果



源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android NDK开发