Building Android* Applications from Command Line

This topic applies to C/C++ on Linux* and Windows* host systems.

You can build applications for Intel architecture-based Android* devices in two ways:

  • Using the Intel® C++ Compiler toolchain as a standalone compiler

  • Enabling the Intel toolchain within the Android NDK build system


For Android* version 5.0 and later, you must use the -fpie and -pie options to generate position-independent code while building dynamically linked executables with the Intel® C++ Compiler for Intel® System Studio.

Install the 64-bit Android* NDK from or the Android* Open Source Project (AOSP) from Refer to the Release Notes for supported NDK versions.

When you install the Intel® C++ Compiler, the installation program integrates the compiler into the Android NDK build system automatically.

Working with a Clang-only Environment

This information applies when:

  • using the Intel® C++ Compiler toolchain as a standalone compiler

  • enabling the Intel Toolchain within the Android NDK build system

By default, the Intel compiler uses an environment based on GCC. You can also use the Intel compiler for Android when working with a Clang environment, such as when working with a version of the NDK that uses Clang as its system compiler.

To do so, do one of the following:

  • Set the compiler option -use-clang-env.

  • Set the environment variable INTEL_USE_ENVIRONMENT=CLANG.

Setting INTEL_USE_ENVIRONMENT to CLANG is equivalent to setting the option use-clang-env. Possible values for this variable are: GCC, CLANG. The option -use-clang-env takes precedence over INTEL_USE_ENVIRONMENT.

If you have set the compiler to use the Clang environment, you can set it to use the GCC environment by doing one of the following:

  • Set the compiler option -no-use-clang-env.

  • Set the environment variable INTEL_USE_ENVIRONMENT=GCC. Alternatively, you can unset INTEL_USE_ENVIRONMENT.

Using the Intel® C++ Compiler Toolchain as a Standalone Compiler

The Intel C++ Compiler requires a GNU or Clang compiler toolchain for the Android* OS to work.

On Linux* host systems, set up the environment for the Intel compiler using the compilervars file. See Using compilervars File for details.

On a Windows* host system, perform the following steps :

  1. Open the setup environment window, from Start Menu > Intel System Studio 2018 for Linux and Android targets > Compiler and Performance Libraries > Command Prompt with Intel Compiler v18.0 > IA-32 Android [or Intel 64 Android].

    A command prompt window opens up with the prompt at C:\Program Files (x86)\IntelSWTools\system_studio_2018.

  2. Change directory to \compilers_and_libraries_2018.x.yyy\windows\bin where the compilervars.bat is located.

  3. Set up the environment for the Intel Compiler using the compilervars.bat file. See Using compilervars File for details.

If you see errors requesting to set up ANDROID_SYSROOT and ANDROID_GNU_X86_TOOLCHAIN environment variables, the possible reason is that you used an unsupported NDK version during installation. In such a case, you can set up the --sysroot and -gnu-prefix compiler options manually.

Using -gnu-prefix and --sysroot Compiler Options for Cross-Compiling

This method uses the --sysroot and -gnu-prefix compiler options instead of setting environment variables and using the -platform compiler option. You can use this method for all versions of Android*.

The --sysroot option specifies the target root directory of headers and libraries. For example, if the headers and libraries are normally located in /usr/include and /usr/lib respectively, --sysroot=/mydir causes the compiler to search in /mydir/usr/include and /mydir/usr/lib for the headers and libraries.

The -gnu-prefix option specifies a string that prepends the name of GNU tools called from the Intel® C++ Compiler. The value depends on the GNU toolchain used for a particular operating system. For example, for the Android platform, the prefix value, for a 64-bit target, is x86_64-linux-android-. Append a hyphen to the prefix only if the toolchain prefix ends with a hyphen.

When working with a Clang-only environment, you need to add the Clang binaries to PATH.

For example, to set the environment variables for 64-bit Android* targets, execute the following commands:

//On Linux* host

export PATH=/<android_ndk_dir>/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin:$PATH
// When working with a Clang environment, add Clang binaries to PATH:
export PATH=/<android_ndk_dir>/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
export SYSROOT=<android_ndk_dir>/platforms/android-<api_level>/arch-x86_64
export GNU_PREFIX=x86_64-linux-android-

//On Windows* host

set PATH=C:\<android_ndk_dir>\toolchains\x86_64-4.9\prebuilt\windows-x86_64\bin;%PATH%
// When working with a Clang environment, add Clang binaries to PATH:
set PATH=C:\<android_ndk_dir>\toolchains\llvm\prebuilt\windows-x86_64\bin;%PATH%
set SYSROOT=C:\<android_ndk_dir>\platforms\android-21\arch-x86_64
set GNU_PREFIX=x86_64-linux-android-

Use icc/icl from the command line with the --sysroot and -gnu-prefix compiler options, as follows:

icc my_source_file.c -gnu-prefix=$GNU_PREFIX --sysroot=$SYSROOT      //Linux* host
icl my_source_file.c -gnu-prefix=%GNU_PREFIX% --sysroot=%SYSROOT%    //Windows* host

If you installed the 32-bit version of the Android NDK, set the environment variables as follows:

//On Linux* host

export PATH=/<android_ndk_dir>/toolchains/x86-4.9/prebuilt/linux-x86_64/bin:$PATH
// When working with a Clang environment, add Clang binaries to PATH:
export PATH=/<android_ndk_dir>/toolchains/llvm/prebuilt/linux-x86/bin:$PATH
export SYSROOT=<android_ndk_dir>/platforms/android-21/arch-x86
export GNU_PREFIX=i686-linux-android-

//On Windows* host

set PATH=C:\<android_ndk_dir>\toolchains\x86-4.9\prebuilt\windows-x86\bin;%PATH%
// When working with a Clang environment, add Clang binaries to PATH:
set PATH=C:\<android_ndk_dir>\toolchains\llvm\prebuilt\windows-x86\bin;%PATH%
set SYSROOT=C:\<android_ndk_dir>\platforms\android-21\arch-x86
set GNU_PREFIX=i686-linux-android-

Using the AOSP environment:

If you are using the AOSP environment on a Linux* host, set the variables as follows:

export TOPDIR= #set to the AOSP root directory (example: /export/JB_WW36) 
export ANDROID_SYSROOT=$TOPDIR/prebuilts/ndk/8/platforms/android-9/arch-x86
export ANDROID_GNU_X86_TOOLCHAIN=$TOPDIR/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.8

Compiling Source Code

You are now ready to use icc/icpc from a command line in the build environment to compile C/C++ source code.

Here is an example on a Linux* host:

$ cd $NDK_DIR/samples/hello-jni/jni 
$ icc -platform=android -c hello-jni.c 
$ icc -platform=android –shared -o hello-jni.o

Enabling the Intel Toolchain within the Android NDK Build System

To understand how to enable the Intel toolchain within the Android* NDK build system, follow the steps to build the hello-jni sample application that ships with the Android NDK. You can locate the sample in $NDK_DIR/samples/hello-jni/jni/.

Build the sample application with the Intel® C++ Compiler either from the command line or using the *.mk file. In either approach, you need to configure APP_ABI if you want to use ICC. If multiple versions of the Intel compiler are installed, a full compiler package version can be specified.

To build an application with the Intel® C++ Compiler 18.0 toolchain using the Android NDK, do one of the following:

  • Explicitly pass to ndk-build: NDK_TOOLCHAIN=x86-iccXX.x.y.nnn (for 32-bit targets) or NDK_TOOLCHAIN=x86_64-iccXX.x.y.nnn (for 64-bit targets), where XX is the major version, x is the minor version, y is the update number, and nnn is the package build number.

  • In the application configuration file ( set NDK_TOOLCHAIN=x86-icc (for 32-bit targets) or NDK_TOOLCHAIN=x86_64-icc (for 64-bit targets)

From the command line

  1. Open the terminal window and navigate to the jni folder of the sample:

    cd $NDK_DIR/samples/hello-jni

  2. Enter the following command:

    $ $NDK_DIR/ndk-build V=1 -B APP_ABI=x86_64 NDK_TOOLCHAIN:=x86_64-icc  // 64-bit Linux host
    >>%NDK_DIR%\ndk-build V=1 -B APP_ABI=x86_64 NDK_TOOLCHAIN:=x86_64-icc // 64-bit Windows host


    V=1 enables verbose output (optional)

    -B means rebuild all (optional)

    APP_ABI indicates target architecture.

Using the file

  1. Open or create the file in your application’s jni folder at $NDK_DIR/samples/hello-jni/jni/.

  2. Add the following lines in

    • For 32-bit targets:

    • For 64-bit targets:

  3. Run the command to build the application:

    $ $NDK_DIR/ndk-build V=1 –B    // on Linux* host
    >>%NDK_DIR%\ndk-build V=1 –B   // on Windows* host

For more complete information about compiler optimizations, see our Optimization Notice.