android system source code compilation steps
1. Source build / envsetup.sh & & Lunch XXX or. / build / envsetup.sh & & Lunch XXX. After executing the envsetup.sh script, the variables in envsetup.sh become global variables, and the functions can also be used directly in the command line of the current terminal.
Common function analysis
- lunch: select the target product and version to compile
- croot: switch to the top-level directory of the source code
- m: build the whole system from the top-level directory
- mm: build all modules in the current directory, but not their dependencies
- mmm: build all modules in the specified directory, but not their dependencies
- mma: build all modules in the current directory and the modules they depend on
- mmma: build all modules in the specified directory and the modules they depend on
Use examples and function explanations of android.mk
mk syntax allows the Source to be packaged into a module, which is divided into:
Dynamic library: it can be installed / copied to the application package (apk)
Static library: can be linked into dynamic library
One or more modules can be defined in a mk, or the same Source can be added to multiple modules.
Simple example display
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := hello.c
include $(BUILD_SHARED_LIBRARY)
-
LOCAL_PATH := $(call my-dir)
MK file must be signed by local_ Starting with path, it is used to find the source file in the development system source code tree. The macro my dir is provided by Build System and returns the directory path containing Android.mk. -
include $(CLEAR_VARS)
CLEAR_VARS variable is provided by the compilation system and points to a specified GNU Makefile, which is responsible for cleaning up many local files_ xxx. It is used to clean up variables and reset the compilation environment. Because all compilation control files are parsed and executed by the same GNU Makefile, their variables are global, so mutual influence can be avoided after cleaning. -
LOCAL_MODULE := hello
The name of the module must be defined. The name must be unique and cannot contain spaces. Build System will automatically add appropriate prefixes and suffixes. -
LOCAL_SRC_FILES := hello.c
Lists the c/c + + files that need to be compiled. -
include $(BUILD_SHARED_LIBRARY)
BUILD_SHARED_LIBRARY is a variable provided by Build System and points to a GNU Makefile script.
- BUILD_STATIC_LIBRARY: compile to static library
- BUILD_SHARED_LIBRARY: compile as a dynamic library
- BUILD_EXECUTABLE: compiled as a Native C executable
-
LOCAL_MODULE_TAGS := optional
Specify what type of version to compile under, usually user/debug/eng or optional (it will be compiled under all versions) -
LOCAL_PRIVATE_PLATFORM_APIS := true
Use the API hidden in the SDK to participate in compilation -
LOCAL_SDK_VERSION := current
Set the SDK version involved in compiling. (if this option is added to Android.mk, the hidden API of the source code will be ignored during compilation, so the compilation will fail after using the hide api of the source code)
- internal api
Translated into internal API, understood as API for internal use in sdk. Such interfaces were originally intended not to be exposed to the public - hide api
See the method or class marked with @ hide in the source code, that is, hide. This kind of interface is intended to be open, but the current stage is still unstable or undeveloped. Therefore, it is not recommended for developers to call. But you can use - General api
Third party app s can also use APIs
-
LOCAL_CERTIFICATE := platform
Specify how to sign -
LOCAL_USE_AAPT2 := true
aapt is the abbreviation of Android Asset Packaging Tool. It is a tool for compiling and packaging resources. appt2 is an upgraded version of appt. -
LOCAL_JNI_SHARED_LIBRARIES := libbluetooth
Shared libraries participating in compilation -
LOCAL_JAVA_LIBRARIES := javax
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
LOCAL_STATIC_JAVA_LIBRARIES :=
com.android.vcard
The static java class library that specifies the dependency will eventually be packaged into apk.
- LOCAL_STATIC_ANDROID_LIBRARIES
LOCAL_STATIC_ANDROID_LIBRARIES :=
androidx.appcompat_appcompat
androidx.fragment_fragment
Declare the package to call android
- LOCAL_PROGUARD_FLAG_FILES := proguard.flags
Add obfuscation rule configuration file to participate in compilation.
Usage examples of android.mk
- Compile apk with source code
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) # Source file, including java, Aidl and local files_ SRC_ FILES += src/com/goodocom/gocsdk/IGocsdkCallback.aidl LOCAL_SRC_FILES := $(call all-java-files-under, src) # Resource resource file LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res # Androidmanifest.xml file LOCAL_MANIFEST_FILE := $(LOCAL_PATH)/AndroidManifest.xml # Whether to enable AAPT2 is a tool for compiling and packaging resources_ MODULE_ Path APK storage path LOCAL_USE_AAPT2 := true LOCAL_MODULE_TAGS := optional LOCAL_MODULE_PATH := $(TARGET_OUT)/app LOCAL_PROGUARD_FLAG_FILES := proguard.flags # APK signature method LOCAL_CERTIFICATE := platform # Destination APK file name LOCAL_PACKAGE_NAME := QualBT # Enable odex optimization LOCAL_DEX_PREOPT := false # SDK version LOCAL_SDK_VERSION := current # The dependent static library so library already exists and does not need to be recompiled #LOCAL_PREBUILT_JNI_LIBS :=libs/armeabi/libserial_port.so # Private path / system/priv_app LOCAL_PRIVILEGED_MODULE := true # Dependent android static libraries LOCAL_STATIC_ANDROID_LIBRARIES := \ androidx.appcompat_appcompat \ androidx.fragment_fragment # Build APK include $(BUILD_PACKAGE)
- Compiling apk without source code
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := btTw LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(LOCAL_MODULE).apk LOCAL_MODULE_CLASS := APPS LOCAL_CERTIFICATE := platform #out\target\product\qssi\system\app LOCAL_MODULE_PATH := $(TARGET_OUT)/app include $(BUILD_PREBUILT)
Preset android.mk
- Preset jar
LOCAL_PATH:= $(call my-dir) # preset include $(CLEAR_VARS) LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := zxing:libs/core.jar \ gson:libs/gson-2.8.0.jar \ android-support-v7:libs/android-support-v7-recyclerview.jar include $(BUILD_MULTI_PREBUILT) # quote include $(CLEAR_VARS) ...... LOCAL_STATIC_JAVA_LIBRARIES += zxing LOCAL_STATIC_JAVA_LIBRARIES += gson LOCAL_STATIC_JAVA_LIBRARIES += android-support-v7 ...... include $(BUILD_PACKAGE)
*Preset so
LOCAL_PATH:= $(call my-dir) # preset include $(CLEAR_VARS) LOCAL_MODULE := mylib2 LOCAL_SRC_FILES_32 := lib/armeabi-v7a/mylib2.so LOCAL_SRC_FILES_64 := lib/arm64-v8a/mylib2.so LOCAL_MODULE_TAGS := optional LOCAL_MODULE_CLASS := SHARED_LIBRARIES LOCAL_MODULE_SUFFIX := .so LOCAL_PROPRIETARY_MODULE := true LOCAL_MULTILIB := both include $(BUILD_PREBUILT) ...... # quote include $(CLEAR_VARS) LOCAL_SHARED_LIBRARIES += mylib2 include $(BUILD_PACKAGE) ......
*Preset apk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Module name LOCAL_MODULE := NewGallery2 # Module name to be overwritten LOCAL_OVERRIDES_PACKAGES := Gallery Gallery3D GalleryNew3D Gallery2 DreamGallery2 # The module type is APPS LOCAL_MODULE_CLASS := APPS # Allow system hidden interfaces LOCAL_PRIVATE_PLATFORM_APIS := true # Signature. If you do not need to re sign, you can directly set it to PRESIGNED and use the existing signature; If you need to re sign, set it to the value of the corresponding signature. LOCAL_CERTIFICATE := platform # Target compiled output directory LOCAL_MODULE_PATH := $(TARGET_OUT)/priv-app # APK file LOCAL_SRC_FILES := apk/NewGallery2.apk # APK preset so ifeq ($(strip $(TARGET_ARCH)), arm64) LOCAL_PREBUILT_JNI_LIBS := libs/arm64-v8a/libjni_jpeg.so else ifeq ($(strip $(TARGET_ARCH)), x86_64) LOCAL_PREBUILT_JNI_LIBS := libs/x86_64/libjni_jpeg.so else ifeq ($(strip $(TARGET_ARCH)),arm) LOCAL_PREBUILT_JNI_LIBS := libs/armeabi-v7a/libjni_jpeg.so else LOCAL_PREBUILT_JNI_LIBS := libs/x86/libjni_jpeg.so endif include $(BUILD_PREBUILT)