Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling with Android 4.X but supporting API Level 9

I'm working on an application which uses ActionBarSherlock. As it's documentation points out:

[...] the library requires that both it and your project are compiled with Android 4.0 or newer. The project also requires that you are compiling with JDK 1.6 in both your editor and any build systems that you may be using.

So, that means I'll compile my application (and the library) against Android 4.X but in my Manifest, I declare that I'm targeting (e.g.) API Level 9.

This all works fine and well but there is something that disturbs me. From the FAQ:

What API level should I target in my manifest when using the library?

Targetting API level 11 or newer is required as it will cause Android to automatically add the native action bar when run on newer devices. Since you will be compiling against new APIs but your app will likely be run on devices with older versions of Android extra care must be taken to either avoid using or properly check and call any methods that were introduced after your minimum SDK version.

That means, that I'll have to manually check every method call, so I don't use any that are not available in my targeted API Level (9 in my case)? This sounds wrong to me.

Is there a way to tell my IDE (IntelliJ), that I'm only using the API Level 9 (so I don't get any auto-completion for non-existing methods/classes and don't use them by accident) and then choose to compile it against another Android version?

Or can I use some automated checks (which run at compile time) to check for that?

like image 583
Lukas Knuth Avatar asked Jul 30 '12 18:07

Lukas Knuth


People also ask

What is the API level of Android 9?

Android 9 (API level 28)

What API level should I develop for Android?

Starting November 1, 2022 if your app doesn't target API level 30 or above, new users with newer Android OS versions won't be able to discover or install your app on Google Play. You can request an extension if you need more time to update your app.

What is the difference between compileSdkVersion and targetSdkVersion?

targetSdkVersion cannot be higher than the compileSdkVersion simply because we cannot target things that we know nothing about during compilation. Ideally, the compileSdkVersion and targetSdkVersion should be equal and both point to the latest SDK.


1 Answers

The ADT's lint feature should take care of this by warning when API calls are being made for the wrong API version.

You should be compiling both ABS and your project with the latest SDK available (at present, 4.1). Your manifest should have a targetSdkVersion as high as possible (ideally matching your compilation SDK) and your minSdkVersion should be set to the lowest version you support.

Lint is partially integrated with IntelliJ IDEA and is also available as a command line tool.

like image 186
Jake Wharton Avatar answered Sep 29 '22 13:09

Jake Wharton