Understanding Android Makefile (Android.mk)
Firstly, let me say that this document is an abstract information which is available in pdk documents. Secondly, you may want to look at the post that tells how to build Android source code. And now, we can start, at a first glance to Android source codes, we see that applications, libraries, components in framework have Android.mk files in their specific directories. These Android.mk files defines how to build that source code. There are well defined specific rules for Android.mk files. Let me summarize them.
- Name: We need to define a name for our build (LOCAL_MODULE := )
- Local Variables: All builds may have some local variables so to start a new build it is good to clear all local variables (include $(CLEAR_VARS))
- Files: We need to write all files we want it to be build (LOCAL_SRC_FILES := main.c)
- Tags: Define tags for build. (LOCAL_MODULE_TAGS := eng development)
- Libraries: If build needs to be linked to other libraries, we need to define them (LOCAL_SHARED_LIBRARIES := cutils)
- Template file: We can define whether our build is executable,library or something else by including template file (include $(BUILD_EXECUTABLE))
BUILD_EXECUTABLE, CLEAR_VARS, etc. variables which are the absolute address of the template files are defined in build/core/config.mk.
Here is a simple Android.mk file that builds an apk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Build all java files in the java subdirectory
LOCAL_SRC_FILES := $(call all-subdir-java-files)
# Name of the APK to build
LOCAL_PACKAGE_NAME := LocalPackage
# Tell it to build an APK
include $(BUILD_PACKAGE)
Last line in Android.mk file builds the APK file. Different source code types must be built differently so we can also use $(BUILD_EXECUTABLE), $(HOST_JAVA_LIBRARY), $(HOST_PREBUILT) etc. variables according to our source code. (Definitions like my-dir, all-subdir-java-files are in build/core/definitions.mk)
we can add LOCAL_MODULE_TAGS variable to Android.mk file to determine that module to be installed in that source code built. Here are the some defined tags and their meanings
- eng
- Default variant
- Installs modules tagged with: eng, debug, user, and/or development
- Installs non-apk modules that have no tags specified.
- Installs APKs according to the product definition files.
- adb is enabled by default.
- user
- Final release
- Installs modules tagged with user
- Installs non-apk modules that have no tags specified
- Install APKs according to the product definition files
- adb is disabled by default.
- userdebug, Same as user except:
- Also installs modules tagged with debug
- adb is enabled by default
These tags is same as build variants that can be specified in “choosecombo” function. “choosecombo” function is available after “source build/envsetup.sh” command. We can specify build variant in building source code like below
make -j4 PRODUCT-generic-eng
“eng” is the build variant “generic” is the product. The command above will build and install modules according to attributes of “eng” tag mentioned above.
Ahmet Oğuz Mermerkaya
Related posts:


