如何為Android,Mac和Windows平台建立Java JNI動態連結程式庫

來源:互聯網
上載者:User

標籤:android   style   http   color   io   os   使用   java   ar   

Java Native Interface (JNI) 是Java和C/C++之間的橋樑。在一些情況下,為了提高Java程式的效能,擴充Java程式的功能,我們需要使用JNI。比如資料庫的調用,當我們下載一個JDBC的jar包之後,解壓可以看到裡麵包含了不同平台的動態連結程式庫:

這篇文章介紹如何為Android,Mac和Windows平台,建立一個最簡單的JNI動態連結程式庫。

參考文章:

Getting Started with JNI on Android, Windows and Mac Android

下載NDK,並在Eclipse中配置路徑:

建立一個Android工程hellojni。右鍵工程,選擇Add Native Support自動產生C/C++工程:

修改C/C++代碼,配置一下Android.mk:

點擊儲存就會自動產生動態連結程式庫libhellojni.so。你也可以通過命令ndk-build手動產生:

Windows

開啟Visual Studio,建立一個Win32工程hellojni:

在應用類型中選擇DLL:

配置標頭檔和庫的路徑:

點擊build就可以產生hellojni.dll:

Mac OS X

開啟Xcode,建立一個工程hellojni :

framework選擇STL:

在build設定裡,配置JavaVM 標頭檔路徑和庫路徑:

點擊build就可以產生libhellojni.dylib:

Java工程中如何使用JNI:

申明native介面:

public native String stringFromJNI();public native static void nativePrint();

載入動態連結程式庫:

static {      System.loadLibrary("hellojni");}
源碼

https://github.com/DynamsoftRD/JNI-

git clone https://github.com/DynamsoftRD/JNI-.git


如何為Android,Mac和Windows平台建立Java JNI動態連結程式庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.