Android.mk Common Grammar (1)

Android


foreword

Recently, when learning about Android, you must use Android.mk. To learn something, you must understand its grammar.

1. What is Android.mk

Android.mk is a Makefile for building Android applications. It is part of the Android NDK build system and is used to compile C/C++ code, generate static or shared libraries, and integrate them into Android applications.

The Android.mk file uses the syntax and rules of the GNU Make build tool to define the rules for compiling and linking. It provides developers with a way to describe project structure, source files, compilation options, dependencies, and more. By writing an Android.mk file, you can automate the build process, simplify compilation operations, and manage complex project structures.

The Android.mk file is usually located in the jni directory of the NDK project, and it works with the Android.mk file. The Android.mk file describes the build rules for native code (such as C/C++), while the Application.mk file describes the global build options, such as specifying the compiler version, target platform, and so on.

When writing an Android.mk file, you can define modules, set source files, specify compile and link options, define dependencies, and more. More complex construction logic can be realized through syntax such as conditional judgment, loop and function call.

In summary, the Android.mk file is a Makefile for building C/C++ code and libraries in the Android NDK, providing a powerful way to manage and build native code in the project.

2. Common grammar

1. Define the module

include $(CLEAR_VARS)
LOCAL_PATH := $(call my-dir)

# 定义模块
LOCAL_MODULE := mymodule

# 设置源文件
LOCAL_SRC_FILES := file1.c file2.c

# 设置头文件路径
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include

# 设置编译标志
LOCAL_CFLAGS := -Wall -O2

# 添加依赖库
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

Define the module name through LOCAL_MODULE, LOCAL_SRC_FILES set the source file, LOCAL_C_INCLUDES set the path of the header file, LOCAL_CFLAGS set the compilation flag, and LOCAL_LDLIBS add the dependent library. Finally use include $(BUILD_SHARED_LIBRARY) to build the shared library.

2. Import other Android.mk files

include $(CLEAR_VARS)
include $(LOCAL_PATH)/path/to/other.mk

Introduce other Android.mk files by include keyword and file path to reuse already defined modules and rules.

3. Add dependencies

LOCAL_STATIC_LIBRARIES := lib1 lib2

4. Use ifeq conditional judgment

YOUR_VARIABLE := some_value

ifeq ($(YOUR_VARIABLE), some_value)
    # 如果 YOUR_VARIABLE 的值为 some_value,则执行这里的代码
else
    # 否则执行这里的代码
endif

Depending on whether the value of $(YOUR_VARIABLE) is equal to some_value, the corresponding code block is executed.

ifeq ($(TARGET_ARCH_ABI), arm64-v8a)
    # 如果目标架构是 arm64-v8a,执行以下规则
    $(info Target architecture is arm64-v8a)
else ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)
    # 如果目标架构是 armeabi-v7a,执行以下规则
    $(info Target architecture is armeabi-v7a)
else
    # 如果以上条件都不成立,执行以下规则
    $(info Unknown target architecture)
endif

5. Generate target files

LOCAL_MODULE := your_module_name

include $(BUILD_EXECUTABLE)

By setting the LOCAL_MODULE variable, you can define the name of an executable file. Then, use include $(BUILD_EXECUTABLE) to set the current module to be of type executable, resulting in an object file.

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/131640361