Android development – Marrying Android with NDK Part1

Reading Time: 6  min

Introduction

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.

In this paper we will introduce you to the Android NDK and it’s capabilities through the following topics:

  • Why is NDK important?
  • What is NDK and what are its components?
  • NDK and JNI with an example
  • Complexities integrating NDK
  • Solutions and  Good practices
  • Conclusion

Why is NDK Important?

  • NDK facilitates the reuse of powerful pre-built C/C++ libraries in your Android application.
  • NDK allows the Android developer to optimize specified algorithms to overcome the memory management and performance limitations of Java.
  • NDK allows you to access lower-level libraries without the need for Java APIs to access them.

It makes sense to run highly optimized, self-contained routines which do not require much RAM, such as signal processing routines, physics simulations in their native form as these components might run better in native or at least might not be worth to port and optimize if gains are small.

What is NDK?

Android applications runs in the Dalvik Virtual Machine. And Android NDK is a toolset that lets you embed components that make use of native code languages such as C and C++. Using NDK, developers can build and cross-compile native code for the device specific architecture.

Components of  NDK?

  • Toolchain to generate native code libraries.
  • Way to embed native libraries into an application bundle, so that can be deployed on Android devices.
  • Provides headers for native system and Android native libraries APIs.
  • And more importantly, it has documentation, samples and tutorials.

Latest NDK (r8), supports ARMv5TE, ARMv7-A, x86 and MIPS.

JNI

Java offers  Java  Native  Interface as a framework connecting Java to the native code. Before using NDK, you should be  familiar with JNI.

Using the NDK

Overview

 

  1. Create .java class representing the native code
  2. Create the native code header file
  3. Native code implementation by writing C code
  4. Compile and build shared library
  5. Use native methods from java class.

Understanding NDK and JNI with an example

Follow below steps to develop JNI libraries,

The steps that you have to take to develop JNI libraries for Android phones are simple in principle. They are outlined below.

  • Install the JNI/NDK package from Google
  • Create your Android project
  • Make a JNI folder in your Android project root directory (called ‘jni’)
  • Put your JNI sources in the ‘jni’ folder
  • Create an ‘Android.mk’ file, and place it in the ‘jni’ folder
  • Open a command line terminal and navigate to the root directory of your Android project.
  • Execute ‘ndk-build’, (if it’s in your PATH variable) or execute ‘/path/to/command/ndk-build’
  • The ‘ndk-build’ command creates the binary for your library and puts it in the proper folder.
  • Switch to Eclipse, Refresh the ‘Project Explorer View’ (F5)
  • Rebuild the project
  • Run your project testing your JNI library.
Stay Updated
Please enable JavaScript in your browser to complete this form.
LinkedIn
Share
Copy link
URL has been copied successfully!

Other stories you may enjoy...

One Year In: Technology Success Stories from the Pandemic

This time last year, US companies were forced to face a new and unsettling reality: business as usual was no longer an option. I wrote then about how businesses could shift their...

How to Work with Your Remote Development Team

Working with remote teams to develop and release new products has become the norm for almost all aspects of software development.  Nowhere is that more true than in the mobile...

Think You Know Your App Dev Needs? Think Again.

The pace of change in mobile app development has been mind-blowing. Here at Apexon, we’ve been working on mobile apps since their inception. With every project we learn...