Getting Started with Jni in Android m

Source: Internet
Author: User

This year Google launched Android 6.0, as an Android developer, it is certainly essential to learn to master, today's small series and we share is the knowledge of the JNI in Android 6.0, which is in an Android tutorial online to see the content, feel very good, Take a look at it ~ ~

Beginners may ask first, what is Jni a ghost?

In fact, the full name of the JNI Java Native Interface,java Local invocation, mainly for the implementation of two functions:

1. Java program calls the Native (c + +) language write function.

2. Native (c + +) program calls Java layer functions.

JNI is a mechanism provided by Java. Java wants to call the C + + library code, and in the middle requires a JNI library, structured like this:

Java Code

JNI Library (lib***_jni.so)

Native (lib***.so)

Java in-load JNI Library

In Android development, classes that need to invoke JNI must first load the JNI library:

System.loadlibrary ("***_jni");

This lets you know if a class is using the JNI method.

You can, in the Android 6.0 code to search, there are hundreds of places to use. For example, an important Systemserver class in an Android system, when it is created, there is a sentence:

Initialize native Services.

System.loadlibrary ("Android_servers");

It seems that Systemserver used the Native library, whose JNI library is named Libandroid_servers.so.

The system.loadlibrary needs to be called before the JNI call is made, and the JNI library is loaded in first.

The Native method in JNI, to be used in Java, requires the creation of a function with the same name in Java with the Native keyword.

Then search for native in the Systemserver code and find only one place:

private static native void Startsensorservice ();

Since Systemserver is used to start a critical service in Android, it loads libandroid_servers.so itself, and other services can directly declare the JNI method to be used without having to load it repeatedly.

For example, in Lightsservice, there are:

private static native long init_native ();

private static native void Finalize_native (long ptr);

static native void Setlight_native (long ptr, int light, int color, int mode,

int onms, int offms, int brightnessmode);

...

In the Powermanagerservice, there are:

Private native void Nativeinit ();

private static native void Nativeacquiresuspendblocker (String name);

private static native void Nativereleasesuspendblocker (String name);

private static native void Nativesetinteractive (Boolean enable);

private static native void Nativesetautosuspend (Boolean enable);

private static native void Nativesendpowerhint (int hintid, int data);

private static native void nativesetfeature (int featureid, int data);

...

None of them ever system.loadlibrary.

JNI the implementation of the library

So many critical services require the JNI approach in libandroid_servers.so, a library that is also well-qualified. Where is the code that implements it? How do you find it?

The functions in the JNI library are named in a way that has strict rules with the methods in Java. For example, the full name of Systemserver's class name is Com.android.server.SystemServer, and the previous article says it has its own unique JNI call Startsensorservice, so Android There must be a function for Android_server_systemserver_startsensorservice in the code.

It is located in Frameworks/base/services/core/jni/com_android_server_systemserver.cpp, and the method is implemented as:

static void Android_server_systemserver_startsensorservice (jnienv*/* env */, Jobject/* Clazz *) {

Char Propbuf[[email protected]];

Property_get ("System_init.startsensorservice", Propbuf, "1");

if (strcmp (Propbuf, "1") = = 0) {

Start the Sensor service

Sensorservice::instantiate ();

}

Let's look at Lightsservice, its class is all called Com.android.server.lights.LightsService, it uses a JNI call for setlight_native, then there must be a function named Android_server_lights_lightsservice_setlight_native, but not found. But the setlight_native, the native implementation of the same name, is found, which is the corresponding JNI implementation, which is located in Frameworks/base/services/core/jni/com_android_server_lights_ Under the LightsService.cpp.

How do you do this with the same name? Let's take a look at the code and see:

static void Setlight_native (jnienv*/* env */, Jobject/* clazz */, Jlong ptr,

Jint Light, Jint Colorargb, Jint flashmode, Jint onms, Jint offms, Jint brightnessmode)

{

...

}

Static Jninativemethod method_table[] = {

{"Init_native", "() J", (void*) init_native},

{"Finalize_native", "(J) V", (void*) finalize_native},

{"Setlight_native", "(JIIIIII) V", (void*) setlight_native},

};

int Register_android_server_lightsservice (jnienv *env)

{

Return Jniregisternativemethods (env, "Com/android/server/lights/lightsservice",

Method_table, Nelem (method_table));

}

It registers the previous bunch of prefixes ("Com/android/server/lights/lightsservice") with a table so that the JNI layer knows the same name function when the Java call is made.

libandroid_servers.so How is it generated?

Looking at the previous two JNI implementations, we found them in different files under frameworks/base/services/core/jni/. How did they organize the final construction of the libandroid_servers.so?

First of all, Libandroid_servers is definitely the name we have in Makefile, under Android is android.mk. Search for Libandroid_servers in the code and find frameworks/base/services/android.mk:

# Native Library

# =============================================================

Include $ (clear_vars)

Local_src_files: =

Local_shared_libraries: =

# include all the JNI subdirs to collect their sources

Include $ (wildcard $ (local_path)/*/jni/android.mk)

Local_cflags + =-degl_eglext_prototypes-dgl_glext_prototypes

local_module:= libandroid_servers

Include $ (build_shared_library)

which

# include all the JNI subdirs to collect their sources

Include $ (wildcard $ (local_path)/*/jni/android.mk)

The JNI in all subdirectories is collected and compiled into Local_module libandroid_servers. And is in the form of a dynamic library, because the JNI library must be a dynamic library.

At this point, the introduction to JNI is done. In the next learning of Android code, encounter with native, encounter a lot of underline method, get out of the way.

The above is the Android6.0 in the relevant knowledge of the JNI, here because of the length of the question, write is relatively shallow, follow-up will continue to share more in-depth relevant content. We suggest that you can try the contents of the above introduction in the local area and deepen your mastery.

Related article: "29 common classes, methods and interfaces for Android development"

Getting Started with Jni in Android m

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.