Android NDK create executable

It is possible to create executable using android ndk. You need adb to push the executable to the device. Modify/create the Android.mk file in jni folder so that ndk-build create executable rather then shared library.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello_world
LOCAL_SRC_FILES := test.cpp

include $(BUILD_EXECUTABLE)

My cpp file contains following code

#include <stdio.h>
int main()
{
	printf("hello world\n");
}

Run ndk-build in jni folder. Then a executable will create in libs/armeabi folder in hello_world name. Attach your device to the pc. Open your console and go the libs/armeabi folder. Run adb push hello_world /data/local/tmp. Now run adb shell. This will open the shell of your device. cd to the /data/local/tmp. Run ./hello_world. The console will show the output. /data is the only executable section in android.

Advertisements
Android NDK create executable

3 thoughts on “Android NDK create executable

  1. Antonio says:

    Hello Mr. Sabbir. great post.
    So, I would like call this executable from android.
    How to make that?

    My java code to call the lib:
    // NDK STUFF
    static {
    System.loadLibrary(“hello_world”);
    }

    In this case, I need only switch of hello_world to /data/local/tmp/hello_world, i.e. the complete path?

    thanks.

    1. The tutorial said about executing a program from terminal/command prompt/shell, not using java. This is not a jni tutorial. This is a way to execute a program from adb shell or android os’s own terminal. The executable is a program, not a library. So you cant load it from java. By the way, you can load any dynamic/shared library in jni using full path ( in java of windows/mac/linux, did not test it in android) or library name if library is in your projects lib folder or in /system/lib folder of android OS.

      Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s