Android JNI Development (advanced tutorial)

Source: Internet
Author: User

I will explain in detail about Java yesterday.
JNI-related exception handling and thread security issues are mainly caused by insufficient memory outofmemoryerror and array out-of-bounds exceptions in JNI.
Arrayindexoutofboundsexception, array assignment type error arraystoreexception, pointer out-of-bounds, and other issues. Simple me
We developed and improved it in Android JNI yesterday.
.

In addition to throw or thrownew to throw an exception, five functions are provided for processing, namely jthrowable.
Predictionoccurred (jnienv * env);, void predictiondescribe (jnienv
* Env); void exceptionclear (jnienv * env); jboolean (jnienv * env) and void
FatalError (jnienv * ENV, const char * MSG );

1. exceptioncheck is used to detect if an exception has been thrown, this method will return jni_true, which is a Boolean value defined as 1 by typedef.

2. exceptionoccurred gets a local reference that is throwing an exception. Native or Java layer must handle this exception and return a jthrowable object.

3. predictiondescribe is used to print the error description of an exception.

4. exceptionclear clear the exception just thrown.

5. fatalError has a special role and generates a fatal error. android123 prompts that the JVM will be shut down, that is, the program stops running, so be cautious when using it.

Let's take the C ++ code as an example to briefly describe how to use it.

Env-> findclass ("android123cwj"); // assume that this class does not exist. <br/> If (env-> predictioncheck ()) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/>}

In this way, the JVM does not exist because the android123cwj class does not exist, resulting in a noclassdeffounderror exception.

JNI provides a set of functions for jint monitorenter (jnienv * ENV, jobject
OBJ); and jint monitorexit (jnienv * ENV, jobject OBJ );
Method, similar to a simple synchronization lock, written in Java

Synchronized (OBJ) {<br/> // dosomething <br/>}

In JNI, we use this set of functions to write

(* Env)-> monitorenter (OBJ); <br/> // dosomething <br/> (* env)-> monitorexit (OBJ );

Tomorrow, the android Development Network will continue to explain JNI-related content, detailing the construction of Java classes in JNI, finding methods and field access, and other knowledge. I hope users who are free will first learn about it.
Java reflection and dynamic proxy knowledge can better understand our last section about Android JNI development.

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.