Compatibility for Java library methods that require Android API level 9?

蹲街弑〆低调 提交于 2019-12-12 16:16:20

问题


Some Java library methods like DecimalFormat.setRoundingMode have a @since 1.6 entry in their documentation.

/**
...
 * @since 1.6
 */
public void setRoundingMode(RoundingMode roundingMode) {
...
}

When I tried to use this method compiled under Android 4.2.2 and JDK 1.6 with my android:minSdkVersion set to 7 as shown,

myNumberFormat.setRoundingMode(RoundingMode.DOWN);

Android Lint underlined setRoundingMode in red for me and told me that

Call requires API level 9 (current min is 7): java.text.NumberFormat#setRoundingMode

How and why can the Android API restrict which Java library methods I can and cannot use? I cleaned my project and the Lint error disappeared. My project compiled with no errors and ran on a device running Android 2.2.3 (API 8).
My program crashed with:

05-09 11:32:38.436: E/AndroidRuntime(2074): Caused by: java.lang.NoSuchMethodError: java.text.NumberFormat.setRoundingMode

The Android documentation confirms that setRoundingMode, copySign, and others were added in API level 9. Does that mean that devices running Android OS level 8 and below are specifically compiled/built with JDK 1.5?

And I can understand that. But is it impossible for the Android Support Library (or anything else) to allow us to use these methods?

Related to Rounding Half Up with Decimal Format in Android

来源:https://stackoverflow.com/questions/16471345/compatibility-for-java-library-methods-that-require-android-api-level-9

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