Android JNI ANT

来源:互联网 发布:sql注入防止php 编辑:程序博客网 时间:2024/05/18 18:46

上篇讲的是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一下。就可以了

0 0