Create x86 APKs and ARM APKs with Intel® Compiler and GNU gcc

Original author: Jennifer J.
  • Transfer
There are Android devices on processors with ARM or x86 instruction set architectures (ISAs). The various instruction set architectures are not binary compatible, so an application containing native code must contain native libraries for each architecture. One of the distribution mechanisms for such applications is the so-called “thick” Android application packages (“thick” APKs).

This article provides step-by-step instructions for creating such a “thick” APK package, including architecture-independent files for the Dalvik virtual machine (Dalvik, 2013), as well as libraries for different architectures. This article describes how to build a native x86 application library using the Intel Integrated Native Developer Experience (INDE).

For demonstration, we use the hello-jni example from the NDK r10 distribution kit (included with the Intel INDE installation).

Preparing the build environment


The following software is required. Install them following the instructions given.
1. Install:
Microsoft .NET Framework ,
Java Development Kit 7u67 must be installed in [jdk7-dir].
2. Add the folder to the environment variable the PATH :
directory [jdk7-dir] \ jre \ bin to use the java virtual machine. This is required for ant.
For Windows, do not forget to specify the short name of the folder (of the form PROGRA ~ 2 ) for program files (x86) .
3. Create a new environment variable:
JAVA_HOME = [jdk7-dir] .
4. Install:
Intel Integrated Native Developer Experience Packagemust be set to [inde-dir].
5. Add the folder to the environment variable the PATH :
directory [ant-dir] \ bin to use ant means (location [ant-dir] default: [inde-the dir] \ IDEintegration )

Command line build


This section contains instructions for creating APK packages to support Android devices with ARM and x86 architecture in a command line build environment.
Open a command prompt or terminal window on Linux; go to the Android NDK sample folder "hello-jni" [ndk-dir] \ samples \ hello-jni ; follow these steps.
Note. All tools listed in the section above should be available from this window.

1. Setting up the application
Run the following command in the hello-jni folder to create the build.xml file, which will later be used to build the example.
$ android update project --target android-19 --name HelloJni --path. --subprojects


Note. --target android-19 is compatible with Android 4.4 (KITKAT).

2. Cleaning the application workspace
Use the following command to clear the entire workspace, including libraries, for all ISA architectures.
    $ ant clean
    $ ndk-build V=1 APP_ABI=all clean


Note : V = 1 : print all commands as they are executed. APP_ABI = all : Force cleanup of all intermediate files for all target platforms. If you do not specify this parameter, only the file for the target armeabi platform will be cleared.

3. Build binaries for the ARM architecture
Use the command below to build a binary file for the ARM architecture:
    $ ndk-build APP_ABI=armeabi-v7a V=1 NDK_TOOLCHAIN=arm-linux-androideabi-4.8


Note : APP_ABI = armeabi-v7a : compilation setting for the target ARM platform. NDK_TOOLCHAIN ​​= arm-linux-androideabi-4.8 : instead of the default GNU gcc 4.6, gcc 4.8 is used.
The application binary file (libhello-jni.so) is created at . \ Libs \ armeabi-v7a \ libhello-jni.so .

4. Build binaries for the x86 architecture using the Intel compiler
Use the command below to build the binary file for the x86 architecture using the Intel compiler.
  $ ndk-build APP_ABI=x86 V=1 NDK_TOOLCHAIN=x86-icc NDK_APP.local.cleaned_binaries=true


Note : APP_ABI = x86 : compilation setting for x86 architecture. NDK_TOOLCHAIN ​​= x86-icc : replacing the default gcc 4.6 compiler with the Intel C / C ++ compiler for Android. NDK_APP.local.cleaned_binaries = true : cancel the removal of the library in the libs / armeabi directory. See notes below.
The application binary file (libhello-jni.so) is created at . \ Libs \ x86 \ libhello-jni.so .

5. Preparing the application package (APK)
After assembling all the binaries for each target Android platform, make sure that the libs \ [targetabi] folder contains the required libraries for each target architecture.
To simplify package creation and avoid signing it, use the following command to create a debug package.
    $ ant debug


After that, the new HelloJni-debug.zip package will appear in the . \ Bin folder . It can be run on x86 or ARM simulators that support the API level 19 or higher contained in the Android SDK.
The HelloJni-debug.zip package contains libraries for two target platforms: ARM EABI v7a and x86.

Build in the Eclipse IDE


1. Open Eclipse and download the example [inde-dir] / IDEintegration / NDK / samples / hello-jni .
From the menu, select File> New> Project> Android .
Click the Android Project from Existing Code button .
In the Root Directory field, click the Browse ... button and select the [ndk-dir] / samples folder .
Then click Deselect All and select only the project you want : hello-jni
Uncheck Copy projects into workspace .
Click the Finish button .

2. Add native code support to the project.
Right-click the project name and select Android Tools> Add Native Support ...
Click Finish .
Note. If after this step the console displays the Unable to launch cygpath error message , you can skip this step, which will not affect the project.

3. Create a separate build configuration for each target platform, specify the build command.
Right-click the project and select Project properties -> C / C ++ Build -> Manage configurations ...
Click New ... to add new configurations based on the default configuration.
Name: x86_icc Description: for the x86 target platform using the Intel icc compiler
Name: arm_gcc Description: for the target ARM platform using the GNU gcc compiler

In the same project properties window, set the Configuration parameter to x86_icc.
Uncheck Use default build command .
Add the following text to the Build command field : ndk-build APP_ABI = x86 NDK_TOOLCHAIN ​​= x86-icc .
Click Apply .
In the same project properties window, set the Configuration parameter to arm_gcc.
Uncheck Use default build command .
Add the following text to the Build command field : ndk-build APP_ABI = armeabi-v7a NDK_TOOLCHAIN ​​= arm-linux-androideabi-4.8 .
Click Apply , then click OK .

4. Cleaning the entire application workspace
If the Application.mk file exists in the [eclipse-workspace-dir] \ HelloJni \ jni folder , make sure that it does not contain the following line.
NDK_APP.local.cleaned_binaries = true
Click the project, right-click and select the Build configurations> Clean all ... .

5. Create the Application.mk file in the [eclipse-workspace-dir] \ HelloJni \ jni folder if it does not exist and make sure that it contains the following line.
NDK_APP.local.cleaned_binaries = true

6. Build the binary for the target ARM * architecture using gcc and for the target x86 architecture using the Intel compiler.
Right-click the project and select Build Configurations> Build Selected ...
Select arm_gcc and x86_icc.
Click OK.
Check the output binaries at the following addresses.
[eclipse-workspace-dir] \ HelloJni \ libs \ armeabi-v7a \ libhello-jni.so
[eclipse-workspace-dir] \ HelloJni \ libs \ x86 \ libhello-jni.so


7. Creating unsigned application packages
Right-click the project mice and select Android Tools> Export Unsigned Application Package ...

Note about not clearing binary files in. \ Libs \ [targetabi]


The NDK_APP.local.cleaned_binaries = true parameter to disable deletion of previously compiled libraries may not work in future versions of NDK. See [ndk-dir] /build/core/setup-app.mk for information on how to disable uninstall actions for installed binaries.

Other Related Articles and Resources




For more information about Intel’s tools for Android developers, see the Intel Developer Zone for Android .

Also popular now: