# Using the Android Native Development Kit (NDK)

version 1.3

## Introduction

The Android Native Development Kit enables developers to write shared libraries in C or C++ and call them from Java code. The native shared libraries can be packaged into apk files along with a normal Android application written in Java, so that the resulting Android application can be downloaded and installed on an Android phone.
The Native Development Kit consists of:

- C/C++ headers for native APIs
- C/C++ libraries for native APIs
- Documentation
- Sample Code

The Native Development Kit is designed to be used with the Android SDK:

- The NDK is used to create a shared library containing native code.
- The SDK is used to create an Android application written in Java that calls into the native code shared library.

#

## Setting up your machine

The Native Development Kit may be installed on either Linux or OS X. Developing under Windows is not yet supported.

### Linux Installation

The Android build is routinely tested on recent versions of Ubuntu (6.06 and later), but may work on other distributions as well.

#### Ubuntu Linux (i386)

To set up your Linux development environment, make sure you have the following:

- Git 1.5.4 or newer.

> $ sudo apt-get install git-core

#### Ubuntu Linux (amd64)

This has not been as well tested.
The Android build requires a 32-bit build environment:

- Get the packages as listed above in the i386 instructions:

> $ sudo apt-get install git-core

#### Other Linux

There's no reason why Android cannot be built on non-Ubuntu systems. In general you will need:

- Git 1.5.4 or newer. You can find it at http://git.or.cz/

### Mac OS Installation

- To build the Android files in a Mac OS environment, you need an Intel/x86 machine. The Android build system and tools do not support the older PowerPC architecture.
- Android must be built on a case-sensitive file system.

- We recommend that you build Android on a partition that has been formatted with the "Case-sensitive Journaled HFS+" file system:

- A case-sensitive file system is required because the sources contain files that differ only in case.
- Journaled systems are more robust. (This is optional, but recommended.)
- HFS+ is required to successfully build Mac OS applications such as the Android Emulator for OS X.

- If you want to avoid partitioning/formatting your hard drive, you can use a case-sensitive disk image instead.

- To create the image:
  - launch /Applications/Utilities/Disk Utility
  - select "New Image"
  - size: 8 GB (this will work, but you can choose more if you want to)
  - volume format: case sensitive, journaled
- This will create a .dmg file which, once mounted, acts as a drive with the required formatting for Android development. For a disk image named "android.dmg" stored in your home directory, you can add the following to your ~/.bash_profile to mount the image when you execute "mountAndroid":
  # command to mount the android file image
  function mountAndroid { hdiutil attach ~/android.dmg -mountpoint /Volumes/android; }
  Once mounted, you'll do all your work in the "android" volume. You can eject it (unmount it) just like you would with an external drive.

- Install git 1.5.4 or newer. You can find it at http://git.or.cz/

## Installing the Android SDK

The Android NDK uses the Android SDK. You can find the Android SDK at http://code.google.com/android/download.html
This version of the Android NDK requires the Cupcake version of the Android SDK.

## Installing the Prebuilt Native Toolchain

The NDK uses the prebuilt native toolchain from the Android Open Source git repository.
To download the prebuilt native toolchain to your working directory, execute the following commands:
git clone git://android.git.kernel.org/platform/prebuilt.git
cd prebuilt
git checkout -b cupcake -t origin/cupcake

## Setting Environment Variables

The NDK requires that you set two environment variables:

- PREBUILT must be set to the directory that contains the prebuilt toolchain. Include the "prebuilt" directory in the path. Example: /Volumes/android/prebuilt
- ANDROID_SDK_BASE must be set to the directory that contains the Android SDK. Example: ~/AndroidSDK

## Unpacking the NDK

Unpack the android_pndk.tar.gz into your working directory
tar -zxvf android_pndk.tar.gz
This will create a directory called pndk. It should contain a README.html file (this file) and the following directories: config, include, lib, and sample.
Look in the "samples" directory for samples showing how to use the NDK.