Android.mk 语法详解

一.Android.mk简介

Android.mk 是Android 提供的一种makefile 文件,注意用来编译生成(exe,so,a,jar,apk)等文件。

Android.mk 语法详解_第1张图片

二.Android.mk编写

分析一个最简单的Android.mk

LOCAL_PATH := $(call my-dir)  //定义了当前模块的相对路径

include $(CLEAR_VARS)  //清空当前环境变量

LOCAL_MODULE:= test  //编译生成的目标名称

LOCAL_SRC_FILES := test.c  //编译该模块需要的源文件

include $(BUILD_EXECUTABLE)  //编译所生成的目标文件格式

三.Android.mk编译静态库动态库

Android.mk 语法详解_第2张图片

编译动态库:编译类型修改为 BUILD_SHARED_LIBRARY

编译静态库:编译类型修改为 BUILD_STATIC_LIBRARY

Android.mk 编译多个目标文件

编译动态库

C/C++ 文件编译生成静态库.so文件参考如下:

LOCAL_PATH := $(call my-dir) 
include $(CLEAR_VARS) 
# 生成libhell.so
LOCAL_MODULE = libhello 
​​​LOCAL_CFLAGS = $(L_CFLAGS) 
LOCAL_SRC_FILES = hello.c 
LOCAL_C_INCLUDES = $(INCLUDES) 
LOCAL_SHARED_LIBRARIES := libcutils 
LOCAL_COPY_HEADERS_TO := libhello 
LOCAL_COPY_HEADERS := hello.h 
#编译动态库 BUILD_SHARED_LIBRARY
include $(BUILD_SHARED_LIBRARY) 

编译静态库

C/C++ 文件编译生成静态库.a文件参考如下:

#编译静态库 
LOCAL_PATH := $(call my-dir) 
include $(CLEAR_VARS) 
# 生成libhell.a
LOCAL_MODULE = libhello
LOCAL_CFLAGS = $(L_CFLAGS) 
LOCAL_SRC_FILES = hello.c 
LOCAL_C_INCLUDES = $(INCLUDES) 
LOCAL_SHARED_LIBRARIES := libcutils 
LOCAL_COPY_HEADERS_TO := libhello 
LOCAL_COPY_HEADERS := hellos.h 
# 编译 静态库 BUILD_STATIC_LIBRARY
include $(BUILD_STATIC_LIBRARY) 

四.Android引入系统库

Android.mk 语法详解_第3张图片

引用静态库
LOCAL_STATIC_LIBRARIES += libxxxxx

引用动态库
LOCAL_SHARED_LIBRARIES += libxxxxx

引用第三方库文件
LOCAL_LDFLAGS:=-L/PATH -Lxxx

引用第三方头文件
LOCAL_C_INCLUDES :=path

五.Android编译apk jar包

Android.mk 语法详解_第4张图片

编译apk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
# 生成hello apk
LOCAL_PACKAGE_NAME := hello
include $(BUILD_PACKAGE)

 编译jar包:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
# 生成 hello
LOCAL_MODULE := hello
# 编译生成静态jar包
include $(BUILD_STATIC_JAVA_LIBRARY)
#编译生成共享jar
include $(BUILD_JAVA_LIBRARY)

六.Android.mk 中的判断语句

ifeq($(VALUE), x) #ifneq
 
do_yes
 
else
 
do_no
 
endif
 
 
ifeq/ifneq:根据判断条件执行相关编译

你可能感兴趣的:(Android,android)