Determining When to Use the Android NDK - Android

Most Android applications are written solely in Java using the Android SDK and run within the Dalvik VM. Most applications run smoothly and efficiently in this fashion. However, there are situations when calling into native code from Java can be preferable. The Android NDK provides tool-chain support for compiling and using native C and C++ libraries in conjunction with your Android Java applications. This is usually done for one of two reasons:

  • To perform processor-intensive operations such as complex physics, which can be implemented more efficiently in C and C++, offering substantial performance improvements.
  • To leverage existing code, usually in the form of shared or proprietary C or C++ libraries, when porting is not ideal. This is often the case when trying to support multiple platforms with a single code base.

Calling into native code from within Java involves some tradeoffs. Application developers must consider their application design carefully, weighing the benefits of using the NDK versus the drawbacks, which include

  • Increased code complexity
  • Increased debugging complexity
  • Performance overhead for each native code call
  • More complex build process
  • Developers required to be versed in both Java and C/C++

Although developers cannot write entire applications in C or C++, they can leverage the benefits of these compiled languages from Java when the situation merits. If your application requires complex math, physics, graphics algorithms, or other intensive operations, the Android NDK might be right for your project. Your libraries can take advantage of a number of stable native C and C++ APIs, including

  • C library headers (libc)
  • Math library headers (libm)
  • The zlib compression library headers (libz)
  • 3D graphics library headers (OpenGL ES 1.1 and 2.0)
  • A cpu features library for detecting device cpu features at runtime
  • Other headers for C++, logging, JNI, and more

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics