I'm building an application for Gingerbread and up with minSdkVersion=10
and targetSdkVersion=17
in my AndroidManifest.xml.
I know that I should check if the API is supported before I call it, for example:
private void removeRule(RelativeLayout.LayoutParams params, int rule) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // API 17
params.removeRule(rule);
} else {
params.addRule(rule, 0);
}
}
But sometimes I forget/or don't know I'm calling a higher level API and occasionally crash my program with NoSuchMethodError
exception.
So before I publish my app, I always set my project to use Android SDK 2.3.3 and make sure I'm not making illegal method calls (e.g. all the errors I get are wrapped in an if
statement checking the android SDK version), then set the SDK back to 4.2.2.
Is there a better way to make sure unsupported API is not called without switching the SDK?
(P.S. I'm using IntelliJ)
If you are on at least API version 4 (Android 1.6 Donut), the current suggested way of getting the API level would be to check the value of android. os. Build. VERSION.
Test using the StrictMode API You can also test for non-SDK interfaces by using the StrictMode API. Use the detectNonSdkApiUsage method to enable this.
The Best API Level is contains follows 1) The best API covers 100% market but all are not prefect so our app should be covered at least 90% with all devices .
What is API Level? API Level is an integer value that uniquely identifies the framework API revision offered by a version of the Android platform. The Android platform provides a framework API that applications can use to interact with the underlying Android system.
You could (should) run Android Lint to check that:
Right click on the project (or package, class) > Analyze > Inspect Code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With