9.2 Android.mk语法分析
9.2.1 编译系统变量解析
9.2.2 编译可执行文件
9.2.3 编译链接库及使用
9.2.4 编译java包及使用
9.2.5 编译应用apk
9.2 Android.mk语法分析
Android的Makefile,用来向编译系统描述编译的
目标,规则,依赖等
与Linux的Makefile功能相同,但语法格式不一致
9.2.1 编译系统变量解析
LOCAL_MODULE - 编译的目标对象
LOCAL_SRC_FILES - 编译的源文件
LOCAL_C_INCLUDES - 需要包含的头文件目录
LOCAL_SHARED_LIBRARIES - 链接时需要的外部库
LOCAL_PRELINK_MODULE - 是否需要prelink处理
LOCAL_JAVA_LIBRARIES - 加入jar包 LOCAL_PACKAGE_NAME - Java 应用程序的名字用该变量定义
LOCAL_PATH - 编译时的目录
include $(CLEAR_VARS) - 清除之前的一些系统变量
9.2.1 编译系统变量解析
BUILD_SHARED_LIBRARY - 指明要编译成动态库
BUILD_HOST_STATIC_LIBRARY -指明要编译成主机端静态库
BUILD_STATIC_LIBRARY - 指明要编译成静态库
BUILD_HOST_SHARED_LIBRARY -指明要编译成主机端动态库
BUILD_EXECUTABLE -指明要编译成可执行文件
BUILD_HOST_EXECUTABLE -指明要编译成主机端可执行文件
BUILD_PACKAGE -指明要编译成apk
BUILD_HOST_PREBUILT -指明要编译成主机端预编译文件
BUILD_PREBUILT -指明要编译成预编译文件
BUILD_JAVA_LIBRARY -指明要编译成动态jar包 BUILD_STATIC_JAVA_LIBRARY -指明要编译成静态jar包 BUILD_HOST_JAVA_LIBRARY -指明要编译成主机端jar包
9.2.2 编译可执行文件
源码:hello_exe/ Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello_exe
LOCAL_SRC_FILES := main.c
#目标板共享
include $(BUILD_EXECUTABLE)
#主机端共享
include $(BUILD_HOST_EXECUTABLE)
#目标板静态
include $(BUILD_EXECUTABLE)
#主机端静态
include $(BUILD_EXECUTABLE)
9.2.2 编译可执行文件
注意事项: 对于非静态编译,LOCAL_SHARED_LIBRARIES 默认
情况下包括了libc++, libc;但是对于静态编译,则需要
在Android.mk中显示指明这些依赖的静态库
对于依赖多个库的情况,
LOCAL_SHARED_LIBRARIES 或者
LOCAL_STATIC_LIBRARIES 变量里库应该按照一定的
顺序,如libhello依赖libc,则libhello先于libc
9.2.3 编译链接库
源码:hello_lib/ Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE = libhello
LOCAL_SRC_FILES = main.c
LOCAL_C_INCLUDES = $(INCLUDES)
#目标板共享库
include $(BUILD_SHARED_LIBRARY)
#主机端共享库
include $(BUILD_HOST_SHARED_LIBRARY)
#主机端静态库
include $(BUILD_STATIC_LIBRARY)
#主机端静态库
include $(BUILD_HOST_STATIC_LIBRARY)
9.2.4 编译java包 源码:hello_jar/jar
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := hello_jar
include $(BUILD_JAVA_LIBRARY)
9.2.5 编译应用apk
源码:hello_jar/test
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_JAVA_LIBRARIES := hello_jar
LOCAL_PACKAGE_NAME := hello_apk
include $(BUILD_PACKAGE)
https://developer.android.google.cn/ndk/guides/android_mk?hl=zh-cn#local_export_c_includes
https://www.it610.com/article/1279972980893433856.htm
如何引入系统库
#LOCAL_SHARED_LIBRARIES += libxxx
LOCAL_SHARED_LIBRARIES += liblog
如何引入第三方库(动态库 xxx.so)
include $(CLEAR_VARS)
LOCAL_MODULE := test
#LOCAL_C_ALL_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := src/main.cpp
LOCAL_SHARED_LIBRARIES += liblog
#引入第三方库
LOCAL_LDFLAGS := -L./lib/ -ltest
#LOCAL_LDFLAGS := -L路径 -l库名
LOCAL_MODULE_PATH := $(LOCAL_PATH)/bin
include $(BUILD_EXECUTABLE)
如何引入第三方头文件
#引入第三方头文件
LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libtest
#LOCAL_C_ALL_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := src/test.cpp
LOCAL_MODULE_PATH_64 := $(LOCAL_PATH)/lib
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := test
#LOCAL_C_ALL_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := src/main.cpp
LOCAL_SHARED_LIBRARIES += liblog
#引入第三方头文件
LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc
#引入第三方库
LOCAL_LDFLAGS := -L./lib/ -ltest
LOCAL_MODULE_PATH := $(LOCAL_PATH)/bin
include $(BUILD_EXECUTABLE)
代码中直接引用绝对路径即可
#include<stdio.h>
#include<utils/Log.h>
#include<test.h>
#define LOG_TAG “main”
//extern void callMyDir(void);
int main(void)
{
//test中的函数
callMyDir();
ALOGE(“test”);
return 0;
}
如何引入静态库 (xxx.a)
首先编译出来一个静态库然后放在lib路径下
#引入第三方静态库
LOCAL_LDFLAGS := $(LOCAL_PATH)/lib/libtest.a
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libtest
#LOCAL_C_ALL_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := src/test.cpp
LOCAL_MODULE_PATH_64 := $(LOCAL_PATH)/lib
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := test
#LOCAL_C_ALL_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := src/main.cpp
LOCAL_SHARED_LIBRARIES += liblog
LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc
#引入第三方静态库
LOCAL_LDFLAGS := $(LOCAL_PATH)/lib/libtest.a
LOCAL_MODULE_PATH := $(LOCAL_PATH)/bin
include $(BUILD_EXECUTABLE)
如何在mk文件中写判断
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
USED_SHARED := true
#LOCAL_C_ALL_FILES := $(call all-c-files-under)
LOCAL_SRC_FILES := src/main.cpp
LOCAL_SHARED_LIBRARIES += liblog
LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc
#判断
ifeq ($(USED_SHARED),true)
LOCAL_LDFLAGS := $(LOCAL_PATH)/lib/libtest.a
else
LOCAL_LDFLAGS := -L./lib/ -ltest
endif
LOCAL_MODULE_PATH := $(LOCAL_PATH)/bin
include $(BUILD_EXECUTABLE)