The Android NDK is a set of tools that allows to embed native machine code compiled from C and/or C++ source files into the application package or create a purely-native Android aplication.
This added difficulty for developers -compared to using the usual framework- can be justified by some of the following reasons.
The first one to use NDK is that may improve application performance. This is usually true for computational and graphically intensive applications, such as game engines, physics simulation or signal processing.
The performance improvements can come from:
- 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) which means it is much slower. Although this may be changing recently because of the new Android RunTime (ART) -the successor and replacement for Dalvik.
- Native code allows to make use of some processor features not accessible at Android SDK, such as NEON, Single Instruction Multiple Data (SIMD) technology, allowing to process data in parallel.
- It's possible to optimize the critical code at an assembly level.
Using native code can also have disadvantages in terms of performance. Calling JNI methods introduces inefficiencies in Dalvik VM performance and since the code is compiled, no runtime optimization can be applied.
The second reason to use C/C++ in Android programming is the portability of apps. The usage of C/C++ allows to reuse great amount of code in order to port the app to other systems.
Use existing libraries
The last reason to use C/C++ is the possibility of taking advantage of existing libraries and code developed from other systems.