First Step of Android NDK

To run C/C++ Native code in Android, we need to use Android NDK. Android NDK is different library from Android SDK.

There are several steps to use Android NDK

Steps

  1. Download NDK from Android NDK web site
  2. Compress under anywhere
  3. Add path of the NDK directory ex c:\android-ndk-r8e
  4. ADT Install NDK Plugin http://dl.google.com/eclipse/plugin/4.2
  5. Set up Eclipse Preferences -> NDK -> NDK Location (Add NDK path)

CPU

We consider CPU to compile native codes. Android has 3 CPU architectures

  • ARM
  • x86
  • MIPS

ARM is most popular architecture of Android phone, or tablets.
x86 is PC architecture, sometimes PC like tablets use that CPU.
We should change compile settings of each architectures.

First NDK Project

Create general Android Project same as Android application.
Next, you add native support(Project -> Right click -> Android Tools -> Native Support)

nativesupport

Select native support. After it, library name dialog show up.

libraryname

We don’t care about library name. We finally make share library (.so) in Android NDK. This is name.
But, we can change
This is default name, so eclipse will make Android.mk(build script) to make this name library.
Actually, automatic script is useless. We need change drastically.

project

Click OK. We can see jni directory under our project. And there are Android.mk and Projectname.cpp file.

Prepare was done.

Android.mk

This is build script to build shared libary.
See default script

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := AndroidCrypto3
LOCAL_SRC_FILES := AndroidCrypto3.cpp

include $(BUILD_SHARED_LIBRARY)

A bit different from Linux Make File.

Build

To build native code from

Eclipse -> Project -> Build Project
The result is shown in Eclipse console. You can see it!

NDK uses ndk-build command in Android NDK tools.

Result

The shared library is located under libs/armeabi (ARM base) directory