Cocos2d-x first school (16)-> C ++ through JNI and Java data mutual

Source: Internet
Author: User

Reprinted! Please indicate the source when reprint: http://write.blog.csdn.net/postedit/8182856

I haven't updated my blog for more than a week. It is estimated that many people are asking me what the bloggers are doing recently! I have not published any new blog posts. Since I went out of my unemployment status last week, and then introduced myself to the group, some friends gave me a job recommendation, I would like to thank him and so many friends for their affirmation... thank you very much!

JNI estimates that most of my friends will be afraid of hearing this ~ If you have never touched on it, it's a big task.

First, let's take a look at some knowledge points:

1. Relationship table for basic data type pairs:

In fact, a closer look shows that the corresponding Java type is pulled by the first letter. boolean is special,
Corresponding to Z, long
Corresponding to J

2. the Java method maps to the signature in C :(Describes the signatures corresponding to the four methods)

Public void test () {} () V

Public void test1 (int I) {} (I) V

Public int Test2 () {return 1;} () I

Public int test3 (int I) {return I;} (I) I

3. Reference Data Type: It is troublesome. It starts with "L" and ends with ";" and corresponds to this type of path in the middle.

For example, string: ljava/lang/string;

Object: ljava/lang/object;

The custom cat class corresponds to package com. duicky;

Cat: lcom/duicky/CAT;

4. array representation:"[" Is used as the flag when the array is represented, and a "[" is used as the one-dimensional array.

For example, int []: [I

Long [] []: [J

Object [] [] []: [[ljava/lang/object;

5,View the JDK SignatureWrite a test class using the following methods:

public void test(){}public void test1(int i){}public int test2(){ return 1;}public int test3(int i) { return i;}public Cat test4(Cat c){ return new Cat();}public Object test5(int [] a){ return null;}public int[][] test5(long [][] b){ return null;}public void test6(Object[][][] b){}

Open cmd command line, CD to generate test class open cmd command line, CD to generate test class directory:

 

CD to the corresponding folder

Enter the command javap-s and the class name of the method signature you want to view.

For example, the javap-s test result shows the desired signature.

 

6. Java calls the C method:

Step 1: Compile the native method:

Private native int fromC ();

Step 2: Compile JNI: (I directly use the main. cpp method in cocos2dx, and then write it to woo ~ in extern "C ~ For ease of viewing, other code is omitted)

using namespace cocos2d;using namespace cocos2d::extension;using namespace std;extern "C"{jint Java_potter_back_MyHelloWorld_fromC(){return 222;}

Step 3: Call the NATIVE METHOD

package potter.back;public class Main {public static void main(String[] args) {System.out.println("fromC++"+new Main().fromC());}private native int fromC();}

7. c calls the Java method: Let's look at it later. Use this method to typeset it later ~

 

Call the JNI Method

package potter.back;public class Main {public static void main(String[] args) {new Main().fromJava();}private native int fromJava();}class Potter {public void fromJava(String msg){System.out.println("From Android");}}

Compile the JNI Method

Void java_potter_back_myhelloworld_fromjava (jnienv * jnienv, jobject thiz) {// ing Class jclass Potter = jnienv-> findclass ("Potter/back/Potter "); // ing construc_id = jnienv-> getmethodid (Potter, "<init>", "() V "); // create the object jobject mpotter = jnienv-> newobject (obj_class, construction_id); // map the object method jmethodid fromjava = jnienv-> getmethodid (Potter, "fromjava ", "(ljava/lang/string;) V"); // call the fromjava method jnienv-> callvoidmethod (mpotter, fromjava, jstrmsg) of the Java object mpotter );}

 8. Passing Java objects through JNI:

Step 1:

Public class main {public static void main (string [] ARGs) {New Main (). javaobjecttoc ();} public void javaobjecttoc () {// call the method in Java through C ++, and then pass the person object person P = new person (24); ccalljava (P ); system. out. println ("P's age =" + P. age);} public native void ccalljava (person P);} class person {public int age; Public Person (INT age) {This. age = age;} public void walk () {system. out. println ("person can walk ");}}

Step 2:

Void java_potter_back_myhelloworld_fromjava (jnienv * jnienv, jobject thiz, jobject OBJ) {// get the person class: Get the handle of the object jclass CLS = jnienv-> getobjectclass (OBJ ); jfieldid FID = jnienv-> getfieldid (CLS, "Age", "I"); jmethodid mid = jnienv-> getmethodid (CLS, "walk", "() V "); int value = jnienv-> getintfield (OBJ, FID); jnienv-> setintfield (OBJ, FID, 6); jnienv-> callvoidmethod (OBJ, mid );}

Step 3: Run

Person can walk

P's age = 6 // This value has been modified ~ The accident was 24, and then JNI was set to 6.

 

9. Java transmits the C ++ object through JNI: (In this method, I have searched for n more materials, but I have not found any corresponding materials. However, using the 7 subtitles, this method can be used for data transmission)
If you know more direct solutions, please let me know... thank you very much ~

Thank you for reading my blog and hope you can see your footprints!
If something is wrong or incorrect,I also hope you can point it out!

References: http://www.cnblogs.com/luxiaofeng54/archive/2011/08/18/2143977.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.