Conditional compilation in Android?

Is there any conditional compilation for Android?

I had to make my project for Android 3 (API 11) only because ExifInterface has almost no useful attributes in Android 2.3 (API 10), despite the fact that it appeared in API 5 (!! ??). I do not want to limit my application to ICS users.

Thanks!

+6
source share
3 answers

You can dynamically check the current version of the API device and do different things depending on this:

if(Build.VERSION.SDK_INT < 14) { // Crappy stuff for old devices } else { // Do awesome stuff on ICS } 

But be careful if you need to instantiate classes that are not available for all APIs, then you must do this in runnable or in a separate wrapper class, for example:

  if(Build.VERSION.SDK_INT < 14) { // Crappy stuff for old devices } else { // Do awesome stuff on ICS new Runnable() { new AmazingClassAvailableOnICS(); (...) }.run(); } 
+9
source

import android.annotation.TargetApi;

and then use annotations:

 @TargetApi(11) public void methodUsesAPI11() { ... 

Using this trick does a very simple thing: it allows you to compile some code that contains API level 11 calls (classes, methods, etc.) and still sets android:minSdkVersion="8" in the manifest. Nothing more, nothing more.

The rest is up to you. You must check the platform version before calling methodUsesAPI11() or handle exceptions to prevent the application from crashing and perform other actions on older platforms.

+7
source

Checking Build.VERSION.SDK_INT or using annotations should be sufficient, however this link, which I added to my bookmarks, may be relevant to your case: http://android-developers.blogspot.com/2010/07/how-to- have-your-cupcake-and-eat-it-too.html? m = 1

You can use what they describe there to have classes that may be incompatible but never be loaded. This is not conditional compilation, but it may be what you need, however, it is a bit more complicated.

+2
source

All Articles