How to Run C/C++ code in Android.

Hi,

I am out of station these days so thought of writing my first blog.

As most of you are aware that Android has Linux kernel,so what was always there in my mind is that there must be some way to run C code in Android, although its most of the application is written in Java.

I was not able to find a perfect method on internet so found a method myself and thought of sharing it.

This thing is possible with the help of Android-NDK.

You can download it here: http://developer.android.com/sdk/ndk/index.html

After downloading NDK you’ll find a very nice tool ndk-build, its nothing but a shell script.

This tool allow us to compile our C/C++ code into binary which can later be pushed into android and executed using SDK tool adb.

Android SDK can be downloaded from: http://developer.android.com/sdk/index.html

I assume while writing this post that you have basic knowledge of Android Development ,C/C++ ,eclipse and how to setup Android SDK .

Method is as follows:

  • Create a new Android Project in Eclipse.
  • Create a folder name “jni” the root directory of the project.
  • In the folder jni ,you need to make Android.mk (Android make file) as:
    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE    := hello-world
    LOCAL_SRC_FILES := hello-world.c
    
    include $(BUILD_EXECUTABLE)

    Its not hard to understand every line of this make file:

    LOCAL_PATH := $(call my-dir)

    Android.mk file must start with the definition of LOCAL_PATH variable (yes, this is a variable and anything like this you see in Android.mk file are variables, you can define your own variable but more on that later.)
    This line uses build-in function my-dir that returns the value of path of current directory ie. the directory containing Android.mk.

    include $(CLEAR_VARS)

    This line clears all the LOCAL_XXX variable except LOCAL_PATH.

    LOCAL_MODULE    := hello-world
    LOCAL_SRC_FILES := hello-world.c

    These are most important lines.

    LOCAL_MODULE , defines the name of the compiled code (ie. executable) that you wish.It should not contain any spaces and must be unique.

    LOCAL_SRC_FILES, it tells the name of C/C++ files that you wish to compile for android.

    include $(BUILD_EXECUTABLE)

    This is in-build function to make executable for android.

  • Now in the jni folder make file hello-world.c (in my case) as:
    #include<stdio.h>
    void main()
    {
     printf("Hello to Android World\n");
     return;
    }

    I assume you understand above code.

  • Now come to root Directory of the folder in terminal (Linux) or in Command Prompt(Windows).
    Windows people have to do little more effort, they have to download GNUMakefile from: http://www.gnu.org/software/make/
    Now, set the GNUMAKE environment variable by running following command:
  • export GNUMAKE=path/to/your/GNUMAKE

    Prefer not having spaces in the path for simplicity.

  • Now comes the final step of compiling it with ndk-build.Just go to the root of the project and run the command ndk-build as:
    /path/to/you/NDK-Directory/ndk-build
  • That you have binary, Finally what remains is to push the file into Android, using adb.
    You can find this tool inside Android-SDK/platform-tools.
    Start your Android Emulator or connect you Android Phone.
    Run following commands :

    sudo ./adb kill-server

    Above command kills any server if its running.

    sudo ./adb push ./hello-world /data/local

    This command pushes binary hello-world into Android directory /data/local.

    sudo ./adb shell

    Wait for 2 sec till this “#” or “$” sign appears.
    Now, this is Android shell prompt.

    cd data/local/
    ./hello-world

    We enter into the directory containing our binary and execute it.

    This should display the output, which in our case is:

    Hello to Android world

Hope you have enjoyed this post, feel free to ask doubts or point error(Grammatical or Technical).

Advertisements