Android NDK Development
Being the most popular mobile OS in the world, Android app development is naturally the top choice for mobile app developers today.. Android apps are typically written in Java, with its elegant object-oriented design. However to overcome the limitations of Java, such as memory management and performance, by programming directly into Android native interface. Android provides Native Development Kit (NDK) to support native development in C/C++, besides the Android Software Development Kit (Android SDK) which supports Java..
Since it’s first release in 2008 from Google and the Open Handset Alliance, Android has become one of the largest mobile platform in the world. With it’s flexibility features, customizable platform, over 67% of mobile application developers uses Android. As of April 2013, Google play had 850,000+ active applications available for download. Most of the android applications are written in Java with Android Software Development Kit (SDK). While writing pure Java applications, developers often feel the need to call native C/C++ libraries. This is where Android NDK plays a role.
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. 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. 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.
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
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.
Using the NDK for native app development on Android has both benefits and disadvantages, such as:
- The Android NDK allows the most performance-intensive pieces to be as close to the hardware as possible
- The developer must now handle some details that were previously handled by the Dalvik Virtual Machine (VM), making development more complicated
- With the Android NDK, the developer is responsible for optimizing native code and understanding which pieces of each app should be native
- NDK cannot access lots of APIs available in the Android SDK directly, and developing in NDK will always introduce extra complexity into your application.
Looking at the these pros and cons, its obvious that you shouldn’t always use the Android NDK in your app’s development, since it can add to your app’s complexity. Ideally, you should only use native app code when:
- Processing data or computing physics for games
- You need access to existing native libraries and high performance code
- You need to optimize computationally expensive processing such as audio, video, or image processing
- Seamless multi-device messaging – since the GCM service maps a single user to a notification key, you can use that key to send a single message to multiple devices owned by the user
In Google’s own words “Before downloading the NDK, you should understand that the NDK will not benefit most apps. As a developer, you need to balance its benefits against its drawbacks. Notably, using native code on Android generally does not result in a noticeable performance improvement, but it always increases your app complexity. In general, you should only use the NDK if it is essential to your app, never because you simply prefer to program in C/C++.”
As a developer you should know when to use the Android NDK when developing any app, since it has more drawbacks, particularly in terms of development and code complexity, than benefits. Only developers who need to access existing code or need to maximize performance can truly benefit from using native code in their Android apps. Everyone else should avoid using the Android NDK altogether.
For more information about the Android NDK Development, please drop an email to firstname.lastname@example.org