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.

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply