android NDK 實用學習-擷取java端類及其類變數

來源:互聯網
上載者:User

標籤:

近期為android 端項目封裝一些c++代碼,故學習ndk相關知識,現總結如下:

1,java與c++類型參照圖:

 

2,此測試中使用的java類:

 1 package com.dasea.test.core; 2 public class TestSetData { 3     // 主要是類ID和欄位ID,方法ID的緩衝 4     static { 5         OnNative(); 6     } 7  8     public native static void OnNative(); 9 10     public boolean bData;11     public double  dData;12     public int     iData;13     public byte    cData;14     public String  sData;15     public byte    byteArr[];16 17     public int     intArr[];18 19     public TestSetData() {20         // TODO Auto-generated constructor stub21         bData = true;22         dData = 100.11;23         iData = 333;24         cData = 100;25         sData = "20150204";26         byteArr = new byte[10];27         for (int i = 0; i < byteArr.length; i++) {28             byteArr[i] = ‘2‘;29         }30 31         intArr = new int[10];32         for (int i = 0; i < intArr.length; i++) {33             intArr[i] = i * 10;34         }35     }36 }

3,使用類介面定義:

①java端:

  public native TestSetData testData(TestSetData obj); 

②c++端:

   JNIEXPORT jobject JNICALL Java_com_dasea_test_core_RTKNativeManager_testData(JNIEnv* env, jobject obj, jobject jobj) ;

4,完整c++端介面實現:

 1 JNIEXPORT jobject JNICALL Java_com_dasea_test_core_RTKNativeManager_testData(JNIEnv* env, jobject obj, jobject jobj){ 2     DEBUG_OUT("test set data start"); 3  4     jclass jcSetDataMgr = env->FindClass("com/dasea/test/core/TestSetData"); 5     if(NULL == jcSetDataMgr){ 6         DEBUG_OUT("Not find class!"); 7         return NULL; 8     } 9     DEBUG_OUT("Find class succ!");10 11     jfieldID jfbdata = env->GetFieldID(jcSetDataMgr, "bData", "Z");12     if(NULL == jfbdata){13         DEBUG_OUT("jfbdata fail!");14     }15     jfieldID jfddata = env->GetFieldID(jcSetDataMgr, "dData", "D");16     if(NULL == jfbdata){17         DEBUG_OUT("jfddata fail!");18     }19     jfieldID jfidata = env->GetFieldID(jcSetDataMgr, "iData", "I");20     if(NULL == jfbdata){21         DEBUG_OUT("jfidata fail!");22     }23     jfieldID jfcdata = env->GetFieldID(jcSetDataMgr, "cData", "B");24     if(NULL == jfbdata){25         DEBUG_OUT("jfcdata fail!");26     }27     jfieldID jfsdata = env->GetFieldID(jcSetDataMgr, "sData", "Ljava/lang/String;");28     if(NULL == jfbdata){29         DEBUG_OUT("jfsdata fail!");30     }31 32     DEBUG_OUT("Get end!");33 34     env->SetBooleanField(jobj, jfbdata, false);35     DEBUG_OUT("SetBooleanField end!");36 37     env->SetDoubleField(jobj, jfddata, 209.22);38     DEBUG_OUT("SetDoubleField end!");39 40     env->SetIntField(jobj, jfidata, 3653);41     DEBUG_OUT("SetIntField end!");42 43     env->SetByteField(jobj, jfcdata, 67);44     DEBUG_OUT("Set field succ!");45 46     char data[10] = "4611354";47     env->SetObjectField(jobj, jfsdata, env->NewStringUTF(data));48 49     jfieldID jiarr = env->GetFieldID(jcSetDataMgr, "intArr", "[I");50     if(NULL == jiarr){51         DEBUG_OUT("jfbdata fail!");52     }53 54     // 擷取Java中數組屬性arrays的對象55     jintArray jint_arr = (jintArray)env->GetObjectField(jobj, jiarr);56 57     int arrInt[10] = {0};58     for(int i = 0; i < 10; ++i){59        arrInt[i] = 220+i;60     }61     env->SetIntArrayRegion(jint_arr, 0, 10, arrInt);62 63     jfieldID jbyteArr = env->GetFieldID(jcSetDataMgr, "byteArr", "[B");64     char arrByte[10] = "20200825";65     jbyteArray jbyte_arr = (jbyteArray)env->GetObjectField(jobj, jbyteArr);66     env->SetByteArrayRegion(jbyte_arr, 0, 8, (const signed char*)arrByte);67 68     DEBUG_OUT("test set data end!");69 70     return NULL;71 }

5,單獨講解:

①尋找java類:

jclass jcSetDataMgr = env->FindClass("com/dasea/test/core/TestSetData"); 

②尋找類成員變數:

1 jfieldID jfbdata = env->GetFieldID(jcSetDataMgr, "bData", "Z");2 jfieldID jfddata = env->GetFieldID(jcSetDataMgr, "dData", "D");3 jfieldID jfidata = env->GetFieldID(jcSetDataMgr, "iData", "I");4 jfieldID jfcdata = env->GetFieldID(jcSetDataMgr, "cData", "B");5 jfieldID jfsdata = env->GetFieldID(jcSetDataMgr, "sData", "Ljava/lang/String;");6 jfieldID jbyteArr = env->GetFieldID(jcSetDataMgr, "byteArr", "[B"

 

android NDK 實用學習-擷取java端類及其類變數

聯繫我們

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