Android JNI instance

Source: Internet
Author: User



Android SDK
Not included
JNI
And how to support
JNI
No documentation is provided. However, since the entire Android platform
Is open-source, we can use
Google
Source code to find some clues (such
Frameworks/base/Media/JNI/
Directory ).
Java
Program passed
JNI
To call
Native C
Functions in the program.

 

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

 

1.

Write
C
Module to implement a dynamic library. (About how
Android
Compiling
C
For more details about the module, see compile native C Module in Android
.)

In
Development
Add a new directory under the Directory
Hellolib
And add
Hellolib. c
And
Android. mk
File.
Hellolib. c
The content is as follows:

# Include <JNI. h> </P> <p> # define log_tag "testlib" <br/> # UNDEF log <br/> # include <utils/log. h> </P> <p> jniexport void jnicall java_com_test_testhellolib_printhello (jnienv * ENV, jobject jobj) <br/>{< br/> logd ("Hello Lib! /N "); <br/>}

Note that the function name here must follow
JNI
(You can also use
Javah-JNI
Tool to generate header files to ensure the correctness of the letter number ).

Java_com_test_testhellolib_printhello
Corresponding
Java
Code,
Package
The name is
Com. Test
The class name is
Testhellolib
,
Native
The function name is
Printhello
.

In addition,
Logd
And
# Define log_tag "testlib"
Other Printing
Log
The method is
Android
Provided
Log
Mechanism to pass
Android
Of
Logcat
See Tool
Log
.

Used for compilation
C
Module
Android. mk
The file content is as follows:

Local_path: = $ (call my-DIR) <br/> include $ (clear_vars) </P> <p> local_src_files: =/<br/> hellolib. c </P> <p> local_c_includes: =/<br/> $ (jni_h_include) </P> <p> local_shared_libraries: =/<br/> libutils </P> <p> local_prelink_module: = false </P> <p> local_module: = libhello </P> <p> 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
-
Required?
Prelink
Processing (refer
Prelink
Detailed introduction :《
Dynamic library Optimization

-- Prelink (pre-connection) Technology



",
Android
Of
Toolchain, 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
Android
Top-level directory and execute
Make libhello
To compile:

# Cd $ (your_android) & make libhello <br/> Target thumb C: libhello <= Development/hellolib. c <br/> Target sharedlib: libhello (Out/target/product/generic/obj/shared_libraries/libhello_intermediates/linked/libhello. so) <br/> target non-prelinked: libhello (Out/target/product/generic/symbols/system/lib/libhello. so) <br/> Target Strip: libhello (Out/target/product/generic/obj/lib/libhello. so) <br/> install: Out/target/product/generic/system/lib/libhello. so


The compilation result is located
Out/target/product/generic/system/lib/
Dynamic shared library of directories
Libhello. So

 

2
. Write
Java
Module
JNI
Method call
C
Interface. Details
Eclipse
For environment setup, see
Android SDK
Detailed descriptions in this document, and
Hello android
The creation process of the program. here only
Testhellolib. Java
File:

Package COM. test; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; </P> <p> public class testhellolib extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> printhello (); <br/>}</P> <p> static {<br/> system. loadlibrary ("hello"); <br/>}</P> <p> private native void printhello (); <br/>}

Note the code section above:
Private native void printhello ()
Used to declare
Native
Interface;

Static {

System. loadlibrary ("hello ");

}


Used to load the generated
Libhello. So
(Note:
Loadlibrary
The method parameter is not
"Libhello. So"
, But after removing the prefix and suffix
"Hello"
),
Oncreate ()
Method is called
Printhello ()
Interface.


This step can generate
Android
Familiar to developers
APK
File:
Testhellolib.apk
.

 

3
. Integration Test
Testhellolib.apk
And
Libhello. So
. Run
Emulator
And set
Testhellolib.apk
And
Libhello. So
Upload
Emulator
. Note that
Libhello. So
Upload
Emulator
Of
/System/lib
Directory. Because the directory is read-only, you must execute
ADB remount
:

# ADB remount <br/> # ADB push out/target/product/generic/system/lib/libhello. So/system/lib <br/> # ADB install testhellolib.apk


Next, you can see the installed
Testhellolib
Program, run it.


Because
JNI
Interface
Printhello ()
There are no changes on the interface. To verify the effect, you need to use
Android
Of
Logcat
Tool to view. Run
"ADB logcat"
You can find the following
Log
Parts:

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



This includes the call
Printhello ()
Interface
Log
Information, where
"
D/testlib (174): Hello Lib !"

Yes
Printhello ()
The printed information. This is successfully completed
Android JNI
.

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.