Android JNI Development Improvement article

Source: Internet
Author: User

On the development of JNI technology, we continue to work around the Android platform, JNI can support C or C + +, so far we have written the JNI code is implemented by C, that is, the file name is. C and what's different about C + +? Is JNI on the Android platform generally written in C or C + +?

The Android platform uses C + + development in the middle tier and most of the class libraries, with the suffix. cpp, such as the Android Framework, Opencore, Webkit, SQLite, and more. The advantage of using C + + is that you can use many libraries but currently Android does not support STL, we know that C means that strings are character arrays, but C + + can be represented by a type like string.

1. What is the difference between writing C and C + + in code

Here Android123 to convert the Unicode string from Java to UTF8 in Jni, and then return a jstring type as an example, you can see the conversion method of the string between JNI and Java.

Implementation of C:

Jniexport jstring jnicall JAVA_ANDROID123_CWJC (jnienv *env, Jobject obj, jstring string)
{
const char *STRUTF = (*env)->getstringutfchars (env, string, 0);
Char szbuffer[255];
strcpy (Szbuffer, STRUTF);
(*env)->releasestringutfchars (env, String, Strutf);
Return (*env)->newstringutf (env, szbuffer);
}

Implementation of C + +:

Jniexport jstring jnicall java_android123_cwjcpp (jnienv *env, Jobject obj, jstring string)
{
const char *strutf = env->getstringutfchars (string, 0);
Char szbuffer[255];
strcpy (Szbuffer, STRUTF);
Env->releasestringutfchars (string, STRUTF);
Return Env->newstringutf (szbuffer);
}

We've added the key differences to the keywords, and we can see that C + + code is more concise.

2. JNI Operation Array Code

The common object for the processing array in JNI is Jobjectarray of course general types such as shaping to Jintarray, Boolean jbooleanarray, but no jstringarray such type, about character array processing we will be in the next Android JNI Development Step-by-step article detailed description. When working with arrays we need to consider that the length of the array cannot be 0 to continue, otherwise there will be an access violation, and a generic type of getarraylength function is provided in JNI. We pass in an array of shapes from Java, adding each element as an example in JNI returns the result of a shaping that tells the Java operation.

Jniexport jint jnicall java_android123_cwjtest (jnienv *env, jobject obj, Jintarray array)
{
int sum = 0;
Jsize length = (*env)->getarraylength (env, array); Get array length

if (length==0)//Prevent an exception from occurring, if it is empty, you need to return the
return 0;

Jint *pointer = (*env)->getintarrayelements (env, array, 0); Get Array pointers
for (int i=0; i<length; i++)
{
Sum + = Pointer[i]; Add each array element
}
(*env)->releaseintarrayelements (env, array, pointer, 0); Free up memory, this can't be forgotten.
return sum;
}

How do you construct an array in JNI? Android Development Network give you a simple example, return an array of shapes:

Jniexport Jobjectarray jnicall
Java_android123_cwjtest2 (jnienv *env, Jclass clazz)
{
Jobjectarray result; Defining the Return Object

Jclass Intarrayclazz = (*env)->findclass (env, "[I");//Find shaped array
if (Intarrayclazz = = NULL)
{
return NULL;
}
result = (*env)->newobjectarray (env, size, intarrayclazz, NULL); Constructs a new Array object
if (result = = NULL)
{
return NULL;
}

for (int i = 0; i < i++)//Cycle 10 times
{
Jint szbuffer[256];
Int J;

Jintarray Newintarray = (*env)->newintarray (env, 10); Constructs 10 array of shaping
if (Newintarray = = NULL)
{
return NULL;
}
for (j = 0; J < x J + +)//10
{
SZBUFFER[J] = i + j;
}
(*env)->setintarrayregion (env, Newintarray, 0, szbuffer); Set length to 10
(*env)->setobjectarrayelement (env, result, I, newintarray);
(*env)->deletelocalref (env, Newintarray);
}
return result;
}

3. Handling of exceptions in JNI

The exception thrown in JNI does not try...catch, but throws an error directly

Method 1: Use thrownew, such as IOException class has occurred filenotfound

(*env)->thrownew (env, (*env)->findclass ("Java/io/ioexception"), "Cwjlog Error, IOException");

Method 2: Use throw to construct your own

Jclass clazz = (*env)->findclass (env, "java/io/ioexception");
Jmethodid Methodid = (*env)->getmethodid (env, Clazz, "<init>", "() V");
Jthrowable throwable = (*env)->newobject (env, Clazz, Methodid);

(*env)->throw (env, throwable);

/**
* @author Zhang Xingye
* Email: xy-zhang#163.com
* Android Development Advanced Group: 278401545
*
*/

Android JNI Development Improvement 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.