For the development technology of JNI, we continue to work around the Android platform. JNI supports C or C ++. The JNI code we have written so far is implemented in C, that is, the file name is. what is the difference between C ++ and C ++? Is JNI on Android generally written in C or C ++?
The Android platform uses the C ++ development method at the underlying layer of the middle layer and most class libraries. The extension is. cpp, such as Android framework, opencore, WebKit, and SQLite. The advantage of using C ++ is that many libraries can be used, but currently Android does not support STL. We know that C represents a string as a character array, but C ++ can use a type like string.
1. What is the difference between writing C and C ++ in code?
Here, android123 converts the Java Unicode string to utf8 in JNI, and then returns a jstring type as an example. We can see the conversion method of the string between JNI and Java.
C implementation:
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 );
}
C ++ implementation:
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 bold out the key differences and we can see that the C ++ code is more concise.
2. JNI Operation Array code
In JNI, the common object for processing arrays is jobjectarray. Of course, the common types include integer jintarray and Boolean jbooleanarray, but there is no such type as jstringarray, we will describe how to process character arrays in the next Android JNI development advanced article. When processing an array, we need to consider that the length of the array cannot be 0 to continue the operation, otherwise there will be access out of bounds and other issues, and a general type of getarraylength function is provided in JNI. We pass in an integer array from Java, and add each element in JNI as an example to return the result of an integer telling the Java operation.
Jniexport jint jnicall java_android123_cwjtest (jnienv * ENV, jobject OBJ, jintarray array)
{
Int sum = 0;
Jsize length = (* env)-> getarraylength (ENV, array); // obtain the array length.
If (length = 0) // prevents exceptions. If it is null, return
Return 0;
Jint * pointer = (* env)-> getintarrayelements (ENV, array, 0); // obtain the array pointer
For (INT I = 0; I <length; I ++)
{
Sum + = pointer [I]; // Add each array element
}
(* Env)-> releaseintarrayelements (ENV, array, pointer, 0); // release the memory, which cannot be forgotten.
Return sum;
}
How to construct an array in JNI? Android development network provides a simple example to return an integer array:
JniexportJobjectarrayJnicall
Java_android123_cwjtest2 (jnienv * ENV, jclass clazz)
{
Jobjectarray result; // defines the returned object
Jclass intarrayclazz = (* env)-> findclass (ENV, "[I"); // search for an integer Array
If (intarrayclazz = NULL)
{
Return NULL;
}
Result = (* env)-> newobjectarray (ENV, size, intarrayclazz, null); // construct a new array object
If (result = NULL)
{
Return NULL;
}
For (INT I = 0; I <10; I ++) // loop 10 times
{
Jint szbuffer [256];
Int J;
Jintarray newintarray = (* env)-> newintarray (ENV, 10); // constructs 10 integer Arrays
If (newintarray = NULL)
{
Return NULL;
}
For (j = 0; j <10; j ++) // 10
{
Szbuffer [J] = I + J;
}
(* Env)-> setintarrayregion (ENV, newintarray, 0, 10, szbuffer); // set the length to 10.
(* Env)-> setobjectarrayelement (ENV, result, I, newintarray );
(* Env)-> deletelocalref (ENV, newintarray );
}
Return result;
}
3. Handling exceptions in JNI
The exception thrown in JNI is not like try... catch, but directly throws an error.
Method 1: thrownew is used. For example, filenotfound occurs in the ioexception class.
(* Env)-> thrownew (ENV, (* env)-> findclass ("Java/IO/ioexception"), "cwjlog error, ioexception ");
Method 2: Use throw and construct it by yourself
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 Group: 278401545
*
*/