Android JNI instance

Source: Internet
Author: User

The android SDK does not support JNI and does not provide any instructions on how to support JNI. However, since the entire Android platform is open-source, we can use the source code released by Google to find some clues (such as the frameworks/base/Media/JNI/directory ), the upper-layer Java program uses JNI to call functions in native C Programs.

Follow the steps below to implement a very simple JNI instance program:

1. Write the C Module to implement the dynamic library. (For more details about how to compile the C Module in Android, see Android compiling environment (1)-compile the helloworld module of native C.)
Add the new directory hellolib under the Development Directory and add the hellolib. C and Android. mk files. The content of hellolib. C is as follows:
# Include <JNI. h>

# Define log_tag "testlib"
# UNDEF log
# Include <utils/log. h>

Jniexport void jnicall java_com_test_testhellolib_printhello (jnienv * ENV, jobject jobj)
{

Logd ("Hello Lib! /N ");
}
 
Note that the function name here must follow the JNI specifications (you can also use the javah-JNI tool to generate the header file to ensure the correctness of the letter number). The name of java_com_test_testhellolib_printhello corresponds to the Java code, the package name is com. test: the class name is testhellolib, and the Native function name is printhello.
In addition, log printing methods such as logd and # define log_tag "testlib" adopt the log mechanism provided by Android, so that logs can be viewed through the android logcat tool.
The android. mk file used to compile the C module contains the following content:
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_src_files: =/

Hellolib. c

Local_c_includes: =/

$ (Jni_h_include)

Local_shared_libraries: =/

Libutils

Local_prelink_module: = false

Local_module: = libhello

Include $ (build_shared_library)
 
The meanings of some variables in this file are as follows:
Local_src_files-
Compiled source file
Local_c_includes-
Header file directory to be included
Local_shared_libraries-
External library required for Link
Local_prelink_module-
Whether prelink processing is required (see prelink for details: dynamic library Optimization-prelink technology, Android toolchain, and prelink tool: Android toolchain and bionic libc)
Local_module-
Compiled target object
Build_shared_library-
Indicates the library to be compiled into a dynamic library.

Next, return to the android top-level directory and execute make libhello to compile:
# Cd $ (your_android) & make libhello
Target thumb C: libhello <= Development/hellolib. c
Target sharedlib: libhello (Out/target/product/generic/obj/shared_libraries/libhello_intermediates/linked/libhello. So)
Target non-prelinked: libhello (Out/target/product/generic/symbols/system/lib/libhello. So)
Target Strip: libhello (Out/target/product/generic/obj/lib/libhello. So)
Install: Out/target/product/generic/system/lib/libhello. So

The dynamic sharing library libhello. So located in the out/target/product/generic/system/lib/directory can be obtained from the compilation result.

2. Write the Java module to call the C interface through JNI. For details about how to build the eclipse environment, refer to the detailed instructions in the android SDK documentation and the creation process of the hello Android program. Here we only provide the testhellolib. Java file to be modified:

Package com. test;
Import Android. App. activity;

Import Android. OS. Bundle;
Public
Class testhellolib extends activity {
/**
Called
When
The
Activity
Is
First
Created.
*/
@ Override
Public
Void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Printhello ();
}

Static {

System. loadlibrary ("hello ");

}

Private
Native
Void printhello ();

}
 
Note the bold part in the above Code: Private native void printhello () is used to declare a Native Interface, static {system. loadlibrary ("hello");} is used to load the libhello generated in the preceding step. so (note that the parameter of the loadlibrary method is not "libhello. so, But remove the "hello") after the prefix and suffix. The oncreate () method calls the printhello () interface.

This step generates the APK file testhellolib.apk that Android Developers are familiar.

3.integrated test testhellolib.apk and libhello. So. Run emulatorand upload testhellolib.apk and libhello. So to emulator. Note: Upload libhello. So to the/system/lib directory of emulator. Because the directory is read-only, run ADB remount before uploading:
# ADB remount
# ADB push out/target/product/generic/system/lib/libhello. So/system/lib
# ADB install testhellolib.apk

Next, you can see the installed testhellolib program in the simulator menu and run it.

Since the JNI interface printhello () has not been changed on the interface, you need to use the android logcat tool to verify its effect. Run "ADB logcat" to find the following log snippet:

I/activitymanager (
48): starting activity: intent {Action = android. intent. action. main categories = {android. intent. category. launcher} flags = 0x10200000 comp = {COM. test/COM. test. testhellolib }}
I/activitymanager (
48): Start proc com. Test for activity com. Test/. testhellolib: pid = 174 uid = 10024 gids = {}
D/dalvikvm (
174): trying to load lib/system/lib/libhello. So 0x43481c58
D/dalvikvm (
174): added shared lib/system/lib/libhello. So 0x43481c58
D/dalvikvm (
(174): No jni_onload found in/system/lib/libhello. So 0x43481c58
D/dalvikvm (
174): ++ not scanning '/system/lib/libwebcore. So' For 'printhello '(wrong cl)
D/dalvikvm (
174): ++ not scanning '/system/lib/libmedia_jni.so' For 'printhello '(wrong cl)
D/testlib (
174): Hello Lib!
I/activitymanager (
48): displayed activity com. Test/. testhellolib: 806 MS

This includes the log information for calling the printhello () interface, Where "d/testlib (
174): Hello Lib !" Is the information printed by printhello. So far, the android JNI instance verification is successfully completed.

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.