2

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 ?

Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • 1
    Your question might be a duplicate of that question: http://stackoverflow.com/questions/3506792/android-api-version-programmatically – fabricemarcelin Mar 16 '12 at 19:52
  • 1
    What **specifically** are you trying to use an `#ifdef` for? As a deleted answer points out, there is no `#ifdef` in Java. Hence, unless you explain **specifically** what you are trying to accomplish, it will be difficult for anyone to give you advice. – CommonsWare Mar 16 '12 at 19:55
  • Your answer helps partly as for example in Android version 2.2 I cant call `String str = "aaaa"; str.isEmpty();` but in Android 4.0.3 I can so if I write this code it will give an error in 2.2, what can I do in this case ? – Viktor Apoyan Mar 16 '12 at 19:56
  • http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html . If you don't know how to use conditional statements in java. I assume you do, however, so elaborate on your question. – bbedward Mar 16 '12 at 19:57
  • Why -1 ? My question was incorrect ? – Viktor Apoyan Mar 16 '12 at 20:15
  • -1 was harsh, but what is 4.0.3 ? That is what is not compiling use SDK_INT and the static final integers in Build.VERSION_CODES http://developer.android.com/reference/android/os/Build.VERSION.html#SDK_INT – danmux Mar 16 '12 at 20:28
  • 4.0.3 is example code. This does not mean that code must work. This is just an example – Viktor Apoyan Mar 18 '12 at 08:38

4 Answers4

4

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
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Doh, beat me to it - voted to delete my answer (you can probably delete it immediately) – danmux Mar 16 '12 at 20:38
  • @CommonsWare So you want to say if I write something like this `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { String str = "foo"; boolean b = str.isEmpty(); } else { String str = "foo"; boolean b = str.length() == 0; }` This code will work both in android 2.2 and 4.0.3 – Viktor Apoyan Mar 17 '12 at 19:51
  • @ViToBrothersApoyan: Well, I don't know what the point is of the code in question. But, yes, that should work. Note that `isEmpty()` was added in API Level 9 (`Build.VERSION_CODES.GINGERBREAD`). – CommonsWare Mar 17 '12 at 20:41
  • I think the point here was using String.isEmpty() which doesn't exist before API level 9. http://developer.android.com/reference/java/lang/String.html#isEmpty() But I don't think that example will work, as the class won't be able to load on the old API level because of missing methods. You could extract the code for the new API level in a new class, which you only load for that API level. CommonsWare talks about this in his book. – d4n3 May 28 '13 at 07:57
1

You can simply check for the Api Level and call Build.VERSION.SDK_INT

fabricemarcelin
  • 1,719
  • 6
  • 25
  • 34
  • Build.VERSION.SDK is deprecated - better to use SDK_INT http://developer.android.com/reference/android/os/Build.VERSION.html#SDK – danmux Mar 16 '12 at 20:30
  • No worries. Folk love to bash Android for UI fragmentation, meh! Thats nothing compared to the API level changes and deprecated API's :) – danmux Mar 16 '12 at 20:39
1

#ifdef is for compile-time processing, and even if that were generally an accepted practice in Java (it's not, but 3rd party tools do exist to support the concept), it would not likely help you, since you need to make a runtime decision about the environment you're running in (unless you want to generate multiple packages?).

As the link fabricemarcelin provided (http://stackoverflow.com/questions/3506792/android-api-version-programmatically) states, you can determine the version of your runtime system easily enough. You'll need to write your application to the API you know will exist at runtime (the 2.2 system, if that's your lower limit), and you'll need to make a runtime decision about calling the 4.0 functions. For 4.0 functionality, you'll likely need to find your APIs using reflection.

I've made use of reflection to access things otherwise only available on newer Android systems and it's worked well. One tip though is to arrange to perform the reflection / API location early in your application if reasonable, and if not, at least arrange to only do it once per call.

mah
  • 39,056
  • 9
  • 76
  • 93
0

I found just recently that productFlavors is the way to do it. Using them you can create folders that are build just for the flavor thus conditionally compiling stuff. It's not the same but can solve similar situations.

Renetik
  • 5,887
  • 1
  • 47
  • 66