Book Image

Android NDK Beginner's Guide

By : Sylvain Ratabouil
Book Image

Android NDK Beginner's Guide

By: Sylvain Ratabouil

Overview of this book

<p>Android NDK is all about injecting high performance into your apps. Exploit the maximum power of these mobile devices using high-performance and portable code.</p> <p>This book will show you how to create C/C++ enabled applications and integrate them with Java. You will learn how to access native API and port libraries used in some of the most successful Android applications.</p> <p>Using this practical step-by-step tutorial, highlighted with comments and tricks, discover how to run C/C++ code embedded in a Java application or in a standalone application. You will create a real native application starting from project creation through to full implementation of native API and the porting of existing third-party libraries. You will discover OpenGL ES and OpenSL ES, which are becoming the new standard in mobility. You will also understand how to access keyboard and input peripherals and how to read accelerometer or orientation sensors.</p> <p>Finally, you will dive into more advanced topics such as debugging and troubleshooting applications. By the end of the book, you should know the key elements to enable you to start exploiting the power and portability of native code.</p>
Table of Contents (19 chapters)
Android NDK Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – preparing Ubuntu Linux for Android development


To work with Android NDK, we need to check and install some system packages and utilities:

  1. First, Glibc (the GNU C standard library, in version 2.7 or later) must be installed. It is usually shipped with Linux systems by default. Check its version using the following command:

    $ ldd -–version
    
  2. We also need the Make build tool for native code. Installation can be performed using the following command:

    $ sudo apt-get install build-essential
    

    Alternatively, Make can be installed through Ubuntu Software Center. Look for build-essential in the dedicated search box and install the packages found:

    Package build-essential contains a minimal set of tools for compilation and packaging on Linux Systems. It also includes GCC (the GNU C Compiler), which is not required for standard Android development as Android NDK already packages its own version.

  3. To ensure that Make is correctly installed, type the following command. If correctly installed, the version will be displayed:

    $ make --version
    

Tip

Special note for 64-bit Linux owner

We also need 32-bit libraries installed to avoid compatibility problems. This can be done using the following command (to execute in a command-line prompt) or again the Ubuntu Software Center:

sudo apt-get install ia32-libs

To run Eclipse and allow compilation of Android Java code to bytecode, Java Development Kit is required. We need to download and install Oracle Sun Java Development Kit. On Ubuntu, this can be performed from the Synaptic Package Manager:

  1. Open Ubuntu System/Administration menu and select Synaptic Package Manager (or open your Linux package manager if you use another Linux distros).

  2. Go to the Edit | Software Sources menu.

  3. In the Software Sources dialog, open the Other Software tab.

  4. Check the Canonical Partners line and close the dialog:

  5. Package cache synchronizes automatically with the Internet, and after a few seconds or minutes some new software is made available in the Canonical Partners section.

  6. Find Sun Java™ Development Kit (JDK) 6 (or later) and click on Install. You are also advised to install Lucida TrueType fonts (from the Sun JRE), the Java(TM) Plug-in packages.

  7. Accept the license (after reading it carefully of course!). Be careful as it may open in the background.

  8. When installation is finished, close Ubuntu Software Center.

  9. Although Sun JDK is now installed, it is not yet available. Open JDK is still used by default. Let’s activate Sun JRE through the command line. First, check available JDK:

    $ update-java-alternatives –l
    
  10. Then, activate the Sun JRE using the identifier returned previously:

    $ sudo update-java-alternatives –s java-6-sun
    
  11. Open a terminal and check that installation is OK by typing:

    $ java –version
    

The Android SDK supports Ant, a Java-based build automation utility, to compile projects from the command line. Let’s install it.

  1. Install Ant with the following command or with the Ubuntu Software Center:

    $ sudo apt-get install ant
    
  2. Check whether Ant is properly working:

    $ ant --version
    

What just happened?

We have prepared our Linux operating system with the necessary utilities to host Android development tools.

We have installed a Java Development Kit in version 1.6 and checked if it is properly working from the command line. Because Android SDK uses generics, the JDK in version 1.5 is the least required for Android development.

You may wonder why we bothered with the installation of Sun JDK while Open JDK is already ready to use. The reason is simply that Open JDK is not officially supported by Android SDK. If you want to avoid any possible interaction with Open JDK, think about removing it entirely from your system. Go to the Provided by Ubuntu section in the Ubuntu Software Center and click on Remove for each OpenJDK line. For more information, look for the official Ubuntu documentation: http://help.ubuntu.com/community/Java.

Finally, we have installed Ant utility that we are going to use in the next chapter to build projects manually. Ant is not required for Android development but is a very good solution to set up a continuous integration chain.

Note

There is no more Sun JDK on Linux repositories since Java 7. The Open JDK becomes the official Java implementation.