adding shared library in `Android.mk`

Linking a shared library in Android.mk

This document is based on my experience of modifying su binary.

Working with Android.mk for C source files

Sharing my experiences of trying to build su binary which a small modification of my own: linking with libcap.so shared library. Assumes that I already have libcap.so built.

At first it was confusing because I didn’t know which options to modify in my Android.mk file.

Initially I thought that I had to refer to libcap through the LOCAL_SHARED_LIBRARIES option. However, this was not the case. The additions that I added were:

LOCAL_CFLAGS := -fPIE
LOCAL_LDFLAGS:= -lcap -fPIE -pie
LOCAL_C_INCLUDES += external/libcap-for-Android-master/libcap/include

I am actually referring to the libcap.so through the -lcap option in LOCAL_LDFLAGS. the pie, PIE is added in order to make the output as a position independent executable. I may have overwritten things about pie here. Perhaps it would have been enough to specify pie either in LOCAL_CFLAGS or LOCAL_LDFLAGS. But I haven’t tested that part. But hey, specifying it in both options seems like a safe bet.

If I don’t specify the PIE related flags, then the built executable will cause the following error when executed.

error: only position independent executables (PIE) are supported.

Note that I have also added the path to the header files of libcap in LOCAL_C_INCLUDES. This is necessary for the su.c code to refer to the libcap header files.

Working with Android.mk for C++ source files

I also had to link the libcap.so with Zygote source code. Linking the libcap.so even for cpp source code’s Android.mk was not so different.

LOCAL_LDFLAGS +=  -lcap -fPIE -pie
LOCAL_C_INCLUDES += external/libcap-for-Android-master/libcap/include
Advertisements

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