Building Native Android* Apps Using Intel(R) C++ Compiler in Android Studio*

Table of Content:

Introduction

For using Intel C++ Compiler in Android Studio 1.0.1, please refer to this other article Building Native Android* Apps Using Intel(R) C++ Compiler in Android Studio* 1.0.1.

The article applies to Android Studio* beta 0.8.6. The Intel® C++ Compiler for Android as part of Intel® Integrated Native Developer Experience (Intel® INDE) is supporting Android Studio as preview feature in Intel INDE 2015 RTM release. There is no additional change or work necessary providing that your native application can be built within Android Studio.

The Intel® C++ Compiler for Android (ICC) is configured as default compiler for x86 targets in the NDK build system during installation. Android Studio is using Gradle as build system. As time of writing Gradle invokes the NDK build system as part of the build process. Therefore ICC is also the default compiler for x86 targets in Android Studio. There is no additional configuration or setup in Android Studio required.

The steps below are describing in general how to setup a new native application in Android Studio, build and run it.

Required Software Tools

To develop native apps in Android Studio, the following tools are required:

  1. Oracle* JDK 7 or greater (native Intel-64 JDK for Windows x64 systems):
    1. set the environment variable "JAVA_HOME" to the JDK7 root folder. e.g. JAVA_HOME=C:\Program Files\Java\jdk1.7.0_67
    2. update "PATH" environment variable to JAVA_HOME/bin directory so "javah" is accessible from command window.
  2. Android Studio:
    1. Install Android Studio and latest update: to get latest update, open Android Studio, from menu [Help] select [Check for Update...] and install the updates.
    2. Android SDK
      1. Installing Android SDK from Android Studio by click on the icon below. Make sure to install the "Intel x86 Atom System Image" for the "Android x.x.x":
    3. Creating a new Android Virtual Device for Intel ATOM CPU/ABI by click on the icon below:
      1. and set the AVD properties like below:
  3. Android NDK r9d or r10b (see Release Notes for requirements): assume it's installed to [ndk-dir]
  4. Intel® INDE and make sure to install Intel® C++ Compiler for Android: select the directory [ndk-dir] for Android NDK when requested.
    1. If you have multiple Android NDKs installed, please see instructions on this article Integrating the Intel(R) C++ Compiler for Android* with multiple Android NDKs.

Using Intel(R) C++ Compiler in Android Studio*

After the Intel C++ Compiler 15.0 for Android installed, the following toolchains are installed to "[ndk-dir]\toolchains" folder:

  • x86-icc
  • x86-icc15.0.X.YYY

The default native C/C++ compiler will be the Intel C++ Compiler after the installation. There is no additional steps to use Intel C++ Compiler within Android Studio. If you would like to use GNU* gcc to build the native code, please follow Changing the default compiler back from Intel C++ Compiler to GCC for x86 targets.

Due to beta status of Android Studio you may experience some issues while importing samples from NDK directly. We will create a new Android project with a native function call to demonstrate the use of Intel C++ Compiler.

  1. Create a new Android project with a native interface:

    1. Open Android Studio, Create a new Android project "nativeDemo" with default setting. e.g.
    2. Open "app\src\main\java\MainActivity.java" and add a native function as below at the end of the class "MainActivity":
      1. public native String getStringFromNative();
    3. Building "nativeDemo" project: select menu "Build > Make Project" to get ready for using "javah".
    4. Open the terminal window from "View > Tools Windows > Terminal" and run "javah" to create the jni header:
      1. In the terminal window, go to "src\main" subfolder:
        1. cd src\main
      2. run the following "javah" cmd: it creates the "com_example_nativedemo_app_MainActivit.h" under src\main\jni folder.
        1. javah -d .\jni -classpath C:\Android\android-studio\sdk\platforms\android-20\android.jar;..\..\build\intermediates\classes\debug com.example.mydemo.nativedemo.MainActivity
      3. e.g.
        1. javah cmd
    5. Right click on "src" folder, select "Sycnhronize 'src'": now you can see the header file "com_example_nativedemo_app_MainActivit.h" under the "src\main\jni" folder.
  2. Add native source code: main.c, dummy.c

    1. Create "main.c": select file "com_example_nativedemo_app_MainActivity.h", use copy/paste to create a new file "main.c" with following code:
      1. #include "com_example_nativedemo_app_MainActivity.h"
        
        JNIEXPORT jstring JNICALL Java_com_example_nativedemo_app_MainActivity_getStringFromNative
          (JNIEnv * env, jobject obj)
        {
          #ifdef __INTEL_COMPILER_UPDATE
            return (*env)->NewStringUTF(env, "Hello from Intel C++ !");
          #else
            return (*env)->NewStringUTF(env, "Hello from default C++ !");
          #endif
        }
    2. Create "dummy.c": create another empty file "dummy.c" -- this file is required only on Windows and will probably not be needed in the final release of Android Studio
    3. Now you have 3 files under "jni" folder: com_example_nativedemo_app_MainActivity.h, main.c, dummy.c
  3. Set NDK directory in the project:

    1. Open project file "local.properties" and add "ndk.dir": i.e.
      1. sdk.dir=C\:/Android/android-studio/sdk
        ndk.dir=C\:/android-ndk-r9d
  4. Change the output library: change default "libapp.so" to "myNativeTest.lib"

    1. Open "build.gradle": add following "ndk" section in "defaultConfig" like below.
          defaultConfig {
              minSdkVersion 18
              targetSdkVersion 19
              versionCode 1
              versionName "1.0"
              ndk {
                  moduleName "myNativeTest"
                  abiFilter "x86"
              }
          }
      			
    2. Now build the project: select [Build > Make Project].You will see all the output folders and files "libmyNativeTest.so" under "app\build\ndk\debug\lib" and "app\build\ndk\debug\obj\local" folder.
  5. Add an ID "hello_testview" to the textview widget
    1. Open res\layout\activity_main.xml and add:
    2.     <TextView
              android:text="@string/hello_world"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/hello_textview" />
  6. ​Update "MainActivity.java" to hook up the native library call to the UI textview widge:

    1. public class MainActivity extends Activity {
          static { // load the native library "myNativeTest"
          System.loadLibrary("myNativeTest");
          }
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
                  // get the text string from native API, and display on the UI
              TextView tv = (TextView)findViewById(R.id.hello_textview);
              tv.setText(this.getStringFromNative());
          }
  7. Start the Android Virtual Machine "Intel-Nexus4"

  8. Run the app by click the "Run" button.

    1. This means the compiler used is the Intel C++ Compiler for Android*.

Summary

Now you know how to build a simple native Android app with Intel C++ Compiler in Android Studio. Try it with your real native application and let us know how it goes.

This article applies to:
    Products: Intel® INDE 2015 RTM
    Host OS: Windows (IA-32, Intel® 64)
    Target OS: Android*

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