Retrieve minSdkVersion programmatically

萝らか妹 提交于 2019-12-06 16:39:21

minSDKVersion is one of the settings in the Android manifest for which there is no API to read the value.

It's not impossible to get hold of - you just end up having to read the AndroidManifest.xml file yourself via XmlResourceParser parser = getResources().getAssets().openXmlResourceParser("AndroidManifest.xml")

and then parsing the appropriate information out of it.

I think what you probably actually want to be doing is retrieving the SDK version of the device running the app. It does not make sense to get the minSdkVersion in any scenario I can think of, which as far as I know is impossible anyways, because how is it relevant at all to the application being run on the device? What you want is the device's SDK version so you can check whether or not it's above a certain version, upon which you can perform some action.

If you indeed want the SDK version of the device you can use android.os.Build.VERSION.SDK_INT.

Or if you want to support all android versions then android.os.Build.VERSION.SDK, but the latter will return a String, which you should convert to an int by Integer.parseInt(Build.VERSION.SDK).

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