1,打开ADT,设置SDK路径,NDK路径
2,建立android工程 cn.githan.hellondk
3,右键点击工程包名—>android tools —>add native support—>输入hello,自动生成jni文件夹
4,建立类GetString,定义native无需实现的方法;
5,进入工程bin目录,输入以下命令生成头文件:
javah -classpath classes/ -jni cn.githan.hellondk.GetString
6,将头文件放入jni文件夹内
7,在hello.cpp内引入头文件 cn_githan_hellondk_GetString.h
8,实现头文件中的方法
9,在MainActivity中调用类的方法
例子:
package cn.githan.hellondk;
public class newTest {
public native static String GetString();
public static native String aString();
public native int sum(int a, int b);
static{
System.loadLibrary("helloNDK");
}
}
多平台编译:
1,jni下建立Application.mk
2,输入
APP_ABI := x86 armeabi
3,command+B重新构建工程
4,查看libs中的文件夹会出现不同cpu架构中的so文件
编译多个源文件:
1,建立一个新的C++源文件
2,Android.mk中添加对源文件的编译:
LOCAL_SRC_FILES := CPPShowLog.cpp \
hello.cpp
ANT自动创建头文件:
1,添加ant控制台
2,新建build_header.xml
3,alt+/ 创建模版
4,
<project name="test" default="BuildAllHeaders”> ->default对应下面的target
<description>
description
</description>
<!-- =================================
target: BuildAllHeaders
================================= -->
<target name="BuildAllHeaders”> —>创建了一个集合target,使用antcall调用了另外两个target
<antcall target="buildShowlogHeader"></antcall>
<antcall target="buildGetStringHeader"></antcall>
</target>
<!-- - - - - - - - - - - - - - - - - -
target: depends
- - - - - - - - - - - - - - - - - -->
<target name="buildShowlogHeader">
<javah destdir="./jni" classpath="./bin/classes/" class="cn.githan.showcpplog.ShowLog"></javah> —>具体使用javah命令建立头文件
</target>
<target name="buildGetStringHeader">
<javah destdir="./jni" classpath="./bin/classes/" class="cn.githan.showcpplog.GetString"></javah>
</target>
</project>