Android Tools Project Site

Recent Changes‎ > ‎

Using the NDK plugin

posted Jul 2, 2012, 7:16 PM by 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.



用上面的方法配置好后,每次编译都会提示错误: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. package com.mylzc.jni;  
  2.   
  3. public class Jni {  
  4.     static {  
  5.         System.loadLibrary("test");//加载库 libtest.so  
  6.     }  
  7.     public static native int plus(int a, int b);//对应native层的Java_com_mylzc_jni_Jni_plus函数  
  8.     public static native String getString(String a, String b);//对应native层的Java_com_mylzc_jni_Jni_getString函数  
  9. }  


4、  编写native层的代码

 

[cpp]  view plain copy
  1. #include <jni.h>  
  2. #include "stdlib.h"  
  3.   
  4. extern "C" {  
  5. jint Java_com_mylzc_jni_Jni_plus  
  6.   (JNIEnv *, jclass, jint x, jint y) {  
  7.     return x + y;//返回x+y的结果  
  8. }  
  9.   
  10. jstring Java_com_mylzc_jni_Jni_getString  
  11.   (JNIEnv *env, jclass, jstring a, jstring b) {  
  12.   
  13.     const char* str_a = env->GetStringUTFChars(a, 0);  
  14.     const char* str_b = env->GetStringUTFChars(b, 0);  
  15.   
  16.     int len_a = strlen(str_a);  
  17.     int len_b = strlen(str_b);  
  18.   
  19.     //concat string  
  20.     char* str_result = new char[len_a+len_b +1];  
  21.     strcpy(str_result, str_a);  
  22.     strcat(str_result,str_b);  
  23.     jstring jstr_result = env->NewStringUTF(str_result);//创建string对象  
  24.     delete[] str_result;  
  25.   
  26.     env->ReleaseStringUTFChars(a, str_a);  
  27.     env->ReleaseStringUTFChars(b, str_b);  
  28.   
  29.     return jstr_result;//返回ab字符串连接之后的结果  
  30. }  
  31. }  


5、  编写Android.mk

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



6、  编译运行

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


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

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值