Android JNI Learning Notes (iv)-data type mapping and native calling Java

Source: Internet
Author: User

1. Preface

The first few learned the basic process of JNI development, the dynamic registration native function and the compilation of related compiled files, we also know the JNI development, but not enough, today we learn, Java and JNI data type mapping (plainly is the corresponding relationship), And how to invoke some of the Java layer at the JNI layer. Secretly tell you, these are all in the jni.h file.

2. Data type Mapping

The first is our basic data type, whose relationship is described in the following table.

The relevant code for the above relationship is in line 44-51 of jni.h, as follows

typedef unsigned CharJboolean;/ * Unsigned 8 bits * /typedef signed CharJbyte;/ * Signed 8 bits * /typedef unsigned  ShortJchar;/ * unsigned-bits * /typedef  ShortJshort;/ * Signed-bits * /typedef intJint/ * Signed-bits * /typedef Long LongJlong;/ * Signed bits * /typedef floatJfloat;/ * 32-bit IEEE 754 * /typedef Doublejdouble;/ * 64-bit IEEE 754 * /

The reference type of the JNI layer is the following.

For these reference types, the implementations of C + + and C are not the same. In the case of C + +, all reference types derive from Jobject, and if written in C, all reference types use Jobject, and other reference types are redefined using typedef. The same code is also in jni.h. This gives only the part of the C + + inheritance structure.

 class _jobject {}; class _jclass :  public _jobject {}; class _jstring :  public _jobject {}; class _jarray :  public _jobject {}; class _jobjectarray :  public _jarray {}; class _jbooleanarray :  public _jarray {}; class _jbytearray :  public _jarray {}; class _jchararray :  public _jarray {}; class _jshortarray :  public _jarray {}; class _jintarray :  public _jarray {}; class _jlongarray :  public _jarray {}; class _jfloatarray :  public _jarray {}; class _jdoublearray :  public _jarray {}; class _jthrowable :  public _jobject {};
3. Native How to call C

Our invocation here includes many aspects, such as:

    • Calling a static method
    • Invoking instance methods
    • Get field values
    • modifying field values
    • Constructing objects
    • Wait a minute

To achieve some of the above functions, also rely on jni.h jninativeinterface This structure, there are a lot of methods for us to use to implement native call Java layer function. And the process of invocation is this:

    • Find the desired class in the JVM based on the fully qualified name
    • Get to method, or field from Jclass
    • Execute fetch value, modify value, Invoke method, or other action
    • Releasing a local reference

Take a look at the example of a static method call.

void Calljavastatic (jnienv *env,jobject jobj){char* str = "call from C + +";  Jclass clazz = Env->findclass ("com/example/cmake_demo/mainactivity");    if (clazz = = null) {LOGE ("class is null");  Return }Jmethodid Method = env-getstaticmethodid(clazz, "Javastaticmethod", "(ljava/lang/ String;)V");  if( Method = = NULL)  {LOGE ("not Find Method"); }jstringjstr = env,newstringutf(str);    Env->callstaticvoidmethod (Clazz, method,jstr);Env->deletelocalref (Clazz); Env->deletelocalref (JSTR);}
3.1 How to find a class

Very simply, we can find the class by the Findclass method.

jclass clazz = env->FindClass("com/example/cmake_demo/MainActivity");
3.2 How to get a method, or field

Roughly the following four ways

 env->GetxxxField() env->GetStaticxxxField() env->GetMethodID() env->GetxxxMethodID()

There are no parameters listed above, but it is still clear that there is not much to say here.

3.3 How to invoke a method

Here it is. Roughly divided into the following four kinds:

  env->CallXXXMethod();  env->CallxxxMethodA();  env->CallxxxMethodV();  env->CallNonvirtualBooleanMethod()

Similarly, I do not give the parameters of the method here, students see jni.h it

    • Call method (here the method may make static, or may not be static)
    • And the difference above is the corresponding Java layer parameters, here in the form of an array to enter
    • And the 1 difference is, with V (vector? In the form of passing in, here I am not very understanding, want to know the students pointing under.
    • Call the constructor to initialize an object, this, said immediately.
3.4 How to modify the value of a field

Believe that here, everyone guess can guess, set, here I will not chatter.

3.5 How to construct an object out

In some cases we need to construct the Java Layer object, so how to construct it, we have two ways.

    • NewObject method
    • Callnonvirtualxxmethod

First of all, the NewObject method, in addition to requiring the Jclass parameter, requires Jmethodid, as well as the parameters corresponding to the Java called construction method. The other two is OK, the key is this jmethodid, this in the acquisition, the method name fixed is < init > (MD syntax reason, note there is no space between angle brackets), don't ask why.

In terms of the second, the second is used in this

  jobject  jo = env->AllocObject(clazz);  env->CallNonvirtualVoidMethod(jo,clazz,jmethodId,arg )
    • The first line of code creates an uninitialized object and allocates memory
    • The second line of code, called the Init Method (constructor), is initialized, and note that it can only be initialized once.
4. Summary

Now we understand the data type mappings between JNI and Java, and the method of invoking the Java layer at the JNI layer.

Android JNI Learning Notes (iv)-data type mapping and native calling Java

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.