Integrating assembly code in JNI

Android NDK allows you to write assembly code at JNI programming. Assembly code is sometimes used to optimize the critical portion of code to achieve the best performance. This recipe does not intend to discuss how to program in assembly. It describes how to integrate assembly code in JNI programming instead.

Getting ready

Read the Passing parameters and receiving returns in primitive types recipe before you continue.

How to do it…

The following steps create a sample Android project that integrates the assembly code:

  1. Create a project named AssemblyInJNI. Set the package name as cookbook.chapter2. Create an activity named AssemblyInJNIActivity. Under the project, create a folder named jni. Refer to the Loading native libraries and registering native methods recipe of this chapter for more detailed instructions.
  2. Create a file named assemblyinjni.c under the jni folder, then implement the InlineAssemblyAddDemo method.
  3. Create a file named tmp.c under the jni folder, and implement the native method AssemblyMultiplyDemo. Compile the tmp.c code to an assembly source file named AssemblyMultiplyDemo.s, using the following command:
    $ $ANDROID_NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -S tmp.c -o AssemblyMultiplyDemo.s --sysroot=$ANDROID_NDK/platforms/android-14/arch-arm/
  4. Modify the AssemblyInJNIActivity.java file by adding code to load the native library, then declare and invoke the native methods.
  5. Modify the layout XML file, add the Android.mk build file, and build the native library. Refer to steps 8 to 10 of the Loading native libraries and registering native methods recipe of this chapter for details.
  6. At AssemblyInJNIActivity.java, enable the callInlineAssemblyAddDemo native method and disable the callAssemblyMultiplyDemo method. Run the project on an Android device or emulator. The phone display should look similar to the following screenshot:
    How to do it…
  7. At AssemblyInJNIActivity.java, enable the callAssemblyMultiplyDemo native method and disable the callInlineAssemblyAddDemo method. Run the project on an Android device or emulator. The phone display should look similar to the following screenshot:
    How to do it…

How it works…

This recipe demonstrates the usage of the assembly code to implement a native method:

  • Inline assembly at C code: We can write inline assembly code for Android NDK development. This is illustrated in native method InlineAssemblyAddDemo.
  • Generating a separate assembly code: One approach to write assembly code is to write the code in C or C++, and use a compiler to compile the code into assembly code. Then, we optimize based on the auto-generated assembly code. Since this recipe is not about writing code in an assembly language, we use the Android NDK cross compiler to generate a native method AssemblyMultiplyDemo and call it from the Java method callAssemblyMultiplyDemo.

    We first write the native method AssemblyMultiplyDemo in AssemblyMultiplyDemo.c, then cross compile the code using the compiler with Android NDK, using the following:

    $ $ANDROID_NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -S <c_file_name>.c -o <output_file_name>.s --sysroot=$ANDROID_NDK/platforms/android-<level>/arch-<arch>/

    In the preceding command, $ANDROID_NDK is an environment variable pointing to the location of Android NDK. If you have followed the recipes in Chapter 1, Hello NDK, then this should have been configured correctly. Otherwise, you can replace it with the full path to your Android NDK location (for example, in my computer, the path is /home/roman10/Desktop/android/android-ndk-r8). <level> indicates the targeted Android version. In our case, we used 14. <arch> indicates the architecture; we used arm. If we build an application for other architectures such as x86, then this should be x86. The -S option tells the cross compiler to compile the <c_file_name>.c file into an assembly code, but don't assemble or link it. The -o option tells the compiler to output the assembly code to a file <output_file_name>.s. If no such option appears, the compiler outputs to a file named <c_file_name>.s.

  • Compile the assembly code: Compiling assembly code is just like compiling C/C++ source code. As shown in the Android.mk file, we simply list the assembly file as a source file as follows:
    LOCAL_SRC_FILES := AssemblyMultiplyDemo.s assemblyinjni.c
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset