How to compile native C library and daemon using Android.mk ?

To compile any c library inside android, we need to write an Android.mk makefile, for reference please check below,

$ mkdir external/mysource
$ cd external/mysource

In below makefile Android.mk we assume, you have a C library code in library_source.c file and application using this c library in main.c

$ vim Android.mk

To compile as Shared Library,

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := library_source.c

LOCAL_C_INCLUDES :=$(LOCAL_PATH)
LOCAL_CFLAGS := -O2 -g -W -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H
LOCAL_MODULE := libmylib
LOCAL_MODULE_TAGS := debug
LOCAL_SYSTEM_SHARED_LIBRARIES := libc libcutils libm
include $(BUILD_SHARED_LIBRARY)

To compile as executable, using above shared library ( append to above Android.mk )

include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CFLAGS := -O2 -g -W -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DHAVE_CONFIG_H
LOCAL_MODULE := main-exececutable
LOCAL_MODULE_TAGS := debug
LOCAL_SYSTEM_SHARED_LIBRARIES := libc libcutils libm
LOCAL_SHARED_LIBRARIES := libmylib
include $(BUILD_EXECUTABLE)

Change red color as per your library, source,

To compile, above source, type from top android source ” make libmylib” i.e. whatever name you have given in LOCAL_MODULE.

Leave a Comment