An Android Cross-Compiling Environment on Windows/UNIX-like systems using NDK

android-ndkThe Android NDK is a toolset that allows us to implement parts of our app using native-code languages such as C and C++. There are many articles about integrating Android SDK/NDK and writing some “C/C++JNI compatible code. JNI is the Java Native Interface. It defines a way for managed code (written in the Java programming language) to interact with native code (written in C/C++).

But today we will talk about:

  • the native cross-compiling on the Windows/UNIX-like systems
  • uploading of the compiled binary file to the Android device
  • running the compiled binary file via Android CLI shell.

And what is interesting, we will not use the Android SDK (except adb utility) or any others Android sources 🙂

Downloading the tools

Download the latest Android NDK for our operating system from Android Developers site.

Windows Users. Download the Cygwin (this is a collection of tools which provide a Linux look and feel environment for Windows) from Cygwin Installation page.

Installing

The NDK installation process is very simple. The only step that we should do is to unzip the NDK archive. We can unzip it to any place because we can delete this folder later. Please remember this path because it will be needed to us to replace the <NDK> text in the commands below.

Windows Users. Please run the downloaded Cygwin Setup program and leave all options “by default” pressing “Next” on the each step. Then we should see the “Cygwin Terminal” link on our Desktop if the installation process successfully completed.

Setup Environment

  1. Open the “Terminal” program based on our operation system (for Windows, open “Cygwin Terminal” that was installed in the previous step). All commands below should be executed to this “Terminal” application.
  2. Then go to “NDK Build Tools” folder:

    Example for Windows & Cygwin (in case we copied the extracted NDK content to the “C:/android-ndk” folder):
  3. Now we must setup standalone Toolchain:

    • <PLATFORM> the full list of Android platforms are available in “<NDK>/platforms”
    • <TOOLCHAIN> please specify the directory where will be installed standalone toolchain.

    Example for Windows & Cygwin below:

  4. Then we need to update $PATH environment variable to include the “<TOOLCHAIN>/bin” directory, so we can execute compiler from any location:

    Warning!!!  When we will restart the “Terminal” program the $PATH variable will be restored to default value (without <TOOLCHAIN>). In this case we should write “export line that wrote above”  to our home ~./bash_profile file with this command:

    Example for Windows & Cygwin below:

  5. Finish. Check ARM compiler version:

    And we should receive the result like this:

Compiling the “Hello World” Program

For testing we will use the simple “Hello World” program:

Please copy the code that is mentioned above into “hello.c” file. The Cross-Compile command will look like:

After successful compilation we should see “hello.bin” file. Please read below how to run this file on our Android device.

Running the “Hello World” Program

For copying and running our “hello.bin” program we will need the “adb” tool. What is it? Android Debug Bridge (adb) is a versatile command line tool that lets us communicate with an emulator instance or connected Android-powered device.

Install the “adb” tool

Please  go to the “Android SDK” page  and click on the “DOWNLOAD FOR OTHER PLATFORMS” link (located at the bottom of the page). From the second table named as “SDK Tools Only” please download the SDK for our operation system. Then unpack it (or install it if we are using Windows & SDK Installer version) to any place (later we will use this path to SDK folder as <SDK>).

Then we need to install the “Android SDL Platform-tools”. We can do it with the “Android SDK Manager”. Run it:

In the launched “Android SDK Manager” please do these steps:

  1. Click on the “Deselect All” link
  2. Check the “Android SDK Platform-tools”
  3. Click on the “Install 1 package…” button
  4. Accept the “Android SDK License”
  5. Press the “Install” button

android-crosscompiler-sdk-manager

If all steps completed successfully we should see the “Android SDK Platform-tools” status as “Installed”. Now please connect our Android-based device to PC and check it with “adb” tool:

Install the “hello.bin” program

The easiest way to install binary program is to copy it to any folder on “Android device” where we can setup the “Execute Permission” for the “hello.bin” file. In my case, I have “rooted device” and I can copy binary file to any place. The “copy” command will look like:

 Run the “hello.bin” program

Firstly, we should grant “hello.bin” file with “Execute Permission”. So, now open the command line shell on our “Android device” and execute these commands in order:

And now run the program:

The end! 🙂 If this article helped you please share it with yours friends. Thanks!