Creating Apps for Android NDK based on Intel Architecture

Most recent:
  • Vampire: The Masquerade – Coteries of New York city arrives on December 4 to PC
  • Overwatch 2 for the PC, PS4, Xbox One, PS5 and Scarlett may be the surprise of Blizzcon
  • How to make the divorce process easier?
    ‘Wounds’: the sinister descent into the darkness of Armie Hammer on Netflix leaves you with honey on the lips

    This article provides an introduction to creating native Android * applications (developed by NDK, Native Development Kit) for Intel ® (AI) architecture devices. Also discuss the export of Android NDK applications that have been created for devices other AI architectures based devices. Will visit two scenarios, one to show the process of creating a basic application using Android NDK * from beginning to end and the other to display a simple process of exporting an existing Android NDK based application to be used in AI-based devices.


    The Android applications can incorporate native code set by Native Development Kit (NDK) tools. Thus, developers can reuse legacy code, do low-level programming to the hardware, or differentiate their applications leveraging features that is not possible or practical access to other mechanisms.

    This article is a basic introduction on how to create based on NDK for Intel architecture (IA) application from start to finish. Will step through a simple scenario to demonstrate the development process.

    We assume that you already have installed the Android development environment, including the Android SDK, Android NDK and x86 emulator for testing applications. You can find information resources for doing this at

    Step by step description of a simple application

    Suppose from an Android application that we want to know low-level details about the processor of the device in that our app works. Suppose also that we have a program that can determine this, but the problem is it is in C and includes calls to assembly language. Listing 1 shows the contents of the program you want to reuse cpuid.c.

     intel -01

    Listing 1. Program cpuid.c legacy. Available in

    Then we show how from an Android application, call the function cpuid_parse () and receive its result.

    1. Creating an Android project default

    To begin, create a folder inside the ndk to create a new Android project with the default structure. This is accomplished by using the command “android” which run as follows:

    dir=”ltr”> ~ $ mkdir ndk

    ~ $ cd ndk

    ~ / ndk $ android create project-android-16-target activity com.example.cpuid CPUIdApp-package-path.

    When you do this, the tool should generate a project with the “CPUIdApp” com.example.cpuid activity in the package and using as a reference level 16 API Android (Jelly Bean 4.1). If we explore our portfolio, we can see that they have created new files and directories, including a src folder structure com / example / cpuid within which is the file is a file type “Hello World” generated automatically .

    Once the project is created, we will compile and install it on a device or emulator (if you do not have an activated emulator, see how to configure one with hardware acceleration on To compile and build the application, we use the “ant” tool to the task “debug”.

    dir=”ltr”> ~ / ndk $ ant debug

    If the job runs successfully, you will see that in the bin directory we have several files, including CPUIdApp-debug.apk is our Android application.

    Note: When creating this article, the latest version of the Android SDK has a bug that causes it to generate a default project and run the ant to build a java exception is thrown. nio.BufferOverflowException. If you face this problem, you using the SDK Manager version of the low-tools to Build 18.1.1 (the bug is in version 19 of the Build-tools).

    The next step is to install the application to a device or emulator. We use the adb command, which is part of the Android SDK.

    dir=”ltr”> ~ / ndk $ adb install-r bin / CPUIdApp-debug.apk

    Checking on our device or emulator, we can see that the app was installed and can run.


    dir=”ltr” Figure 1. Emulator showing our app.

    Clicking on the application, you can see the default “Hello World” application.


    dir=”ltr” Figure 2. Execution default App

    then modify the application to use native code.

    2. Calling native code from Java code

    Listing 2 shows the source code of our file / src / com / example / cpuid / as already discussed, was generated automatically when you create the project.


    Java Code Listing 2 default ( )..

    To use C / C + + code from our Java program, we need to make the call via JNI. TextView will deploy a text that we return the call to native code by JNI.

    Listing 3 contains the adjusted version of to do this.


    dir=”ltr” Listing 3. JNI call and text display. ( )

    Below use the “javah” tool to generate stubs for JNI header.

    3. Utilization “javah” to generate stub headers for JNI native code

    then we will modify our native to comply with the specification calls JNI code.’s “javah” tool can analyze one or more Java class files and generate the corresponding header for C.

    First we need to compile the updated version of our project so we run the ant.

    dir=”ltr”> ~ / ndk $ ant debug

    Once done, use the javah tool to generate the header file for C.

    dir=”ltr”> ~ / ndk jni $ javah-d -classpath bin / classes com.example.cpuid.CPUIdApp

    Note: If you run javah get a similar “class file for not found Error “it is because you require to indicate the jar of Android you’re using can solve this by indicating this as an additional invoke javah classpath If you’re using Windows, separate classpath with ‘;’ and if you use Linux do it with…. ‘:’ Example ~ / ndk $ javah-jni-classpath d ~ / android-sdk/platforms/android-16/android.jar: bin / classes com.example.cpuid.CPUIdApp

    After executing this, we note that there is a jni folder in our project, which contains the file com_example_cpuid_CPUIdApp.h. This is our header file.

    Now we will create the file C code . corresponding (“com_example_cpuid_CPUIdApp.c”) to the header Listing 4 shows the contents:


    dir=”ltr” Listing 4. C code in JNI call to specify. ( )

    What we are doing with this code is to draw cpuid_parse function of our native C program as a parameter giving a character buffer in which we want to write the answer. said buffer is returned as a string of JNI.

    4. Generating Native code with NDK for x86

    are now ready to compile the native code using the NDK x86 assembly tools. NDK If you do not know or have not installed, see the guide at To compile our native code, we will put our cpuid.c program (shown in Listing 1) in the “jni” folder of your project.

    In addition, we need to create a file” “which also goes in the” jni “which specify the tasks which are required of the NDK, eg what are the files to be compiled, the header and compilation type (eg SHARED_LIBRARY).

    Listing 5 shows the contents of our project Remember that this file must be in the “jni” folder. our project.


    Listing 5 Makefile for NDK ( / pedrogk/7427531 )

    As you can see, we are simply stating what the source files, and you want it to be compiled as a shared library.

    Once you create the and taking all the necessary files in our jni folder (, com_example_cpuid_CPUIdApp.c, com_example_cpuid_CPUIdApp.h, cpuid.c) can return to the root directory of your project and issue the ndk-build command (if NDK folder is not in your PATH, you must specify the full path to where you have it installed).

    dir=”ltr”> ~ / ndk $ ndk APP-build-ABI = x86

    Using the “APP_ABI = x86” are informing NDK option we want to generate binaries for x86 architecture. If the command is executed successfully , we can see that the / ndk/libs/x86/

    are now ready to rebuild our Android application and install or run it on an x86 emulator or device was created end.

    5. recompile, install and run Android NDK application for AI

    Let’s make the following script to first clean up old files, then recompile, and then install on our device or emulator.

    dir=”ltr”> ~ / ndk $ ant clean debug

    ~ / ndk $ ant debug

    ~ / ndk $ adb install-r bin / CPUIdApp-debug.apk

    Figure 3 shows the execution of the application showing the results yielded by our native code.


    have successfully compiled based Android NDK application.

    Exporting NDK existing applications to devices with Intel architecture

    If you have existing applications that use NDK, it is easy to modify to support other architectures, such as Intel. Simply you put your native code under “jni” folder and as such made this year, and when running ndk-build using the “APP_ABI: = all” option. NDK to generate native shared libraries for all available platforms Android build system automatically packaged all necessary native libraries in APK and installation time the Android package manager will install only the appropriate native library based architecture finally used.

    To export an existing Android application with native code, usually not meant to x86 The process of amending the application to make it compatible with AI is straightforward in most cases (as discussed above), unless the application uses Assembler languages ​​or algorithms specific architecture. might also have other problems such as alignment memory or uses the platform specific instructions. Consultation


    This article showed how to create and export Android NDK based applications for Intel architecture. Rode step by step Creating a process based NDK application to be used in AI, from start to finish. also discuss the simple process made possible by the NDK tools we export existing Android NDK based applications to AI.

    Article was published in The Free Android (The Android Blog reference. Apps, News Free Games and Android smartphones)

    The Free Android

    Creating Apps for Android NDK based on Intel Architecture
    November 23, 2013

    Next Random post