Android.mk basic syntax and examples

Examples of this article are as follows:
https://github.com/hanyuhang-hz/android-demos
testExternal/test.zip

Android. The compilation of MK can be divided into:
(1)C/C++
It can be compiled into executable file, dynamic library or static library file.
(2)Java
It can be compiled into jar package or apk.

1 a simple Android MK example

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

test.c:

#include <stdio.h>

int main() {
    printf("hyh test! \n");
    return 0;
}

mmm external/test/1

...
[100% 3/3] Install: external/test/test

#### build completed successfully (19 seconds) ####

View the format of the generated executable file: file test

external/test/test: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /system/bin/linker, BuildID[md5/uuid]=f2b0744bf9b392b2100f5dc43c9b112f, stripped

Let's analyze this simple Android mk:
(1)LOCAL_PATH:=$(call my-dir)
LOCAL_PATH refers to Android The directory where MK is located.
(2)include $(CLEAR_VARS)
CLEAR_VARS means to clear all environment variables except LOCAL_PATH.
Why should "include $(CLEAR_VARS)" be placed after "LOCAL_PATH: = $(call my DIR)"? Because if you put it before, clear_ vars. The path of MK will be treated as local_ PATH!!!
(3)LOCAL_MODULE:=test
Module name generated by compilation
(4)LOCAL_SRC_FILES:=test.c
Specify source file
(5)LOCAL_MODULE_PATH := $(LOCAL_PATH)                    
Specifies the directory where the target files are generated
(6)include $(BUILD_EXECUTABLE)
Set the format of the target file. Here is the executable file.

2 Android.mk basic grammar

2.1 multi source file compilation

(1) Add each file to Android In MK
Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c \
                   call.c
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

call.c:

void call() {
    return ;
}

test.c:

#include <stdio.h>

// extern: it can be placed in front of variables or functions to indicate that the definitions of variables or functions are in other files
extern void call();

int main() {
    call();
    printf("hyh test! \n");
    return 0;
}

(2) Use the functions provided by the system
Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
#LOCAL_SRC_FILES := test.c \
                   call.c
LOCAL_ALL_C_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := $(LOCAL_ALL_C_FILES)
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

Note: all-c-files-under is defined in build / core / definitions MK, my dir are also defined in this file

2.2 compiling multiple target files in one mk

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_ALL_C_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := $(LOCAL_ALL_C_FILES)
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)
LOCAL_MODULE := test1
LOCAL_ALL_C_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := $(LOCAL_ALL_C_FILES)
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

test.c:

#include <stdio.h>

int main() {
    printf("hyh test! \n");
    return 0;
}

Note: update all files in the directory, touch externel/test/*

2.3 compiling dynamic library

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcall
LOCAL_ALL_C_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := $(LOCAL_ALL_C_FILES)
#LOCAL_SRC_FILES := call.c
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)

The compilation type is BUILD_SHARED_LIBRARY
call.c:

void call() {
    return ;
}

2.4 compiling static library

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libtest
LOCAL_ALL_C_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := $(LOCAL_ALL_C_FILES)
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_STATIC_LIBRARY)

The compilation type is BUILD_STATIC_LIBRARY
call.c:

void call() {
    return ;
}

2.5 reference system library

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
LOCAL_SHARED_LIBRARIES += liblog
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

LOCAL_SHARED_LIBRARIES += liblog
test.c:

#include <stdio.h>
#define LOG_TAG "HYH"
#include <utils/Log.h>

int main() {
    printf("hyh test! \n");
    ALOGE("test!");
    return 0;
}

2.6 reference to third-party libraries

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcall
LOCAL_SRC_FILES := call.c
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
LOCAL_SHARED_LIBRARIES += liblog
LOCAL_LDFLAGS := -L./ -lcall
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

LOCAL_LDFLAGS := -L/Path -lxxx
call.c:

void call() {
    return ;
}

test.c:

#include <stdio.h>
#define LOG_TAG "HYH"
#include <utils/Log.h>

// extern: it can be placed in front of variables or functions to indicate that the definitions of variables or functions are in other files
extern void call();

int main() {
    call();
    printf("hyh test! \n");
    ALOGE("test!");
    return 0;
}

2.7 reference to third party header documents

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcall
LOCAL_SRC_FILES := call.c
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc/
LOCAL_SHARED_LIBRARIES += liblog
LOCAL_LDFLAGS := -L./ -lcall
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc/
Note: LOCAL_C_INCLUDES requires an absolute path.
inc/call.h:

#ifndef CALL_H
#define CALL_H
extern void call();
#endif

call.c:

void call() {
    return ;
}

test.c:

#include <stdio.h>
#define LOG_TAG "HYH"
#include <utils/Log.h>
#include <call.h>

int main() {
    call();
    printf("hyh test! \n");
    ALOGE("test!");
    return 0;
}

2.8 reference static library

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcall
LOCAL_SRC_FILES := call.c
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_STATIC_LIBRARY)

#include $(CLEAR_VARS)
#LOCAL_MODULE := test
#LOCAL_SRC_FILES := test.c
#LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc/
#LOCAL_SHARED_LIBRARIES += liblog
#LOCAL_LDFLAGS := $(LOCAL_PATH)/lib/libcall.a
#LOCAL_MODULE_PATH := $(LOCAL_PATH)
#include $(BUILD_EXECUTABLE)

Note: the static library is generated in the following directory and needs to be manually copied to the current directory.

out/target/product/xxx/obj/STATIC_LIBRARIES/libcall_intermediates/libcall.a

call.c:

void call() {
    return ;
}

test.c:

#include <stdio.h>
#define LOG_TAG "HYH"
#include <utils/Log.h>
#include <call.h>

int main() {
    call();
    printf("hyh test! \n");
    ALOGE("test!");
    return 0;
}

call.h:

#ifndef CALL_H
#define CALL_H
extern void call();
#endif

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcall
LOCAL_SRC_FILES := call.c
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc/
LOCAL_SHARED_LIBRARIES += liblog
LOCAL_LDFLAGS := $(LOCAL_PATH)/libcall.a
LOCAL_MODULE_PATH := $(LOCAL_PATH)
include $(BUILD_EXECUTABLE)

Note: LOCAL_LDFLAGS requires an absolute path.

2.9 compiling apk

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := TestPackage
include $(BUILD_PACKAGE)

BUILD_PACKAGE: indicates the apk generated by compilation
LOCAL_PACKAGE_NAME: indicates the apk name generated by compilation

2.10 compile and generate jar package

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := com.test.jar
include $(BUILD_STATIC_JAVA_LIBRARY)
#include $(BUILD_JAVA_LIBRARY)

jar package classification:
(1) Static jar package:
include $(BUILD_STATIC_JAVA_LIBRARY)
Used at compile time The jar file packaged from the class file can run on any java virtual machine
(2) Dynamic jar package:
include $(BUILD_JAVA_LIBRARY)
When compiling, based on the static jar package, use The jar file packaged by dex can only run on android system

2.11 apk reference jar package

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_STATIC_JAVA_LIBRARIES := static-library
#LOCAL_JAVA_LIBRARIES := share-library
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := TestPackage
include $(BUILD_PACKAGE)

2.12 precompiled jar package

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE := com.test.share
LOCAL_SRC_FILES := com.test.static
include $(BUILD_PREBUILT)

Precompile static jar packages into dynamic jar packages.
LOCAL_MODULE_CLASS: represents the precompiled file type
(1)JAVA_LIBRARIES: dynamic jar package
(2)APPS: apk
(3)SHARED_LIBRARIES: dynamic library file so
(4)EXECUTABLES: binary files

2.13 Android.mk judgment grammar

ifeq ($(VALUE, x))
    ...
else
    ...
endif

USED_SHARE := true
ifeq ($(USED_SHARE), true)
LOCAL_LDFLAGS := -L./ -ltest
else
LOCAL_LDFLAGS := $(LOCAL_PATH)/lib/libcall.a
endif

Added by tc48 on Sat, 12 Feb 2022 06:35:39 +0200