Search code examples
javaandroidconditional-compilation

#ifdef in Android


I am writing an application for Android. My application must be launched under Android 2.2 and Android 4.0.3, and I want to use something like #ifdef in Android but I still can't find the way to do that.

Code example

Android 4.0.3

if( android.os.Build.VERSION.SDK == 4.0.3)
{
    String str = "foo";
    boolean b = str.isEmpty();
}

Android 2.2

else {
    String str = "foo";
    boolean b = str.length() == 0;
}

I can't write code like this as the compiler will give an error.

What you can suggest me to do ?


Solution

  • What you can suggest me to do ?

    Newcomers to Java should spend time learning Java before starting in on Android development. 4.0.3 is not a valid integer in Java.

    Instead, you should be using:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
     // do something
    }