3

I am building AOSP for a device. Is there a way to get the current AOSP version at native code compile time? I am looking for something similar to the LINUX_VERSION_CODE and KERNEL_VERSION(X,Y,Z) directives in Linux. More specifically, I would like to do something that looks like this in one of my own AOSP add-on projects:

#if (ANDROID_VERSION_CODE >= ANDROID_VERSION(4,2,1))
... compile something ...
#else
... compile something else...
#endif
smichak
  • 4,716
  • 3
  • 35
  • 47

2 Answers2

6

Probably, you can use PLATFORM_VERSION and/or PLATFORM_SDK_VERSION, please see version_defaults.mk

ozbek
  • 20,955
  • 5
  • 61
  • 84
  • 1
    I did just that. Parsed the PLATFORM_VERSION string in build time, passed them as PLATFORM_VERSION_MAJOR, PLATFORM_VERSION_MINOR, PLATFORM_VERSION_REVISION and defined an ANDROID_VERSION for creating a platform version code by shifting and or'ing those PLATFORM_VERSION_* values in compile time. – smichak May 30 '13 at 05:35
  • smichak - could you post your code here? PLATFORM_VERSION is always empty for me. Not sure what I'm doing wrong. – Bill May 22 '15 at 23:18
  • 1
    PLATFORM_VERSION is a make variable defined in build/core/version_defaults.mk. You can parse it in makefile of your own, extract the major, minor and revision numbers from it and build a version code. I added an answer for this. – smichak Jun 21 '15 at 13:05
4

The PLATFORM_VERSION is defined in the AOSP build directory:

build/core/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

In a makefile of your product (or anywhere else) define the following make variables and pass them as macros to the compiler:

# 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

Define a header file with the version code:

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)

Now, to make compile time decisions based on android version simply include the android_version.h file and use pre-processer #if's.

smichak
  • 4,716
  • 3
  • 35
  • 47