- Android Native Development Kit Cookbook
- Feipeng Liu
- 580字
- 2021-07-27 18:07:23
Introduction
Android NDK is a toolset that allows developers to implement a part of or an entire Android application in a native language, such as C, C++, and assembly. Before we start our journey to NDK, it is important to understand the advantages of NDK.
First of all, NDK may improve application performance. This is usually true for many processor-bound applications. Many multimedia applications and video games use native code for processor-intensive tasks.
The performance improvements can come from three sources. Firstly, the native code is compiled to a binary code and run directly on OS, while Java code is translated into Java byte-code and interpreted by Dalvik Virtual Machine (VM). At Android 2.2 or higher, a Just-In-Time (JIT) compiler is added to Dalvik VM to analyze and optimize the Java byte-code while the program is running (for example, JIT can compile a part of the byte-code to binary code before its execution). But in many cases, native code still runs faster than Java code.
The second source for performance improvements at NDK is that native code allows developers to make use of some processor features that are not accessible at Android SDK, such as NEON, a Single Instruction Multiple Data (SIMD) technology, allowing multiple data elements to be processed in parallel. One particular coding task example is the color conversion for a video frame or a photo. Suppose we are to convert a photo of 1920x1280 pixels from the RGB color space to the YCbCr color space. The naive approach is to apply a conversion formula to every pixel (that is, over two million pixels). With NEON, we can process multiple pixels at one time to reduce the processing time.
The third aspect is that we can optimize the critical code at an assembly level, which is a common practice in desktop software development.
Tip
The advantages of using native code do not come free. Calling JNI methods introduces extra work for the Dalvik VM and since the code is compiled, no runtime optimization can be applied. In fact, developing in NDK doesn't guarantee a performance improvement and can actually harm performance at times. Therefore, we only stated that it may improve the app's performance.
The second advantage of NDK is that it allows the porting of existing C and C++ code to Android. This does not only speed up the development significantly, but also allows us to share code between Android and non-Android projects.
Before we decide to use NDK for an Android app, it is good to know that NDK will not benefit most Android apps. It is not recommended to work in NDK simply because one prefers programming in C or C++ over Java. NDK cannot access lots of APIs available in the Android SDK directly, and developing in NDK will always introduce extra complexity into your application.
With the understanding of the pros and cons of NDK, we can start our journey to Android NDK. This chapter will cover how to set up Android NDK development in Windows, Ubuntu Linux, and Mac OS. For developers who have set up an Android NDK development environment before, a recipe with detailed steps of how to update an NDK development environment is provided. At the end of the chapter, we will write a Hello NDK program with the environment setup.
- Spring 5.0 By Example
- 單片機C語言程序設(shè)計實訓(xùn)100例:基于STC8051+Proteus仿真與實戰(zhàn)
- Access 2010數(shù)據(jù)庫基礎(chǔ)與應(yīng)用項目式教程(第3版)
- Mastering Julia
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實訓(xùn)版)
- 用戶體驗增長:數(shù)字化·智能化·綠色化
- Python算法從菜鳥到達人
- 好好學(xué)Java:從零基礎(chǔ)到項目實戰(zhàn)
- Building Serverless Architectures
- 從程序員角度學(xué)習(xí)數(shù)據(jù)庫技術(shù)(藍橋杯軟件大賽培訓(xùn)教材-Java方向)
- Oracle 12c從入門到精通(視頻教學(xué)超值版)
- 量子計算機編程:從入門到實踐
- 讓Python遇上Office:從編程入門到自動化辦公實踐
- TensorFlow程序設(shè)計
- React Native -Building Mobile Apps with JavaScript