標籤:android jni ani
上篇講的是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);}
在整個工程中添加build_headers.xml檔案,右鍵open with選擇ant來開啟
<?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>
xml設定檔寫好了。再Window中Show View找出Ant控制台,第一個按鍵add把build_headers添加進來再雙擊就產生就可以看到在jni檔案夾下產生了com_example_hellondk_GetString.h和com_example_hellondk_GetInt.h兩個檔案啦。
這樣就實現了多個.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
/* * 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}
然後我們找到Ant控制台下雙擊就可以看到com_example_hellondk_GetString.h檔案下多了個方法
/* * Class: com_example_hellondk_GetString * Method: getWord * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_example_hellondk_GetString_getWord (JNIEnv *, jclass);
那麼我們只要在HelloNDL.cpp類中實現這個方法。
接下來要注意!!需要在Android.mk檔案中,在LOCAL_SRC_FILES配置中添加Hello.cpp
LOCAL_SRC_FILES := HelloNDK.cpp Hello.cpp
Project 的Build All一下。就可以了
Android JNI ANT