Monday, January 28, 2019

Pure C Program ( Native Command Line ) on Android , Pure C helloworld Binary on Android.

1. Download Android Studio

2. install NDK  By default android studio only comes with SDK ( for making APP in java)
for Making pure C/C++ program or JNI interface for Java in C/C++ , you need NDK.


3. Set NDK path in env vairbale of windows or Linux

4.Create a project with the following directory hierarchy:

project/
    jni/
        Android.mk
        Application.mk
        *.c, *.cpp, *.h, etc.

5. helloworld.c

#include
#include

int main(int argc, char **argv)
{
printf("Hello, world !");
exit(0);
}//


5. Fill in Android.mk with the following content. The most important thing is the last line. Check the NDK doc for the meaning of the other variables.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := helloworld
LOCAL_SRC_FILES := helloworld.c
LOCAL_CPPFLAGS := -std=gnu++0x -Wall -fPIE # whatever g++ flags you like
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -fPIE -pie # whatever ld flags you like
LIB := $(ANDROID_NDK_ROOT)/platforms/android-$(NDK_PLATFORM_VER)/arch-arm/usr/lib
LIBCRT := $(LIB)/crtbegin_dynamic.o
include $(BUILD_EXECUTABLE) # <-- an="" blockquote="" build="" executable.="" this="" to="" use="">


6. Go to the project/ directory, and simply type
ndk-build

7. Build generates binary for all the supported platform

C:\Users\aprasad4\Downloads\helloworldC>ndk-build
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version andro
id-16.
[arm64-v8a] Compile        : helloworld <= helloworld.c
[arm64-v8a] Executable     : helloworld
[arm64-v8a] Install        : helloworld => libs/arm64-v8a/helloworld
[armeabi-v7a] Compile thumb  : helloworld <= helloworld.c
[armeabi-v7a] Executable     : helloworld
[armeabi-v7a] Install        : helloworld => libs/armeabi-v7a/helloworld
[x86] Compile        : helloworld <= helloworld.c
[x86] Executable     : helloworld
[x86] Install        : helloworld => libs/x86/helloworld
[x86_64] Compile        : helloworld <= helloworld.c
[x86_64] Executable     : helloworld
[x86_64] Install        : helloworld => libs/x86_64/helloworld

8 . If libs folder is created with binary for all the platform  , opening with android shows following dir structure.

project/libs//name-of-your-executable




9 .Copy this hello world to android emulator or phone using at /data/local for it to run
adb push helloworld /data/local

10.run the c app using ./data/local/helloworld  - will print hello world.



No comments:

Post a Comment

Featured Post

XDP - Getting Started with XDP (Linux)