Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out Android's version in AOSP on native code compile time?

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
like image 252
smichak Avatar asked May 14 '26 19:05

smichak


2 Answers

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

like image 107
ozbek Avatar answered May 17 '26 10:05

ozbek


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.

like image 34
smichak Avatar answered May 17 '26 10:05

smichak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!