上篇讲的是JNI的入门,http://blog.csdn.net/chaoyue0071/article/details/45098009
例子讲了一个.java文件到.h .cpp文件的映射。那当多个.java文件或是自己一个.java对应多个.h .cpp文件??那么就要用到Ant啦
在我们的HelloNDK工程中添加GetInt类和方法。
public class GetInt { public static native int getInt(int a); }
<?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== Apr 16, 2015 11:46:52 AM HelloNDK description linchaoyue ====================================================================== --> <project name="HelloNDK" default="BuildAllHeaders"> <description> description </description> <!-- ================================= target: BuildAllHeaders ================================= --> <target name="BuildAllHeaders" > <antcall target="BuildGetStringHeader"></antcall> <antcall target="BuildGetIntHeader"></antcall> </target> <!-- - - - - - - - - - - - - - - - - - target: depends - - - - - - - - - - - - - - - - - --> <target name="BuildGetStringHeader"> <javah destdir="./jni" classpath="./bin/classes/" class="com.example.hellondk.GetString"></javah> </target> <!-- - - - - - - - - - - - - - - - - - target: depends - - - - - - - - - - - - - - - - - --> <target name="BuildGetIntHeader"> <javah destdir="./jni" classpath="./bin/classes/" class="com.example.hellondk.GetInt"></javah> </target> </project>
这样就实现了多个.java文件的编译。
还有一总情况是一个.java对应多个.h文件呢?
我在GetString类中增加方法
public static native String getWord();工程切换到c++控制台下添加Hello.cpp,Hello.h文件并写好方法。
Hello.h
/* * Hello.h * * Created on: Apr 17, 2015 * Author: linchaoyue */ #ifndef HELLO_H_ #define HELLO_H_ class Hello { public: Hello(); char * getWords(); virtual ~Hello(); }; #endif /* HELLO_H_ */
/* * Hello.cpp * * Created on: Apr 17, 2015 * Author: linchaoyue */ #include <Hello.h> Hello::Hello() { // TODO Auto-generated constructor stub } char * Hello::getWords(){ return "hello c++"; } Hello::~Hello() { // TODO Auto-generated destructor stub }
/* * Class: com_example_hellondk_GetString * Method: getWord * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_example_hellondk_GetString_getWord (JNIEnv *, jclass);
接下来要注意!!需要在Android.mk文件中,在LOCAL_SRC_FILES配置中添加Hello.cpp
LOCAL_SRC_FILES := HelloNDK.cpp Hello.cpp
Project 的Build All一下。就可以了
原文地址:http://blog.csdn.net/chaoyue0071/article/details/45098981