Android ndk-based JNI development C call Java and Java call C advanced tutorial

Source: Internet
Author: User

Address: http://blog.csdn.net/loseleo/article/details/5858862

 

 

First, I want to talk about calling Java in C, because there is no detailed information on the Internet. Secondly, it is not long before I develop an android application. For Java errors, please forgive me!

 

 

I should not go into details about how to configure the ndk environment. There are many problems on the Internet, as long as the configuration can compile the local sample program of JNI.

(NDK-1.6 Version)

I. Here I will talk about how to write the JNI program by myself. Many articles on the Internet are automatically generated using javah, which is not necessary. First go to The android-ndk-1.6_r1/apps copy hello-JNI folder to the app directory, change the name you want, here is called test-JNI, then enter the test-JNI directory, open the application with the WordPad. mk

Change content

App_project_path: = $ (call my-DIR)/Project
App_modules: = test-JNI

Save and close. Go to the project/JNI directory and open Android. mk.

Local_module: = hello-JNI
Local_src_files: = hello-jni.c

These two changes

Local_module: = test-JNI
Local_src_files: = test-jni.c

Also change the hello-jni.c to test-jni.c so that you can use make APP = test-JNI-B command to compile your JNI.

Ii. Here I will explain how to manually write JNI code

The JNI created above can be used during compilation, but cannot be used because the JNI function has a function name specification. Open the test-jni.c to see

Java_com_example_hellojni_hellojni_stringfromjni is named in detail. Java _ starts with which class you want to use in Java. For example, I want to use it in the testjni class under COM/example/hellojni, name java_com_example_hellojni_testjni_stringfromjni.

Then there is the jnienv * ENV, jobject thiz parameter. These two are the default parameters. The first is the Java virtual machine, which must be available, and the second is not needed, which is equivalent to the pointer of the object of the current class, that is, this.

 

The content is also written in many ways. It is nothing more than standard C processing. Here, we should note that the array types in the Java side correspond to a class pointer, such as byte [], and jbytearray in the C side,

To obtain the array value, use

Int Len = (* env)-> getarraylength (ENV, MSG );
Jbyte * elems = (* env)-> getbytearrayelements (ENV, MSG, 0 );

(* Env)-> releasebytearrayelements (ENV, MSG, elems, 0 );

 

 

Iii. Focus: C calls Java.

First, let's talk about the call process I understand. C needs to find this class first, then have an object, and then call this function based on the class object.

Therefore, to call a Java function, you must first have a class, then the function ID, and then the object

 

CLS = (* env)-> findclass (ENV, "com/example/hellojni/ext_graphics ");

// Obtain the Array
Mid = (* env)-> getmethodid (ENV, CLS, "getcanvasbmp Byte", "() [B "); // The last parameter here is obtained using the javap-s-p command, which has been mentioned in many articles.
MSG = (jbytearray) (* env)-> callobjectmethod (ENV, job, mid); // note that the second parameter here is the object, it means that you call the method of the current object of the current class. If the current class does not have this function, an error will occur. If you want to call this method in other classes, please pass in the reference of this class object as a parameter:

Java_com_example_hellojni_hellojni_initgraphics (jnienv * ENV, jobject thiz, jobject job) // This is the object where the function is located.

 

This is the only option for the time being. For details, please send me a message ~ I will answer

First, I want to talk about calling Java in C, because there is no detailed information on the Internet. Secondly, it is not long before I develop an android application. For Java errors, please forgive me!

 

 

I should not go into details about how to configure the ndk environment. There are many problems on the Internet, as long as the configuration can compile the local sample program of JNI.

(NDK-1.6 Version)

I. Here I will talk about how to write the JNI program by myself. Many articles on the Internet are automatically generated using javah, which is not necessary. First go to The android-ndk-1.6_r1/apps copy hello-JNI folder to the app directory, change the name you want, here is called test-JNI, then enter the test-JNI directory, open the application with the WordPad. mk

Change content

App_project_path: = $ (call my-DIR)/Project
App_modules: = test-JNI

Save and close. Go to the project/JNI directory and open Android. mk.

Local_module: = hello-JNI
Local_src_files: = hello-jni.c

These two changes

Local_module: = test-JNI
Local_src_files: = test-jni.c

Also change the hello-jni.c to test-jni.c so that you can use make APP = test-JNI-B command to compile your JNI.

Ii. Here I will explain how to manually write JNI code

The JNI created above can be used during compilation, but cannot be used because the JNI function has a function name specification. Open the test-jni.c to see

Java_com_example_hellojni_hellojni_stringfromjni is named in detail. Java _ starts with which class you want to use in Java. For example, I want to use it in the testjni class under COM/example/hellojni, name java_com_example_hellojni_testjni_stringfromjni.

Then there is the jnienv * ENV, jobject thiz parameter. These two are the default parameters. The first is the Java virtual machine, which must be available, and the second is not needed, which is equivalent to the pointer of the object of the current class, that is, this.

 

The content is also written in many ways. It is nothing more than standard C processing. Here, we should note that the array types in the Java side correspond to a class pointer, such as byte [], and jbytearray in the C side,

To obtain the array value, use

Int Len = (* env)-> getarraylength (ENV, MSG );
Jbyte * elems = (* env)-> getbytearrayelements (ENV, MSG, 0 );

(* Env)-> releasebytearrayelements (ENV, MSG, elems, 0 );

 

 

Iii. Focus: C calls Java.

First, let's talk about the call process I understand. C needs to find this class first, then have an object, and then call this function based on the class object.

Therefore, to call a Java function, you must first have a class, then the function ID, and then the object

 

CLS = (* env)-> findclass (ENV, "com/example/hellojni/ext_graphics ");

// Obtain the Array
Mid = (* env)-> getmethodid (ENV, CLS, "getcanvasbmp Byte", "() [B "); // The last parameter here is obtained using the javap-s-p command, which has been mentioned in many articles.
MSG = (jbytearray) (* env)-> callobjectmethod (ENV, job, mid); // note that the second parameter here is the object, it means that you call the method of the current object of the current class. If the current class does not have this function, an error will occur. If you want to call this method in other classes, please pass in the reference of this class object as a parameter:

Java_com_example_hellojni_hellojni_initgraphics (jnienv * ENV, jobject thiz, jobject job) // This is the object where the function is located.

 

This is the only option for the time being. For details, please send me a message ~ I will answer

Related Article

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.