Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling API from higher API level than the minimum requirement

Tags:

android

I wrote most of an app just fine with min API level set to 7. I want to call a single API from level 8. Users with lower versions of android will survive without this "extra feature".

Currently I added @SuppressLint("NewApi") so that my code can run; and I'm testing it on API 14. Everything works fine.

I wonder what the app will behave on API 7 devices. Would this one line be ignored? Would my app crash? Would the app be filtered by Google Play so they can't install?

I'd like to have this single line ignore on lower devices.

like image 703
user1032613 Avatar asked Dec 17 '25 19:12

user1032613


1 Answers

Would this one line be ignored?

No.

Would my app crash?

Spectactularly. :-)

Would the app be filtered by Google Play so they can't install?

No.

I'd like to have this single line ignore on lower devices.

You have two problems:

  1. @SuppressLint("NewApi") was the wrong quick-fix choice

  2. You didn't add any code to avoid this line on older devices

Use @TargetApi(...) instead of @SuppressLint("NewApi"), where ... is the name (e.g., FROYO) or number (e.g., 8) of the code that your method is referencing.

But before you do that, wrap your offending lines in a check to see if they should be executed on this device:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.FROYO) {
  // then execute your code that requires API Level 8
}
// optional else block if you have some workaround for API Level 7

Your if check will cause your line to be avoided. Your @TargetApi annotation will cause Lint to stop yelling at you about referencing a too-new class or method.

like image 89
CommonsWare Avatar answered Dec 20 '25 11:12

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!