Android 4.4.2 Dynamically add Jni library method Records (one JNI library layer)

Source: Internet
Author: User

Welcome reprint, but must indicate the source. http://blog.csdn.net/wang_shuai_ww/article/details/44456755

This article is another way to add jni and services after the s5p4418 Android 4.4.2 Driver Layer Application layer development process record for the HAL Layer service layer.

The previous approach is to add HAL and the service layer directly to the Android API, this way the benefits of the operating system has been developed, leaving the development of the app, Then we only need a Classes.jar file to use the hidden pi of our own Android system (the API that is not in the official Android SDK and the user's own added API), the platform that runs its own Android system can be directly and conveniently tested. Of course, you can also make your own Android.jar file in the SDK platforms corresponding to the Android version, replace the previous Android.jar, then select the target Android version, the load is our own Android system API, can be like Android Oid.os.xxx to call our interface.


This article describes the dynamic loading of JNI, as the name implies, when the APK is running to load our written. So JNI library. This method is flexible, it is convenient to develop the transplant, and does not need to enter the framework directory in the source tree to do all kinds of tedious operation. Just write a. c file that meets the requirements, compile and generate the corresponding. So file, and there is no special requirement for the location, I usually put it in the corresponding board level directory (/device/nexell/realarm).


Looks more convenient than adding directly to the Android API, at least a little more concise, not so cumbersome, but the method in eclipse need to build a more class file, but it is no big deal, O (∩_∩) O. For those who want to learn quickly and add their own led operation, this method is very suitable.


For dynamic JNI, first cite three blog posts, the address is http://blog.sina.com.cn/s/blog_4c451e0e0101339i.html, http://www.cnblogs.com/simonshi/ Archive/2011/01/25/1944910.html, http://blog.csdn.net/happy08god/article/details/11405607. Friends can read more and see the similarities and differences they introduce.


This article is after the reference to a number of blog post, write your own code on the Development Board after the test through the record written, you can refer to the code to write their own program. Then compare the differences with other posts.


My source directory is/device/nexell/realarm/led2, there are two files in this directory led2.c and Android.mk. Their source code is as follows:

LED2.C:

#include <stdio.h> #include "jni.h" #include "JNIHelp.h" #include <assert.h>//Introduction Log header file # include <android /log.h>//Log Label # define TAG "Led_load_jni"//define Info Information # # Logi (...) __android_log_print (Android_log_info, TAG, __VA_ARGS__)//define DEBUG message # define LOGD (...) __android_log_print (Android_log_debug, TAG, __va_args__)//define error message # Define LOGE (...) __android_log_print (Android_log_error, TAG, __va_args__) #define DEVICE_NAME "/dev/real_led" <span style= "COLOR: #ff0000;" > #define NO_REGISTER&LT;/SPAN&GT;&LT;SPAN style= "color: #ff0000;" > #ifndef no_register</span>static jint java_realarm_hardware_hardwarecontrol_ledsetstate (JNIEnv *env, Jobject thiz,jint lednum,jint ledstate) <span style= "color: #ff0000;" > #else </span>jniexport jint jnicall java_realarm_hardware_hardwarecontrol_ledsetstate (JNIEnv *env, Jobject Thiz,jint lednum,jint ledstate) <span style= "color: #ff0000;" > #endif </span>{int fd = open (device_name, 0); if (fd = =-1) {LOGE ("led opEn error "); return 1;} LOGD ("Led Open Success"); Ledstate &= 0x01;ioctl (FD, ledstate, 0); close (FD); return 0;} <span style= "color: #ff0000;" > #ifndef no_register</span>static jninativemethod gmethods[] = {{"Ledsetstate", "(II) I", (void *) Java_realarm  _hardware_hardwarecontrol_ledsetstate},};    static int register_android_test_led (jnienv *env) {Jclass clazz;    static const char* Const KCLASSNAME = "Realarm/hardware/hardwarecontrol";    /* Look up the class */Clazz = (*env)->findclass (env, kclassname); <span style= "color: #3333ff;" >//clazz = Env->findclass (Env,kclassboa);</span> if (clazz = = NULL) {LOGE ("Can ' t find class%s\n",        Kclassname);    return-1; }/* Register all the methods */if ((*env)->registernatives (Env,clazz, Gmethods, sizeof (gmethods)/sizeof (Gmeth Ods[0])! = JNI_OK) <span style= "color: #3333ff;" >//if (Env->registernatives (Env,clazz, Gmethods, sizeof (gmethods)/sizeof (Gmethods[0]))! = JNI_OK) </span> {LOGE ("Failed Registering Methods for%s\n", kclassname);    return-1; }/* Fill out the rest of the ID cache */return 0;} <span style= "color: #ff0000;" > #endif </span>jint jni_onload (javavm* vm, void* reserved) {<span style= "color: #ff0000;" > #ifndef no_register</span>jnienv *env = null;if ((*VM)->getenv (VM, (void**) &env, jni_version_1_4)! = JNI_OK) {<span style= "color: #3333ff;" >//if (vm->getenv (void * *) &env, jni_version_1_4)! = JNI_OK) {</span><span style= "color: #3366ff;"  > </span>logi ("Error getenv\n");  return-1;  } assert (env! = NULL);  if (register_android_test_led (env) < 0) {printf ("register_android_test_led error.\n");  return-1; }<span style= "color: #ff0000;" > #endif </span>/* Success--Return valid version number */logi ("/*****************realarm*******************    ***/"); return jni_version_1_4;}
This code in the understanding of the principle of some processing, this code can be used in two ways to finally refer to the Ledsetstate C + + implementation, the specific control is the red part of the code above the macro.

1. If the above macro is defined, then the Jni_onload function is useful in the last word, return jni_version_1_4; return to the JNI version, and there is no registration for the C + + implementation function of Ledsetstate. So how does Android recognize it. If this is the case, Android will automatically search for the corresponding JNI method based on the Java layer's native definition in the class that the JNI refers to, so the name of the function definition is required, in the form of the Java_ package Name _ Class name _ Function name , The Android app automatically matches the corresponding JNI method when it calls the Ledsetstate method inside the Hardwarecontrol class. This method has a disadvantage, is the need to consume CPU resources to match functions, resulting in inefficient operation, when the program is big trouble.


2. It is therefore recommended to use a method that does not define the red macro above, so that when the library is called, the Ledsetstate function is passed (*env)->registernatives this registration, and java_realarm_hardware_ Hardwarecontrol_ledsetstate are bound together, and the APK performs much more efficiently when calling the Ledsetstate method, without having to auto-match, because it already knows which function to call the operating system.


ANDROID.MK:

Local_path:= $ (call My-dir) include $ (clear_vars) Local_module_tags: = optionallocal_src_files: = Led2.clocal_shared_ LIBRARIES: = Libloglocal_c_includes + = $ (jni_h_include) local_ldlibs:=-l$ (sysroot)/usr/lib-lloglocal_prelink_module : = Falselocal_module_path: = $ (target_out_shared_libraries) Local_module: = Libledjniinclude $ (BUILD_SHARED_LIBRARY)

From the above code can be known, I compiled the generated library named libledjni.so, note that local_module_path to use the path shown, that is, the/system/lib directory, when dynamically loaded, also go to this directory to look for.


OK, here the JNI library is complete, we use the ADB push command to send it to the/system/lib directory of the Development Board, and we'll show you how to use it in the next article.


Android 4.4.2 Dynamically add Jni library method Records (one JNI library layer)

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.