The Android* NDK (Native Development Kit) is a set of tools that allows you to use native languages like C or C++ to implement parts of your application. Game applications often use Android NDK to implement performance-sensitive parts or to reuse existing code and libraries. Android NDK supports the x86 platform.
You can download the Android NDK from: https://developer.android.com/tools/sdk/ndk/index.html
In this article I'll review how to compile a sample application using the standard Android NDK. After that, I'll show how to create optimized libraries for x86 using Intel® C++ Compiler for Android.
Building the sample application using the Android NDK
The sample application I’ll use is called san-angeles and is located in the Android NDK samples directory:
$ANDROID_NDK/samples/san-angeles
Native code is located in the jni/
directory:
$ANDROID_NDK/samples/san-angeles/jni
Native code is compiled for specified CPU architecture(s). Android applications may contain libraries for several architectures in one apk file.
To set target architectures you need to create the Application.mk
file inside the jni/
directory. The following line will compile the native libraries for all supported architectures:
APP_ABI := all
Sometimes, it’s better to specify a list of target architectures. This line compiles the libraries for x86 and ARM architectures:
APP_ABI := x86 armeabi armeabi-v7a
Run the following command inside the sample directory to build libraries:
cd $ANDROID_NDK/samples/san-angeles $ANDROID_NDK/ndk-build
After the successful build, open the sample in Eclipse* as an Android application and click “Run”. Select the emulator or connected Android device where you want to run the application.
To support all available devices you need to compile the application for all architectures. If the apk file size with libraries for all architectures is too big, consider using Google Play Multiple APK Support feature to provide a separate apk file for each platform.
Checking supported architectures
To check that architectures are included in apk file use the command:
aapt dump badging file.apk
The following line lists all architectures:
native-code: 'armeabi', 'armeabi-v7a', 'x86'
Another method is to open the apk file as a zip file and view subdirectories in lib/
directory.
If the application is installed on the device, you could use Apk Info application to check the supported architectures. Please note that if you used the Google Play Multiple APK Support feature and have already published your application, you may not see all supported architectures.
Intel® Integrated Native Development Experience (Intel® INDE)
Intel® Integrated Native Development Experience (Intel® INDE) is a suite of tools and libraries for native application development. Intel INDE integrates into popular IDEs and provides a complete and consistent set of C++ and Java* tools, libraries, and samples for environment setup, code creation, compilation, debugging, and analysis for applications on Intel® Architecture-based devices, and select capabilities on ARM*-based Android devices. The main advantage to using Intel INDE is for its ability to download and provide tool updates when they become available.
Intel C++ Compiler for Android
Intel C++ Compiler for Android is included in Intel INDE suite. Intel C++ Compiler for Android integrates in Android NDK and provides an optimized alternative to compile x86 libraries.
Download and install Intel C++ Compiler for Android. Provide a path to NDK directory during the installation to integrate Intel C++ Compiler for Android into Android NDK.
After the successful installation, the Intel C++ Compiler for Android will be automatically integrated into the Android NDK toolchain and will compile optimized libraries for x86 architecture.
To make sure that Intel C++ Compiler for Android is used, check the output using this command and specify the V=1 option to display verbose output:
cd $ANDROID_NDK/samples/san-angeles $ANDROID_NDK/ndk-build --always-make V=1
Android NDK toolchain should use icc
or icpc
binaries for building x86 libraries.
By default, the NDK is configured to build an application for debugging with all optimizations turned off. When the code is ready for deployment, you need to compile it for performance. To compile the x86 libraries with optimization add these lines to the Application.mk
file:
APP_ABI := x86 APP_OPTIM := release APP_CFLAGS := -xATOM_SSSE3 -O3 –ipo
Please note that these options are only valid when compiling for x86 ABI.
Getting Started documentation provides detailed instructions for how to use Intel C++ Compiler for Android.
Intel C++ Compiler for Android creates optimized binaries for x86 architecture. To measure the performance of game applications you could use Intel® Graphics Performance Analyzers (Intel® GPA) System Analyzer. Intel GPA System Analyzer is useful to quickly collect a lot of metrics such as FPS, CPU load, etc.
Additional information
The NDK Android* Application Porting Methodologies article provides more details about porting applications to Intel Architecture.
To create a high-quality application it’s very important to regularly test the application on real devices with all supported architectures. The Automated Android* Application Testing article explains different approaches for how to create automated testing systems for Android applications. There are many services that will test your application on hundreds of real physical devices. If you want to automatically collect performance metrics from the game application, consider using Intel GPA Console Client. Instructions for using it are here: Using Intel® Graphics Performance Analyzers Console Client for Android* Application Performance Analysis