Android Application system.loadlibrary unload loading library instance

Source: Internet
Author: User
Tags reserved socket

After the Android load library, if you load the same library repeatedly, a warning is already loaded, which means that the so file will not be reloaded. The need to kill the corresponding activity, and then restart the activity can make so reload, the corresponding code:

int pid = Android.os.Process.myPid ();

Android.os.Process.killProcess (PID);

Description of loading and unloading functions for Android Jni dynamic libraries


when Android Virtual Machine executes to the system.loadlibrary ("Dynamic Library name") function,

The first is to perform the Jni_onload function in the C language dynamic library.
Its use has two:
1 tells Virtual Machine which version of Jni the current dynamic library is using.
If the Jni_onload function is not provided in the current dynamic library,
Virtual Machine defaults to the oldest Jni 1.1 version used by the dynamic library.
Since the new JNI has done a lot of extensions, such as the java.nio.ByteBuffer of Jni 1.4.
2 Dynamic library developers can be in the Jni_onload function in the dynamic library initialization settings (initialization),
Registers each local function (Native function) provided in this dynamic library into the Virtual Machine.
So that you can speed up the ability to call local functions in a dynamic library later, it's important to initialize the settings.
Application-level Java classes can be invoked to local functions in a dynamic library through Virtual Machine.
If you have not registered, Virtual Machine in the dynamic library name. So to find the local function to invoke.
It can take a lot more time if you need to call it multiple times and you need to find it again and again.
So the C language Dynamic Library developer can register the local functions in the dynamic library with Virtual Machine.
code example: (Note: Since the Sina blog does not support C comments, please replace/* * * * Imagine with/stars/)
Jint
Jni_onload (javavm* vm,
void* reserved)
{
Jint Jintresult =-1;
jnienv* env = NULL;
/*reference types, in C.
typedef void* Jobject;
typedef jobject Jclass; */
Jclass cls = NULL;

/* typedef struct {
const char* name; * * The name of the function called in Java code.
Const char* Signature; /* Describes the function's parameters and return value * *
void* fnptr; /* function pointer turned to unsigned pointer
} Jninativemethod;
One of the more complex is the second argument,
For example "() v" or "(II) v" or "(ljava/lang/string;) v"
These characters are actually one by one corresponding to the parameter of the function and the type of the return value.
The characters in parentheses () represent parameters, and the parentheses follow the return values.
For example, "() V" denotes a void function name ();
"(II) V" denotes a void function name (int, int);
"(ljava/lang/string) V" denotes a void function name (jstring);
The meaning of each character expressed in the following section is described in detail.
/* The local function information array in the dynamic library/*
Jninativemethod ajninativemethod[] = {
{"Measuredistance",
"(ljava/lang/string;) V",
(void*) Java_myjni_myndk_mydemo_myjnindkdemo_measuredistance}
};
/* #if defined (__cplusplus)
typedef _jnienv JNIENV;
typedef _JAVAVM JAVAVM;
#else
typedef const struct JNINATIVEINTERFACE* jnienv;
typedef const struct JNIINVOKEINTERFACE* JAVAVM;
#endif * *
/* Javavm::getenv prototype for Jint (*GETENV) (javavm*, void**, Jint); */
/* The JNI environment returned by the GETENV () function is different for each thread, * *
/* So we have to get it back every time we enter the function.
if (Jni_ok!= (*VM)->getenv (VM,
(void**) Env,
jni_version_1_6))
{
/* Output log is generally to the/dev/log/under the three devices, you can use the Logcat tool to view * *
__android_log_write (android_log_info,/* Log information * *
"Myjnidemo", * * Log Label * *
"Call javavm::getenv failed"); /* Log Content * *
return jintresult; /* At this time the return is minus one (-1) * *
}
/* If the local function in the dynamic library fails to register with Virtual Machine, then
/* Because Ajninativemethod is a set of function names and function pointers,
You can call the Registernativemethods function multiple times during program execution to replace the registration local function.
/* #if defined (__cplusplus)
typedef _jnienv JNIENV;
typedef _JAVAVM JAVAVM;
#else
typedef const struct JNINATIVEINTERFACE* jnienv;
typedef const struct JNIINVOKEINTERFACE* JAVAVM;
#endif * *
/* Reference types, in C.
typedef void* Jobject;
typedef jobject Jclass; */
/* struct Jninativeinterface in the function pointer
Jint (*registernatives) (jnienv*,
Jclass,
Const jninativemethod*,
Jint); */
CLS = (*env)->findclass (env,
/* The following string is a description of the main class in Java code * *
"Myjni/myndk/mydemo/myjnindkdemo");
if (0 > (*env)->registernatives (env,
Cls
Ajninativemethod,
sizeof (Ajninativemethod)/
sizeof (Ajninativemethod[0]))
{
/* Output log is generally to the/dev/log/under the three devices, you can use the Logcat tool to view * *
__android_log_write (android_log_info,/* Log information * *
"Myjnidemo", * * Log Label * *
"Register native methods failed"); /* Log Content * *
return jintresult; /* At this time the return is minus one (-1) * *
}

Jintresult = jni_version_1_6;
/* This function jni_version_1_6 the macro value to the Virtual Machine,
So Virtual Machine knows the Jni version of the current dynamic library.
return jintresult; /* JNI_VERSION_1_6 (0x00010006) * *
}

Second, the Jni_onunload function corresponds to the Jni_onload function.

The initial setting in the dynamic library in the Jni_onload function,
Call the Jni_onunload function to clear the cleanup when virtual Machine releases the dynamic library.
As with the Virtual Machine call Jni_onload,
When the Jni_unload function is invoked, the JAVAVM pointer is also passed as the first argument, as follows:
Jint
Jni_onunload (javavm* vm,
void* reserved);

Three, jninativemethod::signature description string character meaning description:

1 Basic type correspondence relation:

Identifier Jni type C type
V void 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

2 Base type array: (then at [Start, in two characters)

Identity string Jni type C type
[Z Jbooleanarray boolean[]
[I Jintarray int[]
[J Jlongarray long[]
[D Jdoublearray double[]
[F Jfloatarray float[]
[B Jbytearray byte[]
[C Jchararray char[]
[S Jshortarray short[]

3 Class (Class): (then start with L, end, middle is with/separated package and class name)

Identity string Java type Jni type
L Package 1/Package n/class name; Class name Jobject
Example:
Ljava/net/socket; Socket Jobject

4 Exception (String Class):

Identity string Java type Jni type
ljava/lang/string; String jstring

5 nested Class (class is in another class, with $ as the delimiter between class names)

Identity string Java type Jni type
L Package 1/Package n/class name $ nested class name; Class name Jobject
Example:
Landroid/os/fileutils$filestatus; Filestatus Jobject

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.