Home » Android » intellij idea – How do I make sure unsupported (higher level) API is not called in Android?

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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)

How to&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

Answer:

In Eclipse Properties –> Android Lint Preferences –> NewApi and change the Severity to Error.