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 Windows


In this recipe, we will explore how to set up an Android NDK development environment in Windows.

Getting ready

Check the Windows edition and system type. An Android development environment can be set up on Windows XP 32-bit, Windows Vista 32- or 64-bit, and Windows 7 32- or 64-bit.

Android development requires Java JDK 6 or above to be installed. Follow these steps to install and configure Java JDK:

  1. Go to the Oracle Java JDK web page at http://www.oracle.com/technetwork/java/javase/downloads/index.html, and choose JDK6 or above for your platform to download.

  2. Double-click on the downloaded executable, and click through the installation wizard to finish the installation.

  3. Go to Control Panel | System and Security | System | Advanced system settings. A System Properties window will pop up.

  4. Click on the Environment Variables button in the Advanced tab; another Environment Variables window will pop up.

  5. Under System variables, click on New to add a variable with the name as JAVA_HOME and value as the path of the JDK installation root directory. This is shown as follows:

  6. Under System variables, scroll to find the PATH (or Path) environment variable. Insert %JAVA_HOME%\bin; at the beginning of the value. If no PATH or Path variable exists, create a new variable with the value set to %JAVA_HOME%\bin. Click on OK all the way through to dismiss all windows.

  7. To verify whether JDK is installed and configured correctly, start a new command-line console, and enter javac -version. If JDK is configured correctly, you will get the Java version in the output.

Cygwin is a Linux-like environment for Windows to run software available on Linux. Android NDK development requires Cygwin 1.7 or higher installed to execute some Linux programs; for example, the GNU make.

Since NDK r7, the Windows NDK comes with a new ndk-build.cmd build script, which uses NDK's prebuilt binaries for GNU make, awk, and other tools. Therefore Cygwin is not required for building NDK programs with ndk-build.cmd. However, it is recommended that you still install Cygwin, because ndk-build.cmd is an experimental feature and Cygwin is still needed by the debugging script ndk-gdb.

Follow these steps to install Cygwin:

  1. Go to http://cygwin.com/install.html to download setup.exe for Cygwin. Double-click on it after the download is complete in order to start the installation.

  2. Click on Next, then select Install from Internet. Keep clicking on Next until you see the Available Download Sites list. Select the site that is close to your location, then click on Next:

  3. Look for GNU make under Devel, ensure it is version 3.81 or later, and gawk under Base. Alternatively, you can search for make and gawk using the Search box. Make sure both GNU make and gawk are selected to install, then click on Next. The installation can take a while to finish:

Eclipse is a powerful software Integrated Development Environment (IDE) with an extensible plugin system. It is the recommended IDE to develop Android apps. Go to http://www.eclipse.org/downloads/, and download the Eclipse Classic or Eclipse IDE for Java developers. Extract the compressed file and it will be ready for use. Note that Android development requires Eclipse 3.6.2 (Helios) or greater.

Tip

The Android developer website provides an Android Developer Tools bundle at http://developer.android.com/sdk/index.html. It includes the Eclipse IDE with the ADT plugin, and the Android SDK. We can download this bundle and skip the SDK installation described in steps 1 to 10 of the following How to do it... section.

How to do it…

The following steps show you how to set up an Android NDK development environment in Windows. We will first set up an SDK development environment. Steps 1 to 10 can be skipped if SDK is already set up.

  1. Start Eclipse. Select Help | Install New Software, and a window titled Install will pop up.

  2. Click on the Add… button at the top-right will corner, and another window titled Add Repository will pop up.

  3. In the Add Repository window, enter ADT for Name and https://dl-ssl.google.com/android/eclipse/ for Location. Then click on OK.

  4. It may take a few seconds for Eclipse to load the software items from the ADT website. After loading, select Developer Tools and NDK Plugins, then click on Next to proceed:

  5. In the next window, a list of tools to be installed will be shown. Simply click on Next. Read and accept all the license agreements, then click on Finish.

  6. After installation finishes, restart Eclipse as prompted.

  7. Download Android SDK from http://developer.android.com/sdk/index.html.

  8. Double-click on the installer to start the installation. Follow the wizard to finish the installation.

  9. In Eclipse, select Window | Preferences to open the Preferences window. Select Android from the left panel, then click on Browse to locate the Android SDK root directory. Click on Apply, and then OK.

  10. Start Android SDK Manager at the Android SDK installation root directory. Select Android SDK Tools, Android SDK Platform-tools, at least one Android platform (the latest is preferred), System Image, SDK Samples, and Android Support. Then click on Install. in the next window, read and accept all the license agreements, then click on Install:

  11. Go to http://developer.android.com/tools/sdk/ndk/index.html to download the latest version of Android NDK. Unzip the downloaded file.

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  12. Open Cygwin.bat under the cygwin root directory. It contains the following content by default:

    @echo off
    C:
    chdir C:\cygwin\bin
    bash --login -i
  13. Add the following content after @echo off before C:

    set IS_UNIX=
    set JAVA_HOME=<JDK path>
    set PATH=<SDK path>\tools;<NDK path>
    set ANDROID_NDK_ROOT=/cygdrive/<NDK path>

    As an example, the file content on my machine is as follows; note that Progra~1 is the short name for the Program Files folder:

    set IS_UNIX=set JAVA_HOME=c:/Progra~1/Java/jdk1.7.0_05
    set PATH=C:/Users/Administrator/AppData/Local/Android/android-sdk/tools;C:/Users/Administrator/Downloads/android-ndk-r8-windows/android-ndk-r8
    set ANDROID_NDK_ROOT=/cygdrive/c/Users/Administrator/Downloads/android-ndk-r8-windows/android-ndk-r8
  14. Start Cygwin by double-clicking on cygwin.bat, 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:

  15. In Eclipse, select Window | Preferences to open the Preferences window. Click on Android from the left panel, and select NDK from the drop-down list. Click on Browse to locate the Android NDK root directory. Click on OK to dismiss the pop-up window. This enables us to build and debug Android NDK applications with the Eclipse NDK plugin:

How it works…

In this recipe, we first set up an Android SDK development environment and then the NDK development environment.

Android NDK does not require installation. We downloaded NDK, and configured the path to make it more convenient to use.

Cygwin is not required for Android SDK development, but is essential for NDK development because NDK uses some Linux tools that depend on Cygwin.

NDK plugin in ADT: NDK plugin for Eclipse is available in Android Development Tools (ADT), which allows us to build and debug Android NDK applications easily.

Tip

The NDK plugin is only available for ADT 20.0.0 or later, which was released on June 2012. You may want to update your Eclipse ADT in order to use the NDK plugin.

There's more…

We installed Eclipse IDE as a part of our development environment. Eclipse is the recommended IDE for developing Android applications, and it comes with lots of useful tools and utilities to help our development. However, it is not a compulsory component of the development environment.