Simple use of Android-JNI and android-jni

Source: Internet
Author: User

Simple use of Android-JNI and android-jni

What is JNI?
JNI is short for Java Native Interface. It provides several APIs for Java to communicate with other languages (mainly C & C ++ ). Since java, the JNI standard has become part of the Java platform. It allows java code to interact with code written in other languages. JNI was initially designed for locally compiled languages, especially C and C ++, but it does not prevent you from using other programming languages, as long as the call conventions are supported. Using java to interact with locally compiled code usually results in loss of platform portability. However, in some cases, this is acceptable or even necessary. For example, you can use some old libraries to interact with hardware and operating systems, or to improve program performance. The JNI standard must at least ensure that local code can work in any Java virtual machine environment.
This is the original story of Baidu encyclopedia. It is well understood by experts. The following is a summary of JNI, which is more conducive to your understanding of JNI.

To use JNI, You need to configure the NDK first, just like configuring the SDK. As for how to configure the NDK, please go to Baidu because of limited time. I will not talk about it here.

The theoretical knowledge is here, and our predecessors have already summarized it very well. How can we implement it on the theoretical basis of our predecessors, write your first JNI project in this article.
OK Come On;

Create a project HelloJni and create a click event onButtonClick for layout.

Create a local method in the Activity.

Then we should create our C language method. Right-click our project app-> New-> Folder-> JNI Folder

In this way, a folder is generated to store JNI files.

Create a c language file Hello. c In the jni folder

Hello. c internal code

Create an Android. mk file in the jni folder for NDK compilation.

LOCAL_MODULE is the name of the file generated after compilation.
LOCAL_SRC_FILES is the name of the compiled file.
Note that by default, JNI does not support the X86 and Mips CUP architectures, so we need to manually add support.
Create an Application. mk file,

All CPU architectures are supported, mainly X86 and Mips architectures.
Then you need to use NDK to compile the project.
Open console cd to the project's jni folder, usendk-buildCommand (note that I am a mac OS here. If it is a windows or linux Commandndk-build.cmd)

After completing this step, you will find that there are more things in the project.

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

By running the code, you can click the button to implement a Toast of Hello JNI.

Source Code address: http://download.csdn.net/detail/u011068996/9061135

Standing on the shoulders of giants, we can see farther

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.