Creating an x86 and ARM APK using the Intel Compiler and GNU gcc

Published Date
26 - May - 2015
| Last Updated
27 - May - 2015
 
Creating an x86 and ARM APK using the Intel Compiler and GNU gcc

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:
1. Install:

  • Microsoft* .NET Framework
  • Java* Development Kit 7u67 assuming installed at [jdk7-dir]

2. 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)"
3. Create a new env-var:

  • "JAVA_HOME=[jdk7-dir]"

4. Install:

  • Intel® Integrated Native Developer Experience assuming installed at [inde-dir]

5. 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)

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.

1.Configuring the Application

Run the following command from the "hello-jni" directory to generate a build.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.
2. 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 the armeabi target will be cleaned.

3. 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

4. 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

5. 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.

The HelloJni-debug.apk package contains libraries for 2 targets: ARM EABI v7a and x86.

Build from Eclipse* IDE

1. 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, click Browse... and select the [ndk-dir]/samplesdirectory.
  • Then click Deselect All and select only the project: hello-jni
  • Check the box the says Copy projects into workspace
  • Click Finish.

2. Add Native Support to the project:

  • Right click on project name and select Android Tools > Add Native Support...
  • Click Finish.

Note: If you see the console error "Unable to launch cygpath" after this step it can be ignored and will not affect your project.
3. Create a separate build configuration for each target platform and set build command
Right click on project and select Properties -> C/C++ Build -> "Manage configurations..."
Click New... to add the following new configurations based on Default configuration:

  • Name: x86_icc Description: For x86 target using Intel Compiler icc
  • Name: arm_gcc Description: For ARM target using GNU gcc

Still on the same Project Properties window, set 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

Again on the same Project Properties window, set 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 then OK

4. Clean the whole application workspace

  • If Application.mk exists under [eclipse-workspace-dir]\HelloJni\jnidirectory, 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…

5. 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
6. 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

7. Create unsigned application packages

Right click on project and select Android 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.

For more such intel resources and tools from Intel, please visit the Intel® Developer Zone

Source: https://software.intel.com/en-us/android/articles/creating-an-x86-and-arm-apk-using-the-intelr-compiler-and-gnu-gcc