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.
Read the Passing parameters and receiving returns in primitive types recipe before you continue.
The following steps create a sample Android project that integrates the assembly code:
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.assemblyinjni.c
under the jni
folder, then implement the InlineAssemblyAddDemo
method.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/
AssemblyInJNIActivity.java
file by adding code to load the native library, then declare and invoke the native methods.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.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: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:This recipe demonstrates the usage of the assembly code to implement a native method:
InlineAssemblyAddDemo
.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
.
Android.mk
file, we simply list the assembly file as a source file as follows:LOCAL_SRC_FILES := AssemblyMultiplyDemo.s assemblyinjni.c