Book Image

Android Native Development Kit Cookbook

By : Liu Feipeng
Book Image

Android Native Development Kit Cookbook

By: Liu Feipeng

Overview of this book

Building Android applications would usually mean that you spend all of your time working in Java. There are however times when this is not the most efficient or best method for the application being built. This is where Android NDK comes in. Android NDK allows the developer to write in Native C/C++, giving you the power to reuse code and libraries and also, in most cases, increase the speed and efficiency of your application.The "Android Native Development Kit Cookbook" will help you understand the development, building, and debugging of your native Android applications. We will discover and learn JNI programming and essential NDK APIs such as OpenGL ES, and the native application API. We will then explore the process of porting existing libraries and software to NDK. By the end of this book you will be able to build your own apps in NDK apps."Android Native Development Kit Cookbook" begins with basic recipes that will help you in the building and debugging of native apps, and JNI programming. The recipes cover various topics of application development with Android NDK such as OpenGL programming and Multimedia programming. We will begin with a simple recipe, Hello NDK, before moving on to cover advanced topics with recipes on OpenGL ES that focus on 2D and 3D graphics, as well as recipes that discuss working with NDK and external APIs. If you are looking for ways to make your application available in Android and take measures to boost your application's performance, then this Cookbook is for you.
Table of Contents (16 chapters)
Android Native Development Kit Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up an Android NDK development environment in Ubuntu Linux


This recipe depicts how to set up an Android NDK development environment in Ubuntu Linux.

Getting ready

Check your Ubuntu version and make sure it is version 8.04 or later.

GNU C Library (glibc) 2.7 or above is required. It is usually installed with Linux by default. Two simple methods can check the version of glibc:

  1. Start a terminal, and enter ldd --version. This will print the version of ldd and glibc:

  2. We can execute the library as an application. Start a terminal, locate the library location, and then enter the following command:

    <glibc library location>/<glibc library>. 
    

    The following output will be displayed:

  3. We need to enable 32-bit application execution if we are using a 64-bit machine. Start a terminal, and enter the following command:

    sudo apt-get install ia32-libs
    
  4. Install JDK 6 or above. At a terminal, enter the command sudo apt-get install openjdk-6-jdk, or alternatively we can enter sudo apt-get install sun-java6-jdk. After installation, we need to add the JDK path to the PATH environment variable by adding the following lines to ~/.bashrc:

    export JDK_PATH=/usr/local/jdk1.7.0/bin
    export PATH=$PATH:$JDK_PATH

We will use Eclipse as our IDE. Please refer to the Setting up an Android NDK development environment in Windows recipe for instructions.

How to do it…

The following steps indicate the procedure of setting up an Android NDK development environment on Ubuntu Linux:

  1. Follow steps 1 to 6 of the Setting up an Android NDK development environment in Windows recipe to install the ADT plugin for Eclipse.

  2. Download Android SDK from http://developer.android.com/sdk/index.html, then extract the downloaded package.

  3. Append the following lines to ~/.bashrc:

    export ANDROID_SDK=<path to Android SDK directory>
    export PATH=$PATH:$ ANDROID_SDK/tools:$ANDROID_SDK/platform-tools
  4. Follow steps 9 and 10 of the Setting up an Android NDK development environment in Windows recipe to configure the SDK path at Eclipse, and download additional packages.

  5. Download the latest version of Android NDK from http://developer.android.com/tools/sdk/ndk/index.html, then extract the downloaded file.

  6. Change the lines that you appended to ~/.bashrc in step 3:

    export ANDROID_SDK=<path to Android SDK directory>
    export ANDROID_NDK=<path to Android NDK directory> 
    export PATH=$PATH:$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$ANDROID_NDK
  7. Start a new terminal, then go to the samples/hello-jni directory in NDK. Type the command ndk-build. If the build is successful, it proves that the NDK environment is set up correctly:

How it works…

We first set up Android SDK and then Android NDK. Ensure that the path is set properly, so that the tools can be accessed without referring to the SDK and NDK directories.

The .bashrc file is a startup file read by the bash shell when you start a new terminal. The export commands appended the Android SDK and NDK directory locations to the environment variable PATH. Therefore, every time a new bash shell starts, PATH is set properly for SDK and NDK tools.

There's more…

The following are a few more tips on setting up an NDK development environment:

  • Configure Path at Startup File: We append to the SDK and NDK paths to the PATH environment variable at ~/.bashrc file. This assumes that our Linux system uses the bash shell. However, if your system uses another shell, the startup file used may be different. The startup files used by some commonly used shells are listed as follows:

    • For C shell (csh), the startup file to use is ~/.cshrc.

    • For ksh, the startup file to use can be obtained using the command echo $ENV.

    • For sh, the startup file to use is ~/.profile. The user needs to log out of the current session and log in again for it to take effect.

  • Switch JDK: In Android development, we can either use Oracle Java JDK or OpenJDK. In case we run into issues with any one of the JDKs, we can switch to another Java JDK, if we have installed both of them.

    • To check which JDK the system is currently using, use the following command:

      	$update-java-alternatives -l
      
    • To switch between two JDKs, use the following command:

      	$sudo update-java-alternatives -s <java name>
      

    The following is an example for switching to Oracle JDK 1.6.0:

    $sudo update-java-alternatives -s java-1.6.0-sun