Android compilation system – pre-installed app -> Android.mk

Android-makefile

1. Overview of Android.mk

The Android.mk file is used to describe your source code to the compilation system. Specifically: – This file is a small part of the GNU Makefile that will be parsed one or more times by the build system. The syntax of this file allows organizing your source code into modules, such as: static libraries, shared libraries (which will be installed/copied to your application package); we can define one or more in each Android.mk file Modules, you can also use the same source code file in several modules.

2. Common components of Android.mk

  • LOCAL_PATH := $(call my-dir) #Specify the current directory

  • include $(CLEAR_VARS) #Introduce compilation variables

  • LOCAL_MODULE := hello #Compiled module name

  • LOCAL_MODULE_TAGS := optional #The compilation option is the situation under which it is compiled

  • LOCAL_SRC_FILES := hello.c #Source file (can specify multiple)

  • LOCAL_MODULE_CLASS := EXECUTABLES #Specify the location after compilation

  • include $(BUILD_EXECUTABLE) #Introduce rules for compiling into executable files

  • LOCAL_PACKAGE_NAME #Specify the APP application name

  • LOCAL_PRIVATE_PLATFORM_APIS := true #After setting, the hide api of the sdk will be used to compile

  • LOCAL_USE_AAPT2 := true #aapt is a tool for compiling and packaging resources. And aapt2 is optimized on aapt

  • LOCAL_JNI_SHARED_LIBRARIES := libbluetooth_jni #Declare the name of the shared JNI library to be used

  • LOCAL_JAVA_LIBRARIES := javax.obex telephony-common services.net #Specify the dependent shared java class library. This is a compile-time dependency and will not be packaged in the end.

  • LOCAL_STATIC_JAVA_LIBRARIES := \

    com.android.vcard \

    bluetooth.cc\

    services.net \

    libprotobuf-java-lite \ #Specify the static java class library that you depend on, which will eventually be packaged into apk. When citing multiple references, you can write them as above.

  • LOCAL_STATIC_ANDROID_LIBRARIES := android-support-v4 #Declare the package to call android, here is the v4 package

  • LOCAL_REQUIRED_MODULES := libbluetooth #Specify dependent modules. Once this module is installed, the module specified by this variable will also be installed

  • LOCAL_PROGUARD_ENABLED := disabled #Obfuscation configuration, the default is full obfuscation, full code obfuscation, disabled is not enabled

  • include $(BUILD_PACKAGE) #Compile into APK file

  • include $(BUILD_STATIC_JAVA_LIBRARY) #Generate static JAVA library

3. Reference examples

3.1 Shared library mk

Below is an example of an mk file that generates a shared library.

LOCAL_PATH:= $(call my-dir)

include$(CLEAR_VARS)

LOCAL_MODULE:=helloworld

LOCAL_SRC_FILES:= helloworld.c

include$(BUILD_SHARED_LIBRARY)

Let’s explain these lines of code:

LOCAL_PATH := $(call my-dir)

Android.mk file must define the LOCAL_PATH variable. It is used to find source files in the development tree. The function ‘my-dir’, provided by the compilation system, is used to return the current path (that is, the directory containing the Android.mk file).

include $( CLEAR_VARS)

CLEAR_VARS is provided by the compilation system and specifies that GNU MAKEFILE clears many LOCAL_XXX variables for you (such as LOCAL_MODULE, LOCAL_SRC_FILES, LOCAL_STATIC_LIBRARIES, etc…),

Except LOCAL_PATH. This is necessary because all compilation control files are in the same GNU MAKE execution environment and all variables are global.

LOCAL_MODULE:= helloworld

The LOCAL_MODULE variable must be defined. It is the identifier of each module you describe in the Android.mk file. It must be unique and not contain any spaces. Note that the compilation system will automatically generate appropriate prefixes and suffixes. For example, a shared library module named ‘foo’ will generate the ‘libfoo.so’ file.

LOCAL_SRC_FILES:= helloworld.c

The LOCAL_SRC_FILES variable contains a list of source files that will be compiled and packaged into the module.

include$(BUILD_SHARED_LIBRARY)

What module type needs to be compiled into? BUILD_SHARED_LIBRARY is a variable provided by the compilation system, which represents the generation of a dynamic library; it points to a GNU Makefile script (which should be shared_library.mk in the build/core directory).

3.2 Precompiled Applicationmk

LOCAL_PATH := $(call my-dir)
ifeq ($(strip $(SYSTEMUI_SUPPORT)),yes)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := platform
LOCAL_MODULE := SystemUI
ifeq ($(TARGET_BUILD_VARIANT),user)
LOCAL_SRC_FILES := $(LOCAL_MODULE)-release.apk
else
LOCAL_SRC_FILES := $(LOCAL_MODULE)-debug.apk
endif
LOCAL_MULTILIB := both
LOCAL_PRIVILEGED_MODULE := true
include $(BUILD_PREBUILT)
endif

Then analyze some of the new variables:

ifeq ($(strip $(SYSTEMUI_SUPPORT)),yes)

ifneq: judgment statement, used to compare two parameters. If the two parameters are not equal, the statement passes.

# If a and b are not equal, do something
ifneq ($(a), $(b))
    # do something
endif

Here is the judgment whether to continue execution;

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE_TAGS indicates which version the module is compiled in. Optional means the module is compiled in all versions.

LOCAL_MODULE_CLASS := APPS

LOCAL_MODULE_CLASS defines the module’s classification. According to the classification, the generated module files will be installed in the corresponding directory of the target system. For example: APPS: installed under /system/app; SHARED_LIBRARIES: installed under /system/lib; EXECUTABLES: installed under /system/bin; ETC: installed under /system/etc;

LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)

LOCAL_MODULE_SUFFIX specifies the suffix of the current module. Once specified, when the system generates the target file, it will create the target file with the module name plus a suffix. Here it is designated as apk.

LOCAL_CERTIFICATE := platform

LOCAL_CERTIFICATE signature authentication, platform means signing using the platform signature file.

LOCAL_MULTILIB := both

Specify the compilation target as 32-bit or *64-bit, * its value can be 32, 64, both, indicating that the compiled apk can run in 32-bit, 64-bit, or 32&64 bit hardware platform.

LOCAL_PRIVILEGED_MODULE := true

LOCAL_PRIVILEGED_MODULE is related to compilation, installation, permission management and other aspects. If it is not set or set to false, the installation location is system/app; if it is true, the installation location is system/priv-app.

4. Examples

LOCAL_PATH := $(my-dir)
################################################ #############################
$(warning [xin.wang] : Build ApeFtm_Mtk_USER)

include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := ApeFtm_Mtk_USER
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := platform
LOCAL_PRIVILEGED_MODULE := true

LOCAL_REQUIRED_MODULES := ApeFtm_Mtk_USER_permission.xml
include $(BUILD_PREBUILT)

#Permissions pre-grant
include $(CLEAR_VARS)
LOCAL_MODULE := ApeFtm_Mtk_USER_permission.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)