Recent Changes‎ > ‎

Using the NDK plugin

postedJul 2, 2012, 7:16 PMby Siva Velusamy
ADT 20 includes an NDK plugin that provides support for building and debugging NDK projects in Eclipse. This document describes how to install and use the NDK plugin.
Installation
The NDK plugin currently works with CDT 7.0.2 or CDT 8.0.2.
  1. Download Eclipse for Java.
  2. Install CDT from Eclipse update sitehttp://download.eclipse.org/tools/cdt/releases/indigo.
  3. Install Android SDK + NDK Plugins from Eclipse update sitehttps://dl-ssl.google.com/android/eclipse/
Using the NDK Plugin

1. First set the path to SDK and NDK:

Eclipse -> Window -> Preferences -> Android -> set path to SDK

Eclipse -> Window -> Preferences -> Android -> NDK -> set path to the NDK


2. Right click on an Android project and select Android Tools -> Add native support.
Note that you will not be able to add native support if the project already has C/C++ nature.

At this point, you will be able to build your applications using Project -> Build All.

Debugging native applications

1. Update your build config to include “NDK_DEBUG = 1”.

Right click project -> properties -> C/C++ Build:
2. Set a breakpoint in your C code.
3. Right click on your project, select Debug As -> Android Native Application

Note: There is a delay of a few seconds between when the activity is launched and when native debugging starts. If your code is already executed by that point, then you won’t see the breakpoint being hit. So either put a breakpoint in code that is called repetitively, or make sure that you call JNI code after you see that ndk-gdb has connected.

Known Issues

1. Eclipse does not automatically find the include paths to all the NDK headers on Windows. This issue will be fixed in the next update (20.0.1) when it is released.
2. Eclipse does not automatically find the include paths with CDT 8.1.0 (Juno). This issue is tracked in Bug33788.

原文地址:http://tools.android.com/recent/usingthendkplugin

用上面的方法配置好后,每次编译都会提示错误:make: *** empty variable name. Stop.

查了n久,只是知道这个错误是ndk不识别路径中‘ ’和‘.’符号,但是各种编译路径都修改后,还是会出现。

最后,终于发现是ndk的命令当中有问题,添加调试标志的时候,NDK_DEBUG=1 等号左右也不能有空格,把空格去掉好了~~~


好了,结局是好的,NDK还是越来越好用了~



最近一段时间在做native层的开发,把一些经验和技巧记录下来,希望对大家有帮助。本教程以step-by-step的形式给正处入门阶段的native开发新手提供指引。


导言:在进行Android开发的过程中,在一些对性能要求较高的场景,例如图像处理,视音频编解码等,需要使用到native代码以提高运行效率。本教程将在native层进行加法运算和字符串连接,通过这个简单的例子阐述使用eclipse编译运行ndk代码的过程。

注:JNI基础知识不在本文的讨论范围之内,推荐浏览oracle的帮助文档进行系统学习


开发环境:

Adt-bundle(ver:21.1.0) 下载地址:https://developer.android.com/sdk/index.html

Ndk(ver:r8b) 下载地址:https://developer.android.com/tools/sdk/ndk/index.html

示例工程下载地址:https://github.com/ilzc/Code/tree/master/jni


步骤详解:

1、 配置ndk路径

打开Eclipse后,点击菜单栏的Project->Preferences打开Preferences窗口,点击左侧Android->NDK选项,在右侧NDK Location填入ndk的路径



2、 创建工程并增加native支持

点击菜单栏的File->New->Android Application Project创建Android工程。


创建完毕后,在PackageExplorer中右键点击刚才新建的Android项目,选择Android Tools->Add Native Support,按下图填写,点击确认后,工程目录下会增加jni目录,jni目录下有test.cpp和Android.mk。





3、 编写jave层的jni接口

创建一个Java类,类名为Jni

编写加载库的代码,并添加两个native方法

[java] view plain copy
  1. packagecom.mylzc.jni;
  2. publicclassJni{
  3. static{
  4. System.loadLibrary("test");//加载库libtest.so
  5. }
  6. publicstaticnativeintplus(inta,intb);//对应native层的Java_com_mylzc_jni_Jni_plus函数
  7. publicstaticnativeStringgetString(Stringa,Stringb);//对应native层的Java_com_mylzc_jni_Jni_getString函数
  8. }


4、 编写native层的代码

[cpp] view plain copy
  1. #include<jni.h>
  2. #include"stdlib.h"
  3. extern"C"{
  4. jintJava_com_mylzc_jni_Jni_plus
  5. (JNIEnv*,jclass,jintx,jinty){
  6. returnx+y;//返回x+y的结果
  7. }
  8. jstringJava_com_mylzc_jni_Jni_getString
  9. (JNIEnv*env,jclass,jstringa,jstringb){
  10. constchar*str_a=env->GetStringUTFChars(a,0);
  11. constchar*str_b=env->GetStringUTFChars(b,0);
  12. intlen_a=strlen(str_a);
  13. intlen_b=strlen(str_b);
  14. //concatstring
  15. char*str_result=newchar[len_a+len_b+1];
  16. strcpy(str_result,str_a);
  17. strcat(str_result,str_b);
  18. jstringjstr_result=env->NewStringUTF(str_result);//创建string对象
  19. delete[]str_result;
  20. env->ReleaseStringUTFChars(a,str_a);
  21. env->ReleaseStringUTFChars(b,str_b);
  22. returnjstr_result;//返回ab字符串连接之后的结果
  23. }
  24. }


5、 编写Android.mk

[html] view plain copy
  1. LOCAL_PATH:=$(callmy-dir)#指定源文件目录
  2. include$(CLEAR_VARS)#清空变量
  3. LOCAL_MODULE:=test#模块名称,对应编译出libtest.so
  4. LOCAL_SRC_FILES:=test.cpp#指定要编译的源文件
  5. include$(BUILD_SHARED_LIBRARY)#指定编译动态链接库



6、 编译运行

编译:在Package Explorer视窗,右键点击jni项目,选择Build Project编译项目,编译成功后,在工程libs->armeabi目录下可以看到libtest.so。


运行:在Package Explorer视窗,右键点击jni项目,选择Run As->Android Application运行工程。

最后,我们可以在logcat中可以看到打印结果:




更多相关文章

  1. Android(安卓)6.0+ 需要在运行时请求的权限
  2. 20-45万 Android(安卓)Engineer 安卓工程师
  3. RK3399 编译recovery
  4. android xbmc编译
  5. Android(安卓)技巧
  6. Android(安卓)编译摘要
  7. android 扫描包下所有类
  8. 使用“aapt dump”查看APK内容
  9. java.lang.VerifyError: Verifier rejected class 问题解决

随机推荐

  1. android init进程分析 ueventd — 设备
  2. 将列添加到表时可能发生的冲突
  3. 设置边距更改时,ValueAnimator滞后
  4. Android系统启动流程 -- android
  5. Android 通过Volley 模拟登录教务系统 出
  6. Volley使用指南第一回(来自developer.andr
  7. 以编程方式将位置模式更改为高精度Androi
  8. 理解Android的本地Service和跨进程Servic
  9. Android小项目之六 apk下载
  10. 尝试查看所有XML文件的图形布局时出现Sta