Is checking SDK_INT enough or is lazy loading needed for using newer android APIs ? Why?

南笙酒味 提交于 2020-01-09 11:26:12

问题


Code such as :

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
        ed.apply();
    else ed.commit();

produces a warning in Froyo :

04-27 03:40:35.025: W/dalvikvm(3138): VFY: unable to resolve interface method 219: Landroid/content/SharedPreferences$Editor;.apply ()V

But I understand that in older devices this would be a RuntimeError which would abort the application (see here and here).

So is this way of conditionally calling new API (methods) safe in API 8 (Froyo) and above or there are cases where lazy loading is still needed ?

What changes on Dalvik made this possible ?

Related

  • 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

回答1:


produces a warning in Froyo

This is perfectly normal.

But I understand that in older devices this would be a RuntimeError which would abort the application

For Android 1.x, yes.

So is this way of conditionally calling new API (methods) safe in API 8 (Froyo) and above

Yes.

What changes on Dalvik made this possible ?

It no longer "fails fast" when encountering an unidentified symbol, but instead waits to try again to resolve it when the statement is executed. By checking SDK_INT and ensure that the statement is not executed, you don't crash.



来源:https://stackoverflow.com/questions/20271593/is-checking-sdk-int-enough-or-is-lazy-loading-needed-for-using-newer-android-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!