Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

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

5.00/5 (3 votes)
7 Apr 2015CPOL5 min read 16.2K  
The steps below provide a general description of how to setup, build, and run a new native application in Android Studio 1.0.1.

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Intel® Developer Zone offers tools and how-to information for cross-platform app development, platform and technology information, code samples, and peer expertise to help developers innovate and succeed. Join our communities for Android, Internet of Things, Intel® RealSense™ Technology, and Windows to download tools, access dev kits, share ideas with like-minded developers, and participate in hackathon’s, contests, roadshows, and local events.

Table of Contents:

Introduction

The current Android Studio* release is 1.0.1 at the time of this writing. The Intel® C++ Compiler for Android as part of Intel® Integrated Native Developer Experience (Intel® INDE) is supporting Android Studio 1.0.1 in Intel INDE 2015 Update 1. Since Android Studio 1.0.1 does not support Android NDK, instructions have been provided in this article with steps on how to build a native Android* app using Android NDK r10d and Intel C++ Compiler for Android.

Android Studio uses Gradle for its build system. At the time of this writing Gradle invokes the NDK build system as part of the build process. With Android NDK r10 and later, the Intel® C++ Compiler for Android (ICC) is no longer the default compiler in the NDK build system after Intel INDE is installed.

The steps below provide a general description of how to setup, build, and run a new native application in Android Studio 1.0.1.

If you have Android Studio* 0.8.6, please refer to this article Building Native Android* Apps Using Intel(R) C++ Compiler in Android Studio* for how to use Intel C++ Compiler.

Required Software Tools

Successful installation of INDE 2015 Update 1 with Android Studio Integration will ensure you have the all required software installed.

Please refer to the latest Intel(R) C++ Compiler Release Notes for Intel(R) Integrated Native Developer Experience 2015 for detailed software and system requirements.

The instructions provided in this article uses the following software for targeting both IA-32 and Intel®-64 architecture:

  • Oracle* JDK 7 (native Intel-64 JDK for Windows x64 systems)
  • Android SDK 20 or greater
  • NDK r10d (assumed to be installed at: [ndk-dir] )
  • Android Studio 1.0.1

Please make sure the following Android NDK directories were added to the environment variable PATH (add them if missing):

  • C:\Intel\INDE\IDEintegration\NDK\build\tools
  • C:\Intel\INDE\IDEintegration\NDK

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

After the Intel C++ Compiler 15.0 for Android is installed, the following toolchains are installed to "[ndk-dir]\toolchains" folder (the default directory is "C:\Intel\INDE\IDEintegration\android-ndk-r10d\toolchains"):

  • x86-icc
  • x86-icc15.0.X.YYY
  • x86_64-icc (if NDK supports 64-bit target)
  • x86_64-icc15.0.X.YYY (if NDK supports 64-bit target)

For NDK up to r9d: 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.

For NDK r10 to r10d: The Intel C++ compiler is not default after the installation. Follow steps 3, 4, 5 below to use Intel C++ Compiler from Android Studio.

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.

We will now create a new Android project for Intel-64 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 settings. e.g.

      Image 1

    2. Open "app\src\main\java\MainActivity.java" and add a native function as below at the end of the class "MainActivity":
      C++
      public native String getStringFromNative();

      You file should now look like this:

      Image 2

    3. To build your "nativeDemo" project select: "Build > Make Project" to get ready for using "javah".
    4. Open the terminal window from "View > Tools Windows > Terminal" and follow the steps below to run "javah" and create the jni header:
      1. In the terminal window, navigate to the "src\main" subfolder:
        cd src\main
      2. Then, to create the "com_example_nativedemo_app_MainActivit.h" under the src\main\jni folder, run the following "javah" cmd:
        javah -d .\jni -classpath C:\Intel\INDE\IDEintegration\android-sdk-windows\platforms\android-21\android.jar;..\..\build\intermediates\classes\debug com.example.mydemo.nativedemo.MainActivity

      e.g.Image 3

    5. In the Project window change the view to Project and right click on the "src" folder and select "Synchronize 'src'". Now you can see the header file "com_example_mydemo_nativedemo_MainActivity.h" under the "src\main\jni" folder.
  2. Add native source code: main.c

    1. Create "main.c": select file " com_example_mydemo_nativedemo_MainActivity.h ", use copy/paste to create a new file "main.c" with following code:
      C++
      #include "com_example_mydemo_nativedemo_MainActivity.h"
      
      JNIEXPORT jstring JNICALL
      Java_com_example_mydemo_nativedemo_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. Save Changes
    3. Now you have 2 files under "jni" folder: com_example_mydemo_nativedemo_MainActivity.h and main.c

      Image 4

  3. Add make file: Android.mk

    1. Right click on "jni" folder, select "New > File"
    2. Type "Android.mk" and click "OK".
    3. Add the following lines to this file (note that LOCAL_SRC_FILES should contain source files located in "jni" folder:
      C++
      LOCAL_PATH := $(call my-dir)
      include $(CLEAR_VARS)
      
      LOCAL_MODULE    := nativeDemo 
      LOCAL_SRC_FILES := main.c 
      
      include $(BUILD_SHARED_LIBRARY)
  4. Add make file: Application.mk

    1. Right click on "jni" folder, select "New > File"
    2. Type "Application.mk" and click "OK".
    3. Add the following lines to this file:
      C++
      # For IA-32
      #APP_ABI:=x86
      #NDK_TOOLCHAIN:=x86-icc
      #include $(BUILD_SHARED_LIBRARY)
      
      # For Intel-64
      APP_ABI:=x86_64
      NDK_TOOLCHAIN:=x86_64-icc
      include $(BUILD_SHARED_LIBRARY)
    4. [Optional] to change compiler options, use the following:
      C++
      APP_CFLAGS := -O3
  5. Configure your application to run ndk-build with make files

    1. Open "app\build.gradle" file
    2. Add the following imports at the top of the file:
      C++
      import com.android.build.gradle.tasks.NdkCompile
      import org.apache.tools.ant.taskdefs.condition.Os
    3. Add the following lines after "defaultConfig" section:
      C++
      sourceSets.main {
          jniLibs.srcDir 'src/main/libs' //set .so files location to libs
      } 
      
      tasks.withType(NdkCompile) { // disable automatic ndk-build call
          compileTask -> compileTask.enabled = false
      }
      
      task ndkBuild(type: Exec) { // call ndk-build(.cmd) script 
          if (Os.isFamily(Os.FAMILY_WINDOWS)) {
              commandLine 'cmd', '/c', 'ndk-build.cmd', '-C', file('src/main').absolutePath            
          } else {
              commandLine 'ndk-build', '-C', file('src/main').absolutePath
          }
      }
      
      tasks.withType(JavaCompile) {
          compileTask -> compileTask.dependsOn ndkBuild
      }
    4. Add following line at end of the file:
      C++
      dependencies {
          compile fileTree(dir: 'libs', include: ['*.jar'])
      }
    5. Save Changes
    6. Now build the project: select [Build > Make Project].You will see all the output folders and files "libmain.so" under "main\libs" and "main\obj\local" folders.
       
  6. Add an ID "hello_textview" to the textview widget

    1. Open res\layout\activity_main.xml and modify the textview widget as shown below:
      XML
      <TextView
          android:text="@string/hello_world"
      	android:layout_width="wrap_content"
      	android:layout_height="wrap_content"
      	android:id="@+id/hello_textview" />
  7. ​Update "MainActivity.java" to hook up the native library call to the UI textview widget:

    1. C++
      public class MainActivity extends Activity {
          static { // load the native library "nativeDemo"
          System.loadLibrary("nativeDemo");
          }
          @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());
          }
    2. Press ALT + ENTER to import the TextView widget and save your changes.
       
  8. Start the Android Virtual Machine "Intel-Nexus 7 x64" and run the app by clicking the "Run" button.

    Image 5

    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 Update 1 or later
    Host OS: Windows (IA-32, Intel® 64)
    Target OS: Android* (IA-32, Intel® 64)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)