Table of Contents
- Introduction
- Preparing the Build Environment
- Build from Command Line
- Build from Eclipse* IDE
- Note on Avoiding Cleanup the Binaries under
.\libs\[targetabi]
Introduction
There are Android* devices running on processors supporting ARM* or x86 instruction set architectures (ISA). Different ISAs are not binary compatible and hence an application, if containing native code, should provide native libraries for each targeted ISA. The 'fat' Android* application packages ('fat' APK) is one of the distribution mechanisms for such applications.
This article provides the step by step instructions on building such 'fat' apk that includes the ISA-independent dex files for Dalvik* virtual machine (Dalvik, 2013) as well as the libraries for different ISA targets. It includes building the native application library for x86 using the Intel® C++ Compiler for Android*.
We will use hello-gl2 sample from the NDK r9 distribution for demonstration.
Preparing the Build Environment
The following tools are necessary and should be installed:
- Android* SDK bundle (2013) assuming installed at
[android-sdk-dir] - Android* Native Development Kit (Android NDK 2013) assuming installed at
[ndk-dir] - Java* Development Kit 6 assuming installed at
[jdk6-dir] - Apache* Ant* tool version 1.8 assuming installed at
[ant-dir] - Intel® C++ Compiler for Android* assuming installed at
[icc-dir]
This should be installed last.
Adding the necessary directories to the "PATH" environment variable:
- Directory "
[ndk-dir]" for using "ndk-build" tool - Directory "
[android-sdk-dir]\sdk\tools" for using "android" tool - Directory "
[ant-dir]\bin" for using "ant" tool - Directory "
[jdk6-dir]\jre\bin" for using "java" VM. This is required by "ant" tool
For Windows environment, make sure to use short-folder name like "PROGRA~2" for "program files (x86)" - Creating a new env-var "
JAVA_HOME=[jdk6-dir]"
Build from Command Line
This section provides the instructions on how to create the APK package to support Android devices with ARM* and x86 architectures from a command line build environment.
First open a command window or terminal window on Linux*; go to the Android NDK sample "hello-gl2" directory [ndk-dir]\samples\hello-gl2; and follow the steps below.
Note: All the tools noted in above section should be accessible from this window.
Configuring the Application
Run the following command from the "hello-gl2" directory to generate abuild.xmlfile that will be used to build the sample later.$ android update project --target android-18 --name HelloGL2 --path . --subprojects
--target android-18: corresponds to Jelly Beans MR2 Android release.Cleaning the Application Workspace
Use the following command to clean the whole workspace including libraries for all ISA architectures.
$ ant clean
$ ndk-build V=1 APP_ABI=all clean
. V=1: prints all the commands as they are being executed.
. APP_ABI=all: forces cleanup of intermediate files for all targets. If this parameter is omitted only thearmeabitarget will be cleaned.Building the Binary for ARM* Architecture
Use the command below to build the application binary for ARM architecture:
$ ndk-build APP_ABI=armeabi V=1 NDK_TOOLCHAIN=arm-linux-androideabi-4.8
. APP_ABI=armeabi: configures compilation for the ARM* target.
. NDK_TOOLCHAIN=arm-linux-androideabi-4.8: overrides the default GNU* gcc 4.6 and uses gcc 4.8.The application binary (
libgl2jni.so) is created at.\libs\armeabi\libgl2jni.soBuilding the Binary for x86 Architecture with Intel Compiler
Use the command below to build the application binary with Intel compiler for x86 architecture:
$ ndk-build APP_ABI=x86 V=1 NDK_TOOLCHAIN=x86-icc NDK_APP.local.cleaned_binaries=true
. APP_ABI=x86: configures compilation for the x86 architecture.
. NDK_TOOLCHAIN=x86-icc: overrides default compiler gcc 4.6 with Intel C/C++ compiler for Android.
. NDK_APP.local.cleaned_binaries=true: suppresses the library removal in the libs/armeabi directory. See notes at the bottom.The application binary (
libgl2jni.so) is created at.\libs\x86\libgl2jni.soPreparing Application package (APK)
After all of the binaries have been built for each Android target, check that the
libs\[targetabi]directory contains required library for each targeted architecture.To simplify the package creation and avoid package signing, use the following command to create a debug package:
$ ant debugAfter that the new
HelloGL2-debug.zippackage can be found in the.\bindirectory. It can be run on x86* or ARM* emulators supporting API level 18 or higher provided by Android SDK.
TheHelloGL2-debug.zippackage contains libraries for 2 targets: ARM EABI and x86.
Build from Eclipse* IDE
- Open Eclipse and load the sample
[ndk-dir]/samples/hello-gl2- Select menu
File > New > Project > Android Project... - Select the
Android project from existing codebutton - Select any API level above Android 1.5.
- In the
Root Directoryfield, clickBrowse...and select the[ndk-dir]/samples/hello-gl2directory. - Click
Finish.
- Select menu
- Add
Native Supportto the project:
Right click on project name and selectAndroid Tools > Add Native Support... - Create a separate build configuration for each target platform and set build command
Right click on project and selectProject properties -> C/C++ Builder -> "Manage configurations..."
ClickNewto add following new configurations based onDefaultconfiguration:"x86_icc" for x86 target using Intel Compiler icc"amr_gcc" for ARM target using GNU gcc
Configurationto "x86_icc":- Uncheck "Use default build command"
- Adding following to
Build commandfield:ndk-build APP_ABI=x86 NDK_TOOLCHAIN=x86-icc
Configurationto "arm_gcc":- Uncheck "Use default build command"
- Adding following to
Build commandfield:ndk-build APP_ABI=armeabi NDK_TOOLCHAIN=arm-linux-androideabi-4.8 - Click
OK
- Clean the whole application workspace
Right click on project and selectBuild configurations > Clean all…Note: if
Application.mkexists underjnidirectory, make sure it does not have following line:NDK_APP.local.cleaned_binaries=true - Build the app binary for ARM* target using gcc
- Create
Application.mkunderjnidirectory and add the following line:NDK_APP.local.cleaned_binaries=true - Right click on project and select
Build configurations > Set Active > arm_gcc - Right click on project and select
Build Project - Verify the output binary file at
[eclipse-workspace-dir]\GL2JNIActivity\libs\armeabi\libgl2jni.so
- Create
- Build the app binary for x86 using Intel Compiler
- Right click on project and select
Build configurations > Set Active > x86_icc - Right click on project and select
Build Project - Verify the output binary file at
[eclipse-workspace-dir]\GL2JNIActivity\libs\x86\libgl2jni.so
- Right click on project and select
- Build all the app binaries for all targets configured
- Create
Application.mkunderjnidirectory and add the following line:NDK_APP.local.cleaned_binaries=true - Right click on project and select
Build configurations > Build All... - Verify the output binary files at:
[eclipse-workspace-dir]\GL2JNIActivity\libs\armeabi\libgl2jni.so[eclipse-workspace-dir]\GL2JNIActivity\libs\x86\libgl2jni.so
- Create
- Create unsigned appliation packages
Right click on project and selectAndroid Tools > Export Unsigned Application Package...
Note on Avoiding Cleanup the Binaries under .\libs\[targetabi]
Use of NDK_APP.local.cleaned_binaries=true parameter to suppress the removal of the previously built libraries may stop working in the future NDK releases. Please consult the [ndk-dir]/build/core/setup-app.mk makefile on how to disable delete actions for the target clean-installed-binaries.