如何Build.VERSION_ codeS可能工作呢? [英] How can Build.VERSION_CODES possibly work?
问题描述
我很困惑就在Android API的内部工作。
如果我的应用程序编译的安卓5.0,那么它是可以接受的在设备上运行了以下工作安卓5.0及以上:
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.LOLLIPOP)
不过,这仍然有效,如果我运行较早版本的Android设备上运行它。我的假设是,该设备上的库没有为变量 Build.VERSION_ codeS.LOLLIPOP
的定义。那么如何才能变量对这些旧设备来解决,当应用程序运行此code?
那么如何才能变量对这些旧设备时,应用程序运行此code解决?
简单:没有变量
Build.VERSION_ codeS.LOLLIPOP
是静态最终诠释
。在 的javac
- 生成字节codeS会内联,当你引用 INT
价值
Build.VERSION_ codeS.LOLLIPOP
,而不是做的查找,在运行时该值。由于字节codeS包含 INT
,您的APK包含 INT
,因此,你不依赖于该框架的设备的版本,以提供 INT
给你。
Build.VERSION.SDK_INT
是没有的一个静态最终诠释
,因此, 是的值抬头望着运行。
I'm confused on the internal workings of the Android API's.
If my app is compiled against Android 5.0, then it's acceptable that the following works on a device running Android 5.0 and up:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
However, this still works if I run it on a device running older versions of Android. My assumption is that the library on that device doesn't have a definition for the variable Build.VERSION_CODES.LOLLIPOP
. Then how can the variable be resolved on those older devices when the app runs this code?
Then how can the variable be resolved on those older devices when the app runs this code?
Simple: there is no variable.
Build.VERSION_CODES.LOLLIPOP
is a static final int
. The javac
-generated bytecodes will inline the int
value when you reference
Build.VERSION_CODES.LOLLIPOP
, rather than doing the lookup for that value at runtime. Since the bytecodes contain the int
, your APK contains the int
, and therefore you are not dependent upon the device's edition of the framework to supply the int
to you.
Build.VERSION.SDK_INT
is not a static final int
, and therefore that value is looked up at runtime.
这篇关于如何Build.VERSION_ codeS可能工作呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!