- Android Native Development Kit Cookbook
- Feipeng Liu
- 431字
- 2021-07-27 18:07:25
Introduction
Programming with Android NDK is essentially writing code in both Java and native languages such as C, C++, and assembly. Java code runs on Dalvik Virtual Machine (VM), while native code is compiled to binaries running directly on the operating system. Java Native Interface (JNI) acts like the bridge that brings both worlds together. This relationship between Java code, Dalvik VM, native code, and the Android system can be illustrated using the following diagram:

The arrow in the diagram indicates which party initiates the interaction. Both Dalvik VM and Native Code run on top of Android system (Android is a Linux-based OS). They require the system to provide the execution environment. JNI is part of Dalvik VM, which allows Native Code to access fields and invoke methods at Java Code. JNI also allows Java Code to invoke native methods implemented in Native Code. Therefore, JNI facilitates the two-way communication between Native Code and Java Code.
If you are familiar with Java programming and C, or C++, or assembly programming, then learning programming with Android NDK is mostly learning JNI. JNI comes with both primitive and reference data types. These data types have their corresponding mapping data types in Java. Manipulating the primitive types can usually be done directly, since a data type is normally equivalent to a native C/C++ data type. However, reference data manipulation often requires the help of the predefined JNI functions.
In this chapter, we'll first cover various data types in JNI and demonstrate how to invoke native methods from Java. We then describe accessing the Java fields and calling Java methods from the native code. Finally, we will discuss how to cache data to achieve better performance, how to handle errors and exceptions, and how to use assembly in native method implementation.
Every recipe in this chapter comes with a sample Android project that illustrates the topic and related JNI functions. Because of the space constraint, we cannot list all the source code in the book. The code is a very important part of this chapter and it is strongly recommended that you download the source code and refer to it when going through the recipes.
Tip
JNI is a complex topic, and we tried to cover the most essential parts of it in the context of Android NDK programming. However, a single chapter is not enough to provide all the details. Readers may want to refer to Java JNI Specification at http://docs.oracle.com/javase/6/docs/technotes/guides/jni/ or the Java Native Interface: Programmer's Guide and Specification book at http://java.sun.com/docs/books/jni/. For Android-specific information, you can refer to JNI Tips at https://developer.android.com/guide/practices/jni.html.
- MATLAB圖像處理超級學習手冊
- Oracle 12c中文版數據庫管理、應用與開發實踐教程 (清華電腦學堂)
- Python金融數據分析
- INSTANT CakePHP Starter
- Learning Python by Building Games
- 快速念咒:MySQL入門指南與進階實戰
- Java網絡編程核心技術詳解(視頻微課版)
- Python3.5從零開始學
- Scratch趣味編程:陪孩子像搭積木一樣學編程
- Hands-On Kubernetes on Windows
- 創意UI:Photoshop玩轉APP設計
- 遠方:兩位持續創業者的點滴思考
- Practical Predictive Analytics
- Mastering Unreal Engine 4.X
- PostgreSQL 12 High Availability Cookbook