Conditional compiling in Android?
Asked Answered
H

3

9

Is there any kind of conditional compiling for Android?

I had to make my project for Android 3 (API 11) just because ExifInterface has almost no useful attributes in Android 2.3 (API 10), despite the fact that it appeared in API 5 (!!??). I don't want to restrict my app to ICS users.

Hubey answered 14/11, 2012 at 13:56 Comment(0)
L
11

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

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

But be careful that if you need to instantiate classes that are not available for all APIs then you should do it in a runnable or in a separate wrapper class, e.g:

    if(Build.VERSION.SDK_INT < 14) {
        // Crappy stuff for old devices
    }
    else {
        // Do awesome stuff on ICS
        new Runnable() {
            new AmazingClassAvailableOnICS();
            (...)
        }.run();
    }
Lutyens answered 14/11, 2012 at 14:2 Comment(4)
This is nice!! Still, I compiled the whole thing with API 11. Will it still work in API 10 (except for the Runnable part)? Thanks!!Hubey
Sorry, when trying to implement this, I observed that you said: do it in a runnable **or** in a separate wrapper class. Still, you did both? Can I just put a method inside the runnable? (I'm getting errors implementing your suggestion: Syntax error, insert "}" to complete).Hubey
BTW, I only want to update a string variable msg if API >= 11. So would this simple code work? if(Build.VERSION.SDK_INT > 10) { String a = API11stuff ; msg = msg + a;}Hubey
Forget it, stupid me, I understand now what you meant. (Yes, my code will work).Hubey
M
10

import android.annotation.TargetApi;

and then use annotations:

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

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

The rest is up to you. You must check platform version before you call methodUsesAPI11() or you handle exceptions in order to prevent app crash and perform other action on older platforms.

Moving answered 14/11, 2012 at 14:3 Comment(2)
Sorry, I've a doubt now: what will happen when I run this code under an API10 and call the method meethodUsesAPI11()? I would like to do something like msg = meethodUsesAPI11(). Wouldn't this fail for API10? Or should I use void as return value and ask meethodUsesAPI11() to change a global variable?Hubey
I'm pretty sure this is just a lint annotation to supress warnings, not to conditionally compile.Youthful
E
2

Checking Build.VERSION.SDK_INT or using annotations should suffice, however, this link I'd bookmarked might 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 not be compatible, but will never be loaded. It's not conditional compilation, but it may be what you need, however, it is a bit more complex.

Erasion answered 14/11, 2012 at 14:4 Comment(1)
I read everything, and that also would do what I want. Thanks a lot!!Hubey

© 2022 - 2024 — McMap. All rights reserved.