Android--jni Simple Example parsing

Source: Internet
Author: User

Recently the project iteration several versions, at present relatively easy, although the project is idle, but the person cannot be too idle, otherwise is obsolete. A journey of a few, quantitative changes caused qualitative change, learning without thinking is not .... All right, all right. Can't make up, let us go straight to the point, or say a word, welcome everyone to exchange learning, common progress, welcome to throw bricks, even if you throw me do not answer

First, build a new Android project, the same as the previous project

Second, create a jnicall. Java class:


Create a local method that acts as an interface for us to call the C + + program, and we statically load the. So file in static mode.

Third, add local support

Select items, right----->android Tools---->add Native support,


This will bring up an Add Android Native Support dialog box, at which point we enter the name of our so file to be loaded OK:


At this point, the project will be one more, the Jni folder, containing a my_jni.cpp and android.mk files,


Where there is only one line of code in the My_jni.cpp file, the JNI library is introduced:


Let's take a look at the Android.mk file:


There are several properties in this autogenerated makefile file:

Local_path is the directory in which C + + code is located, which is our JNI directory.

include$ (clear_vars) represents cleanup of previously dependent class libraries

Local_module is the name of the library to compile, which is the name we enter in the Add Android Native Support dialog box. The compiler automatically adds Lib to the front, followed by. So.

Local_src_files is a C + + file to compile.

include$ (build_shared_library) The representative compiles a dynamic library containing $ (build_static_library), which compiles a static library.

Compile JNI, below we need to generate the. h header file:

Locate the Bin folder in the Android project, locate the Classes folder, and Eclipse automatically generates the bytecode files for us under this folder, first switch to the directory using the CD command at the command line, and then

After using the Javah command, generate the. h header file that we want to get:


This header file generated by the name, we can be modified, as long as the legal can, let us look at the generated. h file:

V. Write a CPP file that generates a local library:


Here we need to introduce the header file, the key is we look at the format of the method name:

Java_com_example_jnidemo_jnicall_myjni

Java: Fixed notation, must be so

Com_example_jnidemo: For the package name that contains the native method

jnicall: The name of the class that contains the native method

myjni: For native method name

These method names we must correspond, otherwise there will be an error:Java.lang.UnsatisfiedLinkError:Native method not found

Vi. generate. So files (this step is not necessary, because we can run it, Eclipse can automatically build the. so file with the project, of course, we can also compile their own, the following describes the process of individual compilation)

Start with the command CD into the directory where the project resides, and then generate the. So file from the Ndk-build command:


Description so file generated, refresh the project, and then we look at the directory changes:


Seven: Call the native method:


Eight, the operation of the project, the effect



So far, a simple JNI program has been completed




Android--jni Simple Instance parsing

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.