OPhone platform Native Development and JNI mechanism (1)

Source: Internet
Author: User

JNI Introduction

Java Native InterfaceJNI) is an important feature provided by Java. It enables code written in languages such as C/C ++ to be integrated with Java code running on the Java Virtual Machine JVM. Sometimes, Java cannot meet all your development needs. For example, you want to improve the efficiency of some key modules, or you must use a library written in C/C ++ or other Native languages. At this time, JNI can meet your needs to access these Native modules in Java code. The emergence of JNI allows developers to use both the cross-platform Java language, rich class libraries, convenient development, and the efficiency of Native language.

In fact, JNI is part of the JVM implementation. Therefore, Native and Java code run in the Host Environment of JVM. See figure 1. In addition, JNI is a two-way Interface: developers can not only access the Native module in Java code through JNI, but also embed a JVM in Native code, and access the Java module running in it through JNI. It can be seen that JNI acts as a bridge, linking JVM with the Native module, thus implementing mutual access between Java code and Native code. Using Java Virtual Machine on OPhone is a Dalvik Virtual Machine Specially optimized for embedded devices. Each time an application is started, the system creates a new process to run a Dalvik Virtual Machine. Therefore, each application is actually running in its own VM. Dalvik VM provides comprehensive support for JNI specifications, and supports enhancements from JDK 1.2 to JDK 1.6.

Before using JNI, developers must fully understand its advantages and disadvantages so that they can reasonably select technical solutions to achieve their goals. The advantages of JNI have been mentioned earlier. The shortcomings of JNI are also obvious: due to the use of Native modules, Java code will lose its original features such as cross-platform and type security. In addition, in JNI applications, Java code and Native code run in the same process space. For cross-process or even cross-host environment communications between Java and Native, you can consider using the socket, Web Service, and other IPC communication mechanisms.

Use JNI in OPhone Development

As described in the previous section, JNI is a two-way interface, so the interaction type can be divided into calling the Native module in Java code and calling the Java module in Native code. Next, we will use a Hello-JNI example to describe the development points of the two interaction methods.

Java calls the Native Module

The structure of the Hello-JNI example is very simple: first, we use Eclipse to create a Java project for the OPhone application and add a class of com. example. hellojni. HelloJni. This class is actually an Activity. Later we will create a TextView and display some text on it.

To use the Native module in Java code, the Native function must be declared first. In our example, open the HelloJni. java file and you can see the following declaration:


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.