代码如:
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
ed.apply();
else ed.commit();
在Froyo发出警告:
04-27 03:40:35.025: W/dalvikvm(3138): VFY: unable to resolve interface method 219: Landroid/content/SharedPreferences$Editor;.apply ()V
但据我所知,在较旧的设备中,这将是一个RuntimeError,它会中止应用程序(参见here和here).
那么有条件地在API 8(Froyo)及以上版本中有条件地调用新API(方法)的方式是否仍然需要延迟加载?
Dalvik有什么变化使这成为可能?
有关
> Android recommended way of safely supporting newer apis has error if the class implements a newer interface. Why?
> Android solving compatibility with SDK_INT hack; is this ok?
> Android Dalvik Verification in Eclair
解决方法:
produces a warning in Froyo
这是完全正常的.
But I understand that in older devices this would be a RuntimeError which would abort the application
对于Android 1.x,是的.
So is this way of conditionally calling new API (methods) safe in API 8 (Froyo) and above
是.
What changes on Dalvik made this possible ?
遇到未识别的符号时,它不再“快速失败”,而是在执行语句时等待再次尝试解决它.通过检查SDK_INT并确保未执行该语句,您不会崩溃.