My understanding of android jni

Source: Internet
Author: User

In the past few days, the Basic Debugging of rfid is almost the same. It is a real building process from scratch, and it has a great sense of accomplishment. The following describes the jni of rfid.
.
This jni development uses android ndk tools.
In jni development, the most important part is the jni data structure. During the development process, I also found a lot of information on the Internet. Below is a good jni
Data Structure Description:
Andoird uses a different traditional Java JNI method to define its native function. An important difference is that Andorid uses a ing table array of Java and C functions, and describes the parameters and return values of the functions. The type of this array is JNINativeMethod, which is defined as follows:

Typedef struct {
Const char * name;
Const char * signature;
Void * fnPtr;
} JNINativeMethod;

The first variable name is the name of the function in Java.

The second variable signature describes the parameters and return values of the function using a string.

The third variable fnPtr is the function pointer pointing to the C function.

The second parameter is hard to understand, for example

"() V"

"(II) V"

"(Ljava/lang/String;) V"

In fact, these characters correspond to the function parameter types one by one.

The character in "()" represents a parameter, and the subsequent character represents the return value. For example, "() V" indicates void Func ();

"(II) V" indicates void Func (int, int );

The relationship between each character is as follows:

Character Java type C type

V void
Z jboolean boolean
I jint int
J jlong long
D jdouble double
F jfloat float
B jbyte byte
C jchar char
S jshort short

The array starts with "[" and is represented by two characters.

[I jintArray int []
[F jfloatArray float []
[B jbyteArray byte []
[C jcharArray char []
[S jshortArray short []
[D jdoubleArray double []
[J jlongArray long []
[Z jbooleanArray boolean []

The above are all basic types. If the Java function parameter is a class, it starts with "L" and ends with ";" and ends with a package and class name separated. The parameter of the corresponding C function name is jobject. An exception is the String class, and its corresponding class is jstring.

Ljava/lang/String; String jstring
Ljava/net/Socket; Socket jobject

If a JAVA function is located in an embedded class, $ is used as the delimiter between class names.

For example, "(Ljava/lang/String; Landroid/OS/FileUtils $ FileStatus;) Z"

With the above knowledge, you will understand the relationship between the native method and the data type between java.
In addition, for jni development, the native method name is mainly used:
For example, the path of your app is src/com/android/rfid/Rfid. java.
Your native method name should be Java_com_android_rfid_Rfid_xxx (JNIEnv * env, jobject thiz)
Add the following content to the java file:
Public native short [] xxx ();
Static {
System. loadLibrary ("rfid-jni ");
}
Then you can directly use the xxx () method.

In the above example, librfid_jni.so of System. loadLibrary ("rfid-jni") is compiled by the ndk tool. The use of ndk online is very
The relevant information will not be described here.


 

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.