Table of Contents
- Introduction
- Preparing the Build Environment
- Build from Command Line
- Build from Eclipse* IDE
- Note on Avoiding Cleanup of Binaries under
.\libs\[targetabi]
- Other Related Articles and Resources
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 Intel® Integrated Native Developer Experience (INDE).
We will use hello-jni sample from the NDK r10 distribution for demonstration (which comes with Intel® INDE install).
Preparing the Build Environment
The following tools are necessary and these instructions should be follow in order:
- Install:
- Microsoft* .NET Framework
- Java* Development Kit 7u67 assuming installed at
[jdk7-dir]
- Add this directory to the "PATH" environment variable:
- Directory "
[jdk7-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)
"
- Directory "
- Create a new env-var:
- "
JAVA_HOME=[jdk7-dir]
"
- "
- Install:
- Intel® Integrated Native Developer Experience assuming installed at
[inde-dir]
- Intel® Integrated Native Developer Experience assuming installed at
- Add this directory to the "PATH" environment variable:
- Directory "
[ant-dir]\bin
" for using "ant
" tool (Default location of[ant-dir]
is:[inde-dir]\IDEintegration
)
- Directory "
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-jni
" directory [ndk-dir]\samples\hello-jni
; 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-jni
" directory to generate abuild.xml
file that will be used to build the sample later.$ android update project --target android-19 --name HelloJni --path . --subprojects
Note:
--target android-19
: corresponds to to Android 4.4 (KITKAT) 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
Note:
. 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 thearmeabi
target 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-v7a V=1 NDK_TOOLCHAIN=arm-linux-androideabi-4.8
Note:
. APP_ABI=armeabi-v7a
: 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 (
libhello-jni.so
) is created at.\libs\armeabi-v7a\libhello-jni.so
Building 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
Note:
. 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 (
libhello-jni.so
) is created at.\libs\x86\libhello-jni.so
Preparing 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 debug
After that the new
HelloJni-debug.apk
package can be found in the.\bin
directory. It can be run on x86* or ARM* emulators supporting API level 19 or higher provided by Android SDK.
TheHelloJni-debug.apk
package contains libraries for 2 targets: ARM EABI v7a and x86.
Build from Eclipse* IDE
- Open Eclipse and load the sample
[inde-dir]/IDEintegration/NDK/samples/hello-jni
- Select menu
File > New > Project > Android
- Select the
Android Project from Existing Code
button - In the
Root Directory
field, clickBrowse...
and select the[ndk-dir]/samples
directory. - Then click
Deselect All
and select only the project:hello-jni
- Check the box the says
Copy projects into workspace
- Click
Finish
.
- Select menu
- Add
Native Support
to the project:- Right click on project name and select
Android Tools > Add Native Support...
- Click
Finish
.
- Right click on project name and select
- Create a separate build configuration for each target platform and set build command
Right click on project and selectProperties -> C/C++ Build -> "Manage configurations..."
ClickNew...
to add the following new configurations based onDefault
configuration:- Name:
x86_icc
Description:For x86 target using Intel Compiler icc
- Name:
arm_gcc
Description:For ARM target using GNU gcc
Configuration
to "x86_icc":- Uncheck "Use default build command"
- Adding following to
Build command
field:ndk-build APP_ABI=x86 NDK_TOOLCHAIN=x86-icc
- Click
Apply
Configuration
to "arm_gcc":- Uncheck "Use default build command"
- Adding following to
Build command
field:ndk-build APP_ABI=armeabi-v7a NDK_TOOLCHAIN=arm-linux-androideabi-4.8
- Click
Apply
and thenOK
- Name:
- Clean the whole application workspace
- If
Application.mk
exists under[eclipse-workspace-dir]\HelloJni\jni
directory, make sure it does not have the following line:NDK_APP.local.cleaned_binaries=true
- Right click on project and select
Build configurations > Clean all…
- If
- Create
Application.mk
under[eclipse-workspace-dir]\HelloJni\jni
directory is it does not exist, and make sure it has the following line:NDK_APP.local.cleaned_binaries=true
- Build the app binary for ARM* target using gcc and x86 target using Intel Compiler.
- Right click on project and select
Build Configurations > Build Selected...
- Select both “arm_gcc” and “x86_icc”
- Click
Ok
. - Verify the output binary files at:
-
[eclipse-workspace-dir]\HelloJni\libs\armeabi-v7a\libhello-jni.so
[eclipse-workspace-dir]\HelloJni\libs\x86\libhello-jni.so
-
- Right click on project and select
- Create unsigned application packages
Right click on project and selectAndroid Tools > Export Unsigned Application Package...
Note on Avoiding Cleanup of 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.
Other Related Articles and Resources
- NDK Android* Application Porting Methodologies
- Changing the default compiler back from Intel C++ Compiler to GCC for x86 targets
- Intel® C++ Compiler for Mac OS* - Compatibility with the GNU* gcc and g++ compilers
- Android* NDK for Intel® Architecture
- Speeding Up the Android* Emulator on Intel® Architecture
To learn more about Intel tools for the Android development, visit Intel® Developer Zone for Android.