Android JNI ANT

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.