JNI/NDK development guide (opening chapter)

I believe that many friends who have done Java or Android development often come into contact with JNI technology. Friends who have done Android will pass some complex logic and algorithms through native code (C or C++) for application security. Implementation, and then package it into a so dynamic library file, and provide a Java interface for the application layer to call, the main purpose of this is to provide the security of the application and prevent the logic of the application from being analyzed by criminals after being decompiled. Of course, packaging into so cannot be said to be completely safe, but compared to decompiling Java class bytecode files, the logic of disassembling the so dynamic library to analyze the program is much more complicated, and it is not so easy to be cracked. For example, the Android SDK of the location service, search service, LBS service, and push service provided by Baidu Open Platform, in addition to the jar package of the Java interface, there is also a .so file. This so is the dynamic implementation of the native interface defined by the Java layer. library. Interested friends can learn about the interface of related services: http://lbsyun.baidu.com/sdk/download .

In the past, the company had a JavaWeb project, in which there was a user registration module, which needed to verify the user's mobile phone number (the process is known to everyone). Since the number of users in this project is not large, there is no need to use the operator's SMS gateway interface, directly Purchased a 16-port SMS cat device and SIM card to solve this problem. Since the SMS cat device only provides the C interface, and Java cannot directly interact with the C language, JNI comes in handy. First, define the related native methods such as sending SMS, receiving SMS, and SMS sending queue in the Java layer. , and then use the javah command to generate the .h header file from the class bytecode file that defines the Java native interface (this will be discussed later), and finally use the C interface provided by the equipment manufacturer to implement the native method of java, and then compile it into a dll Or so dynamic library, which can be used by Java programs.

JNI is also often used in the Cocos2d-x game engine. The engine is developed in pure C++ and is cross-platform. Relying on the cross-platform feature of C++, the game can be packaged and released to the For different platforms (IOS, Android, WinPhone, Blackberry, Linux, Windows), the details of packaging and distribution are not discussed here. If the game is to be released to the Android platform, the interaction between the C++ layer and the Java layer is indispensable during the development process, such as opening a web page, playing a video or opening a new window in the game. These are very troublesome to implement in the C++ layer. If you use the API provided by the Android application layer, it becomes quite easy. So at this time, you have to write JNI to complete the requirements of these functions. Of course, these commonly used JNI operations are encapsulated by the Cocos2d-x engine, and the related interfaces are defined in the class JniHelper.cpp, which can be used directly. (More examples will follow later)

Although the current Internet of Things and smart home industries are still in their infancy, with the development of technological innovation and continuous improvement in this era, imagine that in 5 years, the Internet of Things and smart home industries will truly mature. Due to the open source of the Android system , will naturally be adopted by major hardware vendors, which is equivalent to the Android smartphone market in recent years, and may still be in the dominant position of mobile smart terminals. You might ask, but what does this have to do with JNI and ? When various devices are connected to the Internet, applications for human-computer interaction are naturally indispensable. When an application needs to call a hardware-specific function, it can only be used by upper-layer applications by encapsulating the JNI interface of the corresponding function in C or C++. For example, when using the app in the mobile phone to control all smart electronic devices such as lights, curtains, refrigerators, air conditioners, etc., the application must communicate with the underlying hardware. As for the operation control of various smart devices, it is naturally realized by the manufacturer. , they only need to provide the interface to operate the relevant functions of the device. Although the manufacturer will encapsulate the JNI interface, we also need to understand the principle of jni and java communication, so that we can quickly locate the problem when we encounter problems during the development process. This is just some of my guesses about the future development of the Internet of Things or smart home, welcome everyone to discuss!

Having said so much, I want to explain only one purpose: JNI will have a wide range of uses in the future, and accumulating technology now means accumulating wealth for the future! Interested friends come and learn JNI development with me. Later, I will write a series of JNI/NDK development articles from shallow to deep, and systematically introduce the related technologies involved in JNI development. First, I will talk about some basic knowledge of JNI development. Each knowledge point will be combined with a case to get through. Finally, I will talk about NDK development. NDK mainly talks about the configuration of the compilation environment, the writing of Android.mk, the compilation of modules and the NDK compilation system. Introduced, because the development of NDK interface is the same as JNI (no knowledge of NDK development and application is discussed here). Interested friends please pay attention to my blog. Here is an outline of the follow-up article:

1. JNI development process (dynamic libraries compiled under different operating system environments) (use a HelloWorld example to kick off JNI development)

2. Rules for JVM to find java native methods

3. JNI data types and the mapping relationship with Java data types

4. JNI string processing

5. Access arrays (basic type arrays and object arrays)

6. C/C++ access to AVA static methods and instance methods

7. C/C++ access JAVA instance variables and static variables

8. Calling constructor and instance methods

9. JNI call performance test and optimization

10. JNI local references, global references and weak global references

11. Exception handling

12. Multithreading

13. Native code embedded in the JVM

14. Some precautions for JNI development

15. Common error sharing (local reference table overflow, local thread is not attached to the JVM problem)

16. NDK development environment construction

17. Detailed explanation of NDK compilation system

18. Comprehensive examples of NDK development (Android, Cocos2d-x)

Guess you like

Origin blog.csdn.net/shangsongwww/article/details/122493034