Unit - 8 Using NDK

Lesson Plan: Unit - 08
Subject: P15A2AAD - Android Application Development
Topic of Study: Using NDK
Grade/Level: Master of Computer Applications
Objective: Using NDK
Time Allotment: 55 Minutes
  • Determining When to Use the Android NDK:
    • Android applications are primarily written in Java.
    • Some developers need or prefer to leverage native C or C++ libraries.
    • So, the Android Native Development Kit (NDK) provides the tools necessary to include and use native libraries within your Android applications.
    • Exa.
    • The Android NDK provides tool- chain support for compiling and using native C and C++ libraries in conjunction with your Android Java applications.
    • Two reasons…
      • To perform processor-intensive operations such as complex physics, which can be implemented more efficiently in C and C++.
      • To leverage existing code, usually in the form of shared or proprietary C or C++ libraries, when porting is not idea. This is often the case when trying to support multiple platforms with a single code base.
    • Note: The native libraries created by the Android NDK can be used only on devices running Android 1.5 and higher.
  • Main Components of NDK
    • You should have an understanding of the following components as you build your NDK app:
    • Native shared libraries: 
      • The NDK builds these libraries, or .so files, from your C/C++ source code.
    • Native static libraries: 
      • The NDK can also build static libraries, or .a files, which you can link into other libraries.
    • Java Native Interface (JNI): 
      • The JNI is the interface via which the Java and C++ components talk to one another. 
    • Application Binary Interface (ABI): 
      • The ABI defines exactly how your app's machine code is expected to interact with the system at runtime. The NDK builds .so files against these definitions. Different ABIs correspond to different architectures: The NDK includes ABI support for 32-bit ARM, AArch64, x86, and x86-64. 
    • Manifest: 
      • If you are writing an app with no Java component to it, you must declare the NativeActivity class in the manifest. 
  • Getting Started with the NDK
    • The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android.
    • The NDK may not be appropriate for most novice Android programmers who need to use only Java code.
    • The ability to use native code in Android apps can be particularly useful to developers who wish to do one or more of the following:
      • Port their apps between platforms.
      • Reuse existing libraries, or provide their own libraries for reuse.
      • Increase performance in certain cases, particularly computationally intensive ones like games.
    • Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. 
    • Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework. 
    • Android Studio's default build tool to compile native libraries is CMake.
    • However, if you are creating a new native library, you should use CMake.
  • Download the NDK and Tools
    • To compile and debug native code for your app, you need the following components:
    • 1. The Android Native Development Kit (NDK): 
      • a set of tools that allows you to use C and C++ code with Android.
    • 2. CMake: 
      • an external build tool that works alongside Gradle to build your native library. Note: You do not need this component if you only plan to use ndk-build.
    • 3. LLDB: 
      • the debugger Android Studio uses to debug native code.
    • Install the NDK, CMake, and LLDB:
      • Android Studio installs all versions of the NDK in the android-sdk/ndk/ directory.
      • To install CMake, LLDB, and the default NDK in Android Studio, do the following:
        • Step 01: With a project open, click Tools > SDK Manager.
        • Step 02: Click the SDK Tools tab.
        • Step 03: Select the LLDB, NDK (Side by side), and CMake checkboxes.
        • Step 04: Click OK.
        • Step 05: A dialog box tells you how much space the NDK package consumes on disk.
        • Step 06: Click OK.
        • Step 07: When the installation is complete, click Finish.
      • Your project automatically syncs the build file and performs a build. Resolve any errors that occur.
  • 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.
  • Exploring the Android NDK:
    • The Android NDK contains a number of different tools and files, specifically
    • Native system libraries and headers that are forward compatible with the Android platform (1.5 and beyond)
    • Tools for compiling and linking native libraries for ARMv5TE and ARMv7-A de-vices (x86 coming soon)
    • Build files for embedding native libraries into Android applications
    • Native debugging using ndk-gdb
    • NDK documentation in the /docs subdirectory
    • NDK sample applications in the /samples subdirectory
  • How it Works:
    • You should have an understanding of the following components as you build your app:
    • ndk-build: 
      • The ndk-build script launches the build scripts at the heart of the NDK. 
      • These scripts: 
        • Automatically probe your development system and app project file to determine what to build.
        • Generate binaries.
        • Copy the binaries to your app's project path.
      • You should have an understanding of the following components as you build your app:
    • Java: 
      • From your Java source, the Android build process generates .dex (Dalvik EXecutable) files, which are what the Android OS runs in the Dalvik Virtual Machine (“DVM”). 
      • Even if your app contains no Java source code at all, the build process still generates a .dex executable file within which the native component runs. 
      • When developing Java components, use the native keyword to indicate methods implemented as native code. 
      • For example:
        • public native int add(int  x, int  y);
  • You should have an understanding of the following components as you build your app:
    • Native shared libraries: 
      • The NDK builds these libraries, or .so files, from your native source code. 
    • Native static libraries: 
      • The NDK can also build static libraries, or .a files, which you can link against other libraries.
    • Java Native Interface (JNI): 
      • The JNI is the interface via which the Java and C++ components talk to one another. 
    • Application Binary Interface (ABI): 
      • The ABI defines exactly how your app's machine code is expected to interact with the system at runtime. 
      • The NDK includes ABI support for ARMEABI (default), MIPS, and x86. 
    • Manifest: 
      • If you are writing an app with no Java component to it, you must declare the NativeActivity class in the manifest. 
  • Drawbacks of using NDK…
    • Increased code complexity
    • Increased debugging complexity
    • Performance overhead for each native code call
    • More complex build process
    • Required to be versed in both Java and C/C++ i.e you can’t write entire applications in C or C++.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form