Thursday, December 15, 2016

Build and run native C/C++ Android applications with NDK standalone toolchains

  • Install Android NDK.

Instructions are here.

Android NDK by default will be installed in the following directory:

/home/<username>/.local/share/umake/android/android-ndk/
  • Select your Android API level and processor architecture.

Let's select API level 21 (Android 5.0 "Lollipop") and ARM architecture.

In /home/<username>/.local/share/umake/android/android-ndk/platforms/ directory select android-21 subdirectory and there arch-arm subdirectory.

So, the sysroot directory is

/home/<username>/.local/share/umake/android/android-ndk/platforms/android-21/arch-arm
  • Create hello.cpp file.

We'll take one created in the previous blog post:

#include <stdio.h>

int main(int argc, char *argv[])
{
printf("Hello, World!\n");

return 0;
}
  • Build Android native application.
/home/<username>/.local/share/umake/android/android-ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc-4.9 -static -march=armv7-a --sysroot=/home/<username>/.local/share/umake/android/android-ndk/platforms/android-21/arch-arm  hello.cpp -o hello

Output is hello binary file.

  • Upload this file to Android device
adb push hello /data/local/tmp
  • Change file permissions
adb shell chmod 777 /data/local/tmp/hello

Don't forget to do it after each file push.

  • Run application
adb shell /data/local/tmp/hello

Output is:

vurdalakov@ubuntu:/tmp$ adb shell /data/local/tmp/hello
Hello, World!

No comments:

Post a Comment