How to compile native C library and native C program as part of Android AOSP’s external directory using Android.mk makefile

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

1. mkdir external/mysource

2. cd external/mysource

3. 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 Reply