PLATFORM_VERSION 在 AOSP 构建目录中定义:
构建/核心/version_defaults.mk:
ifeq "" "$(PLATFORM_VERSION)"
# This is the canonical definition of the platform version,
# which is the version that we reveal to the end user.
# Update this value when the platform version changes (rather
# than overriding it somewhere else). Can be an arbitrary string.
PLATFORM_VERSION := 5.1
endif
在您的产品(或其他任何地方)的 makefile 中定义以下 make 变量并将它们作为宏传递给编译器:
# Passing Android version to C compiler
PLATFORM_VERSION_MAJOR := $(word 1, $(subst ., ,$(PLATFORM_VERSION)))
PLATFORM_VERSION_MINOR := $(word 2, $(subst ., ,$(PLATFORM_VERSION)))
PLATFORM_VERSION_REVISION := $(word 3, $(subst ., ,$(PLATFORM_VERSION)))
COMMON_GLOBAL_CFLAGS += -DPLATFORM_VERSION_MAJOR=$(PLATFORM_VERSION_MAJOR) \
-DPLATFORM_VERSION_MINOR=$(PLATFORM_VERSION_MINOR)
ifneq ($(PLATFORM_VERSION_REVISION),)
COMMON_GLOBAL_CFLAGS += -DPLATFORM_VERSION_REVISION=$(PLATFORM_VERSION_REVISION)
endif
定义带有版本代码的头文件:
android_version.h:
#define ANDROID_VERSION(major, minor, rev) \
((rev) | (minor << 8) | (major << 16))
#ifndef PLATFORM_VERSION_REVISION
#define PLATFORM_VERSION_REVISION 0
#endif
#define ANDROID_VERSION_CODE ANDROID_VERSION( \
PLATFORM_VERSION_MAJOR, \
PLATFORM_VERSION_MINOR, \
PLATFORM_VERSION_REVISION)
现在,要根据 android 版本做出编译时间决定,只需包含android_version.h文件并使用预处理器#if。