1. Android.mk file description
Android.mk is a makefile provided by Android, which can group source files into modules. The header file directory used for quoting, the *.c/*.cpp file that needs to be compiled, the jni source file, the specified compilation to generate the *.so shared library file or the *.a static library file, one or more modules can be defined, and The same source file can be used in multiple modules.
To facilitate module compilation, the compilation system has set up many module description environment variables and macro definitions. Some commonly used ones are listed below.
Module description environment variables:
-
LOCAL_SRC_FILES:
Source files contained in the current module;
-
LOCAL_MODULE:
The name of the current module;
-
LOCAL_PACKAGE_NAME
The name of the current APK application;
-
LOCAL_C_INCLUDES:
The header file path required by C/C++;
-
LOCAL_STATIC_LIBRARIES:
The name of the static link library required by the current module during static linking;
-
LOCAL_SHARED_LIBRARIES:
The name of the dynamic link library that the current module depends on at runtime;
-
LOCAL_STATIC_JAVA_LIBRARIES:
The Java static library that the current module depends on;
-
LOCAL_JAVA_LIBRARIES:
The Java shared library that the current module depends on;
-
LOCAL_CERTIFICATE:
The signature method of the current application, such as using the system signature platform.
-
LOCAL_MODULE_TAGS:
Indicates the identifier of the module compilation, which may be eng, user, tests or optional (default value). The value specifically means:
user: means that the module is only compiled under the user version
eng: means that the module is only compiled under the eng version
tests: means that the module is only compiled under the tests version
optional: means that the module is compiled under all versions
Defined include variables:
-
include $(BUILD_SHARED_LIBRARY)
Indicates that it needs to be compiled into a so dynamic link library.
-
include $(BUILD_JAVA_LIBRARY)
Indicates that it needs to be compiled into a java dynamic library.
-
include $(BUILD_JAVA_STATIC_LIBRARY)
Indicates that it needs to be compiled into a java static library.
-
include $(BUILD_STATIC_LIBRARY )
Indicates that it needs to be compiled into a so static library.
-
include $(BUILD_EXECUTABLE)
Represents compiled into Native C executable program
-
include $(BUILD_PREBUILT)
Indicates that the source file has been compiled and only needs to be copied
The defined function macro, the prototype is $(call <function>)
, the following are a few:
-
$(call my-dir)
Get the current folder path;
-
$(call all-java-files-under, )
Get all Java files in the specified directory;
-
$(call all-c-files-under,)
Get all C files in the specified directory;
-
$(call all-Iaidl-files-under, )
Get all AIDL files in the specified directory;
2. Provide several Android.mk templates
#编译so静态库
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE = libtest
LOCAL_SRC_FILES = testxxx.c
LOCAL_C_INCLUDES = $(INCLUDES)
LOCAL_SHARED_LIBRARIES := libcutils
include $(BUILD_STATIC_LIBRARY)
#编译为so动态库链接库
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE = libtest
LOCAL_SRC_FILES = testxx.c
LOCAL_C_INCLUDES = $(INCLUDES)
LOCAL_SHARED_LIBRARIES := libcutils
include $(BUILD_SHARED_LIBRARY)
#编译java动态库
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := testjar
include $(BUILD_JAVA_LIBRARY)
#编译java静态库
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := testjar
include $(BUILD_STATIC_JAVA_LIBRARY)
#编译为可执行文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hellos
LOCAL_STATIC_LIBRARIES := libhellos
LOCAL_SHARED_LIBRARIES :=
LOCAL_LDLIBS += -ldl
LOCAL_CFLAGS := $(L_CFLAGS)
LOCAL_SRC_FILES := mains.c
LOCAL_C_INCLUDES := $(INCLUDES)
include $(BUILD_EXECUTABLE)
#使用动态库
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hellod
LOCAL_MODULE_TAGS := debug
LOCAL_SHARED_LIBRARIES := libc libcutils libhellod
LOCAL_LDLIBS += -ldl
LOCAL_CFLAGS := $(L_CFLAGS)
LOCAL_SRC_FILES := maind.c
LOCAL_C_INCLUDES := $(INCLUDES)
include $(BUILD_EXECUTABLE)
#预编so动态链接库到指定目录
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libtest.so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_PATH := $(ANDROID_OUT_SHARED_LIBRARIES)
LOCAL_SRC_FILES := lib/$(LOCAL_MODULE )
OVERRIDE_BUILD_MODULE_PATH := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)
include $(BUILD_PREBUILT)
#预编二进制文件到系统
include $(CLEAR_VARS)
LOCAL_MODULE := testbin
LOCAL_SRC_FILES := testbin
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_MODULE_TAGS := optional
include $(BUILD_PREBUILT)
# 内置Apk文件到系统
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := adbinputmethod
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_MODULE_TAGS := optional
LOCAL_PRIVILEGED_MODULE := true
LOCAL_BUILT_MODULE_STEM := package.apk
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
# LOCAL_CERTIFICATE := platform
LOCAL_CERTIFICATE := PRESIGNED
include $(BUILD_PREBUILT)
3. Built-in apk in the source code to the system
Here takes the built-in Sogou input method into the system as an example.
(1). Create a directory to store the built-in App, here I created it as packages/myapps/sougou, as shown below:
qiang@ubuntu:~/lineageOs$ pwd
/home/qiang/lineageOs
qiang@ubuntu:~/lineageOs$ mkdir -p packages/myapps/sougou
qiang@ubuntu:~/lineageOs$
(2). Copy the Sogou input method apk to packages/myapps/sougou, and create the built-in apk to the system Android.mk. As follows:
qiang@ubuntu:~/lineageOs/packages/myapps/sougou$ pwd
/home/qiang/lineageOs/packages/myapps/sougou
qiang@ubuntu:~/lineageOs/packages/myapps/sougou$ ls -la
total 58480
drwxrwxrwx 2 qiang qiang 4096 1月 1 18:22 .
drwxrwxr-x 3 qiang qiang 4096 1月 2 02:25 ..
-rwxrw-rw- 1 qiang qiang 755 1月 1 18:29 Android.mk
-rwxrw-rw- 1 qiang qiang 59870272 11月 3 07:35 sougou.apk
The corresponding Android.mk content is:
# ///ADD START
# ///ADD END
# 设置当前工作路径
LOCAL_PATH:= $(call my-dir)
# 清除变量值
include $(CLEAR_VARS)
# 生成的模块名称
LOCAL_MODULE := sougou
# 生成的模块类型
LOCAL_MODULE_CLASS := APPS
# 生成的模块后缀名,此处为apk
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
# 设置模块tag,tags取值可以为:user debug eng tests optional
# optional表示全平台编译
LOCAL_MODULE_TAGS := optional
# LOCAL_PRIVILEGED_MODULE := true
LOCAL_BUILT_MODULE_STEM := package.apk
# 设置源文件
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
# LOCAL_CERTIFICATE := platform
# 设置签名,此处表示保持apk原有签名
LOCAL_CERTIFICATE := PRESIGNED
# 此处表示预编译方式
include $(BUILD_PREBUILT)
(3) Configure the created Sogou input method compilation module to the compilation system. This is very important. If you do not add the compilation image, the module just added will not be compiled by default. In lineageOs, you can add sougou where you add the Settings module in the following path.
File location:
build/make/target/product/handheld_product.mk
Part of the content after adding the sougou content is as follows:
PRODUCT_PACKAGES += \
Browser2 \
Calendar \
Camera2 \
Contacts \
DeskClock \
Gallery2 \
Launcher3QuickStep \
Music \
OneTimeInitializer \
Provision \
QuickSearchBox \
Settings \
SettingsIntelligence \
StorageManager \
SystemUI \
WallpaperCropper \
frameworks-base-overlays \
sougou
(4) Compile and flash
qiang@ubuntu:~/lineageOs/build/make/target/product$ croot
qiang@ubuntu:~/lineageOs$ breakfast oneplus3
Looking for dependencies in device/oneplus/oneplus3
Looking for dependencies in device/oppo/common
device/oppo/common has no additional dependencies.
Looking for dependencies in kernel/oneplus/msm8996
kernel/oneplus/msm8996 has no additional dependencies.
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=10
LINEAGE_VERSION=17.1-20210102-UNOFFICIAL-oneplus3
TARGET_PRODUCT=lineage_oneplus3
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=kryo
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv8-a
TARGET_2ND_CPU_VARIANT=kryo
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-5.4.0-58-generic-x86_64-Ubuntu-20.04.1-LTS
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=QQ3A.200805.001
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=vendor/oneplus/oneplus3 device/oneplus/oneplus3 vendor/nxp/opensource/pn5xx hardware/qcom-caf/msm8996
============================================
qiang@ubuntu:~/lineageOs$ brunch oneplus3
Looking for dependencies in device/oneplus/oneplus3
Looking for dependencies in device/oppo/common
device/oppo/common has no additional dependencies.
Looking for dependencies in kernel/oneplus/msm8996
kernel/oneplus/msm8996 has no additional dependencies.
4. Effect display
Pay attention to the official account and get updated content in time: