Android.mk parsing and usage

Keywords: Java Android

1, Role of Android.mk file

Android.mk is an Android project management file, which is similar to the manual of the compiled file. It is used to describe the source code to the compiled system and group the source file into modules (including static library, shared library and independent executable file). Android.mk will be parsed by the compiling system one or more times. One or more modules can be defined in each android.mk file, or multiple modules can use the same. MK file.

2, Simple example

#Location of the source file in the development tree
LOCAL_PATH := $(call my-dir)
#Clear local_ Local other than path variable_ XXX variable
include $(CLEAR_VARS)

#Files to compile
LOCAL_SRC_FILES :=$(call all-subdir-java-files)

#Generated module name
LOCAL_MODULE := TestMK
#Compiled Tags
LOCAL_MODULE_TAGS := optional
#Specify signature
LOCAL_CERTIFICATE := platform

#Reference static jar
LOCAL_STATIC_JAVA_LIBRARIES := jar1 jar2

#Compile apk
include $(BUILD_PACKAGE)

#Libraries that need to be precompiled
include $(CLEAR_VARS)  
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := jar1:path1 \
										jar2:path2
include $(BUILD_MULTI_PREBUILT) 

3, Detailed analysis

1. Define the location of the current module

LOCAL_PATH := $(call my-dir)

LOCAL_PATH is a variable indicating the current module location, my dir is a macro function provided by the system to return the path of the current file, and $(call my DIR) means calling this function.

2. Clear LOCAL_XXX variable

include $(CLEAR_VARS)

This code clears local_ Local other than path variable_ XXX variable. All compilation control files are in the same GNU MAKE execution environment, and all variables are global. Other modules may have been compiled before compiling this module, resulting in a large number of variables, which will be mistaken by the system as belonging to this module, and unpredictable errors may occur.

3. Files to be compiled

LOCAL_SRC_FILES :=$(call all-subdir-java-files)

LOCAL_ SRC_ The files variable represents the file to be compiled, and the all subdir java files function returns local_ All java files in the path subdirectory. You can also directly write out the file path to be compiled:

LOCAL_SRC_FILES :=src/com/example/test/MainActivity.java \
                  src/com/example/test/Demo1.java \
                  src/com/example/test/Demo2.java 

However, it should be noted that the following statement is added at the end of the file to indicate LOCAL_PATH directory.

include $ (call all-makefiles-under,$(LOCAL_PATH))

Or add local to each file path_ PATH :

LOCAL_SRC_FILES :=$(LOCAL_PATH)/src/com/example/test/MainActivity.java \
                  $(LOCAL_PATH)/src/com/example/test/Demo1.java \
                  $(LOCAL_PATH) /src/com/example/test/Demo2.java 

Several common methods for obtaining source files:
$(call all Java files under, SRC): get all Java files in the specified directory.
$(call all-c-files-under, src): get all C language files in the specified directory.
$(call all IADL files under, SRC): get all AIDL files in the specified directory.
$(call all makefiles under, folder): get all Make files in the specified directory.

4. Define the module name generated by compilation

LOCAL_MODULE := TestMK

LOCAL_ The module variable must be defined and unique. As the identification of the module, the compilation system will automatically generate appropriate prefixes and suffixes.

5. Compiled Tags

LOCAL_MODULE_TAGS := optional

Commonly used are: debug, eng, user, development or optional (default).

5. Signature properties

LOCAL_CERTIFICATE := platform

Common are:
platform: the APK completes some core functions of the system. Through the access test to the folders existing in the system.
shared: the APK needs to share data with the home/contacts process.
Media: the APK is a part of the media/download system.

6. Reference static jar Library

LOCAL_STATIC_JAVA_LIBRARIES := jar1 jar2

jar1 and jar2 are aliases of third-party Java packages, which need to be defined. See later.
LOCAL_JAVA_LIBRARIES is used to reference dynamic jar s.

7. Compile into apk

include $(BUILD_PACKAGE)

include $(BUILD_STATIC_LIBRARY): compile into a static library
include $(BUILD_SHARED_LIBRARY): compile into dynamic library
include $(BUILD_EXECUTABLE): compile into executable program
include $(BUILD_STATIC_JAVA_LIBRARY): compile into a Java static library

8. Libraries that need to be precompiled

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := jar1:path1 \
										jar2:path2

jar1 and jar2 define the static library alias. path1 and path2 are the paths of the static library. Note that the suffix. Jar. Should be written all the time.

9. Copy to local compilation

include $(BUILD_MULTI_PREBUILT) 

Copy the library defined by prebuild to local for compilation.

10. Specify the generation directory

Via LOCAL_MODULE_PATH variable, you can specify the generated apk Directory:

LOCAL_MODULE_PATH := $(TARGET_OUT)/

$(TARGET_OUT) stands for / system, and the subsequent road strength shall be supplemented as needed

$(TARGET_OUT_DATA_APPS) represents the data/app directory

epilogue

A rookie. I hope the leaders can correct the mistakes. Thank you very much.
Main reference blog posts:
https://blog.csdn.net/kunminxu/article/details/102787853
https://blog.csdn.net/yuanjize1996/article/details/54376228
https://blog.csdn.net/gdutxiaoxu/article/details/78187420

Posted by Skepsis on Sat, 02 Oct 2021 15:43:14 -0700