Android JNI development Basics

Source: Internet
Author: User

1.
Many netizens do not understand the representation of the JNI type method. In the following Android development network, the C-level type is basically the previous type of J + Java, for example, at the C level of JNI
Jstring while Java is string. For Boolean Type, jboolean corresponds to boolean in Java.

2. The ing relationship between Java class representations in JNI is as follows:

Long cwjinfo (INT Nage, string sname, int [] arrsalary
);

We can represent it as "(iljava/lang/string; [I) J"
Double quotation marks are removed. The first parameter indicates a parameter type. The following I indicates that the first parameter is an int integer, and the L parameter indicates a class. Here Java/lang/string is used.
This class is followed by [representing an array, followed by I representing an integer array, and) followed by J representing the return type, in JNI J representing a long integer, the corresponding relationship is as follows:

V void
Z Boolean
B byte
C char
S short
I int
J long
F float
D double
L fully qualified class
[Array type []
(Arg-types

3.
The string passed in the Java layer cannot be directly converted to jstring in JNI, because the Java internal encoding is Unicode, and each Chinese Character occupies two bytes.
In JNI, we need to use UTF-8 to indicate that UTF-8 is not long in special Chinese and English characters. For example, ASCII characters of English characters occupy one byte while Chinese characters use three bytes.
End with/N, so the following error method is:

Jniexport jstring jnicall java_cwj_showlog (jnienv * ENV, jobject OBJ, jstring log) <br/>{< br/> printf ("% s", log ); // This is incorrect. printf cannot directly process the types sent from Java. <Br/> // dosomething returns jstring and so on. <Br/>}

The correct way to convert Unicode to UTF-8 is to use the getstringutfchars function:

Jniexport jstring jnicall java_cwj_showlog (jnienv * ENV, jobject OBJ, jstring log) <br/>{< br/> const jbyte * strdest; <br/> strdest = (* env) -> getstringutfchars (ENV, log, null); <br/> If (strdest = NULL) <br/>{< br/> return NULL; // note that the outofmemoryerror must be thrown due to insufficient memory. Therefore, an empty value is returned first, android Development Network for JNI exception handling will be explained in the following article <br/>}< br/> printf ("% s", strdest ); // now the strdest can be displayed with printf <br/> (* env)-> releasestringutfchars (ENV, prompt, strdest); // the memory to be released when the strdest is used up

Next, we need to return a jstring type, which can be input by the user, for example

Char szbuf [255]; // allocate a buffer <br/> scanf ("% s", szbuf); // receive user input <br/> return (* env) -> newstringutf (ENV, szbuf); // return a UTF-8 string, that is, a jstring.

4. Getting the string length in JNI cannot simply use functions such as strlen. For different processing, we can use
Getstringlength gets the length of a string of the Java Unicode type (wchar_t *), or getstringutflength gets
Take the UTF-8 (char *) String Length in JNI.

5. Finally, today, android123 provides users with a skill to distinguish JNI from Java. JNI-related characters or strings contain UTF keywords,

For example, Java Unicode types are:

Getstringchars/releasestringchars getstringlength newstring getstringregion

The utf8 types corresponding to JNI include:

Getstringutfchars/releasestringutfchars getstringutflength newstringutf getstringutfregion

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.