Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make sure unsupported (higher level) API is not called in Android?

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)

like image 802
Ray Zhou Avatar asked Jun 12 '13 00:06

Ray Zhou


People also ask

How do I know my Android API level?

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.

How can I enable access to non SDK interfaces?

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.

What is the best API level in Android?

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 in Android apps?

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.


1 Answers

You could (should) run Android Lint to check that:

Right click on the project (or package, class) > Analyze > Inspect Code.

enter image description here

like image 141
Ahmad Avatar answered Sep 28 '22 14:09

Ahmad