Simple use of Android-jni

Source: Internet
Author: User

What is JNI?
JNI is the abbreviation for Java Native Interface, which provides a number of APIs that enable the communication of Java and other languages (primarily c&c++). Starting with Java1.1, the JNI standard becomes part of the Java platform, which allows Java code to interact with code written in other languages. JNI was initially designed for local compiled languages, especially C and C + +, but it does not prevent you from using other programming languages, as long as the calling convention is supported. Using Java to interact with locally compiled code often loses platform portability. However, in some cases it is acceptable and even necessary. For example, use some old libraries to interact with the hardware, the operating system, or to improve the performance of the program. The JNI standard should at least ensure that local code can work in any Java Virtual machine environment.
This is the Baidu encyclopedia above the exact words, the introduction of experts, we all understand. Here is a summary of the knowledge of the JNI, more conducive to the understanding of JNI

The use of JNI needs to configure the NDK as the configuration SDK, as for the NDK is how to configure, because the time is limited, please go to Baidu, I do not say much here.

Theory above the knowledge here, the predecessors have summed up for us very well, how to achieve it in the theoretical basis of the predecessors, write the first of their own JNI project is what this article wants to say.
OK Come on;

Create a project Hellojni and create a click event for layout OnButtonClick

Then create the local method in the activity

Then it's time to create our C-language method. Right-click our project App->new->folder->jni Folder

This creates a folder dedicated to the Jni file.

Create a C language file in the Jni folder hello.c

HELLO.C Internal Code

Then create a android.mk file in the Jni folder for the NDK compilation

Local_module the name of the file generated after compilation
Local_src_files the name of the file being compiled
It is important to note that, by default, JNI does not support the X86 and MIPS CPU architectures, so we need to add support manually.
Create a application.mk file,

To support all CPU architectures, in fact, the main is X86 and MIPS architecture
Then you need to use the NDK to compile the project
Open the console CD to the JNI folder for the project, using ndk-build the instructions (note that I am here for Mac OS, if it is Windows or Linux using the instructions ndk-build.cmd )

Once this is done, you'll find something more in the project.

Finally, we need to refer to this libs in the Java code.

Run the Code click button to simply implement a Hello JNI toast

Source Address: http://download.csdn.net/detail/u011068996/9061135

Standing on the shoulders of giants, we can see farther

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple use of 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.