Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do android.os.Build.VERSION_CODES work on older platforms

As pointed out by this question and a bit of testing I did, android.os.Build.VERSION_CODES work on platform versions that are older than the platform version of the VERSION_CODE you are using.

E.g. this:

android.os.Build.VERSION_CODES.HONEYCOMB

Also works on Gingerbread devices, although Honecomb was released after Gingerbread.

This seems to only work if your target SDK is later than the VERSION_CODE you use, which leads me to the assumtion that the version code somehow get compiled into the APK from the target SDK.

Now, my question is:

How does that work? According to the accepted answer of the linked question, this works via an int alias, but Java does not seem to have support for aliases.

like image 434
FD_ Avatar asked Dec 09 '22 09:12

FD_


2 Answers

It's a compile-time constant that gets inlined in the generated bytecode. Because of that, the symbol does not need to be resolved at runtime.

A compile-time constant is something that is

  • declared final
  • primitive or String
  • initialized within declaration
  • initialized with a constant expression
like image 90
laalto Avatar answered Jan 12 '23 01:01

laalto


The VERSION_CODES are all static final ints, meaning that when you build your app, all of the variables are just replaced with numbers. Basically, if you look in your app's bytecode after compilation, you will likely see any references to the VERSION_CODES just replaced with the actual int that each version code variable represents.

like image 42
NasaGeek Avatar answered Jan 12 '23 01:01

NasaGeek