Android developer Tim Mackenzie notes that native code should be used only when necessary. Find out when it is needed.
There are many ways to create apps for Android devices, ranging from very high-level cross-platform engines to writing code native to specific processors. The Native Development Toolkit (NDK) allows using C and C++ in Android apps, but why you would want to and how to do so is a mystery to many developers.
What is native code for Android devices, and what is the NDK?
Android apps run within the Dalvik virtual machine, which interprets device-agnostic, cross-platform commands into instructions for the specific device that it is running on. Code written in Java (or in other high-level languages that are converted into Java) does not need to handle machine-specific details; the virtual machine manages details for the processor, graphics, and even memory. You don't need to handle any communication to processes or modules outside of the virtual machine.
In most cases, the speed and memory overhead is a worthwhile tradeoff. In some cases, developers need the absolute fastest performance possible. The NDK allows embedding C and C++ components within Android apps, allowing the most performance-intensive pieces to be as close to the hardware as possible. This comes at a cost, though — using native code complicates development. There are more tools to use and infrastructure to set up. Also, some details that were handled by the Dalvik virtual machine must now be handled by the developer. For these reasons, native code should be used only when necessary.
When native code is needed
Many times the choice of high-level language and tools depends on the developer's skill set and the existing assets (such as game logic or graphic libraries); however, the Android team admonishes that familiarity with C or C++ is not a sufficient reason to use the NDK.
There are times that using native code can be advantageous, such as processing data or computing physics and graphics for games. Access to existing native libraries, as well as high-performance code, can also be good reasons.
The increased complexity from using native code should be balanced with a suitable performance increase. Native code isn't inherently faster — the developer is responsible for optimizing the native code and understanding which pieces of each app should be native.
Uses for the native code
The Android team makes it clear that most developers shouldn't need to use the NDK. In other words, if you don't know what native code is, and if you don't have a clear reason to use it, stay away from it.
Game engine developers often dive right in to native code. The limited speed and memory of mobile devices means native code may be necessary to squeeze every bit of potential out for them. There are a number of established high-performance game engines based on C++ that are used to develop cross-platform games; this doesn't mean that game development requires working in C++.
Another notable use of the NDK is when using a platform that cross-compiles into native code. While the platform builders would need to know the NDK in detail, the developer would not. For example, some game engines allow developers to use Lua or similar high-level scripting languages to create the game, and handle the interaction with the NDK internally.
While you might think that interfacing with external hardware would require using native code, the Accessory Development Kit handles that.
One final example of when to use native code is computationally expensive math, such as audio, video, or image processing. This is not likely to be an area for developers to be casually involved in and is most often used in specialized applications.
The NDK isn't for everyone. However, developers who need access to existing code or need to maximize performance can benefit from using native code in their Android applications.