Android Studio 1.5.1 JNI 編程

來源:互聯網
上載者:User

Android Studio 1.5.1 JNI 編程

1. 建立project MyJNI,使用預設設定即可。
2. 建立Test類:右鍵com.example.myjni建立java類

3. 在Test類中編寫如下代碼,loadLibrary(“test”)中“test”庫為NDK庫名,之後將會定義。myFun()即為我們自訂的c++函數。

public class Test {    static {        System.loadLibrary("test");    }    native String myFun();}

4. 產生Test類所對應的標頭檔:開啟AS Terminal,切換到$工作目錄$\MyJNI\app\src\main\java目錄下,鍵入命令:javah -jni -cp ;, com.example.myjni.Test產生標頭檔(注意-cp之後是 空格+分號+逗號),經過嘗試直接使用“javah -jni com.example.myjni.Test”命令會出現“ Could not find class file for ‘com.example.myjni.Test’.”的錯誤。此時在java\目錄下就可以看到產生的.h檔案了。

產生的.h內容如下:<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">/* DO NOT EDIT THIS FILE - it is machine generated */#include /* Header for class com_example_myjni_Test */#ifndef _Included_com_example_myjni_Test#define _Included_com_example_myjni_Test#ifdef __cplusplusextern "C" {#endif/** Class: com_example_myjni_Test* Method: myFun* Signature: ()Ljava/lang/String;*/JNIEXPORT jstring JNICALL Java_com_example_myjni_Test_myFun(JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif

5. 建立jni檔案夾:在main目錄上右鍵,new->folder->jni floder
6. 將步驟4中產生的標頭檔剪下到jni檔案夾中,並且建立C++ source file “test.cpp”,編輯代碼如下:

#include "com_example_myjni_Test.h"JNIEXPORT jstring Java_com_example_myjni_Test_myFun(JNIEnv *env, jobject obj) {    return env->NewStringUTF("hello, this is myFun output!");}jstring Java_com_example_myjni_Test_myFun(JNIEnv *env, jobject obj);

在編輯完標頭檔之後,可以build一下,這樣便會提示相關變數類型及函數。至此代碼部分就搞定了~

7. 配置AS NDK環境:開啟AS SDK manager在sdk tools中選擇Android NDK點擊安裝。

8.左鍵file,選擇project structure。進入sdk location,看到如下介面,選擇OK。此時NDK及SDK的目錄將會出現在local.properties中。

local.properties中代碼如下所示:

ndk.dir=D\:\\Users\\XXX\\AppData\\Local\\Android\\sdk\\ndk-bundlesdk.dir=D\:\\Users\\XXX\\AppData\\Local\\Android\\sdk

9.向gradle.properties中加入android.useDeprecatedNdk=true
10.向app目錄下build.gradle檔案中設定庫檔案名稱(產生的so檔案名稱),其moduleName “test”就是第3步中System.loadLibrary(“test”);中所填的庫名。

apply plugin: 'com.android.application'android {    compileSdkVersion 23    buildToolsVersion "23.0.2"    defaultConfig {        applicationId "com.example.myjni"        minSdkVersion 15        targetSdkVersion 23        versionCode 1        versionName "1.0"        //添加部分        ndk{            moduleName "test"        }        //添加部分結束    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}

11.至此我們第一個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.