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

I am creating an application for Gingerbread and minSdkVersion=10 and targetSdkVersion=17 in my AndroidManifest.xml.

I know that I have to 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 / don’t know what I call the higher-level API and sometimes I wrap my program with a NoSuchMethodError exception.

Therefore, before publishing my application, I always set my project to use the Android SDK 2.3.3 and make sure that I do not make illegal method calls (for example, all the errors that I get are wrapped in an if that checks the android SDK), then install the SDK back to 4.2.2.

Is there a better way to make sure that an unsupported API is not called without switching the SDK?

(PS I am using IntelliJ)

+8
android intellij-idea lint
source share
2 answers

You can (should) run Android Lint to verify that:

Right-click the project (or package, class)> Analysis> Verify Code.

enter image description here

+7
source share

In the properties of Eclipse -> Android Lint Settings -> NewApi and change the severity to an error.

+2
source share

All Articles