0

very new to Android programming. Despite of all my search I couldn't find how to write SDK specified code blocks in Android Studio.

For example, as far as I learnt so far there are different types of Notifications depending on the target SDK version.

I want to keep the minSDKversion as low as possible (9 for my case) but I also want to create 3 different functions for the higher versions of SDK in order to support modernest type notifications like this:

createNotificationForSKD9(String msg) {
    //code to show older-type notification for API level 9
}
createNotificationForSKD16(String msg) {
    //code to show notification for API level 16
}
createNotificationForSKD21(String msg) {
    //code to show newer-type notification for API level 21
}

But when I do this, Android Studio gives compile errors because my minSDKlevel has been set to 9 but I wrote some code for the SDK version above 9.

So, what is the workaround for this ?

Thanks by now.

Roni Tovi
  • 828
  • 10
  • 21

2 Answers2

4

Just check Build.VERSION.SDK_INT which provides version of the API on the device:

if (Build.VERSION.SDK_INT >= 21) {
   //code to show newer-type notification for API level 21+
} else if (Build.VERSION.SDK_INT >= 19) {
   //code to show newer-type notification for API level 19+
} else if {Build.VERSION.SDK_INT >= 9) {
   //code to show newer-type notification for API level 9+
} else {
   //code for api lower than 9
}

And instead of 9, 19, 21 I'd use version codes, for better readability:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   ...
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
   ...
} else if {Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
   ...
} else {
   //code for api lower than 9
}

EDIT

this is where I get compile errors from Android Studio because my minSDKversion is set to 9 but I wrote code against higher API level

What you most likely take as compilation errors is in fact Lint (tool scanning your code for potential issues) output (see docs about Lint). But it is not strictly compilation error and the reason your build process fails here is because it's configured by default to that (which can be changed using gradle file - see below).

To make Lint happy add @TargetApi(NN) annotation, where NN stands for API version you are targeting the code for. That would tell Lint you know there's a mismatch but you are doing it on purpose:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void functionForLollipop() {
   ..
}

If you remove the annotation, lint uses the manifest min SDK API level setting instead when checking the code, so that's why it complained.

And to make Lint not abort the build add:

lintOptions {
    abortOnError false
}

to your build.gradle (in android block).

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • this is where I get compile errors from Android Studio because my minSDKversion is set to 9 but I wrote code against higher API level – Roni Tovi Apr 25 '16 at 18:03
  • You must compile against highest **supported** API version, so even you support API9, your target (`compileSdk`) must be API21 otherwise no API21 related code will compile. If you got that set correctly, you will not get any compilation errors relatede to the above snippet. – Marcin Orlowski Apr 25 '16 at 18:09
  • My compiledSdkversion is already 23 but the compiler gives error message and stops compiling due to minSdkversion limitation – Roni Tovi Apr 25 '16 at 18:12
  • thank you, the @TargetApi(NN) directive is surely what I was looking for. – Roni Tovi Apr 25 '16 at 18:16
0

What you want to look in to is the @TargetApi(int) annotation which you put just before each method. What this annotation does is tell Android Studio that this method is built for API greater than or equal to the API version submitted. This does not stop you from calling these methods nor does it prevent a crash if you call one at a lower api level than is supported. It only produces a warning in the IDE that says "Are you sure you want to call this without checking?"

So a setup would be something like this:

public void createNotification(String msg) {
   if(Build.VERSION.SDK_INT >= 21) {
      createNotificationForSDK21(msg);
   } else if (Build.VERSION.SDK_INT >= 16) {
      createNotificationForSDK16(msg);
   } else if {Build.VERSION.SDK_INT >= 9) {
      createNotificationForSDK9(msg);
   } else {
      // not supported
   }
}

@TargetApi(9)
public void createNotificationForSDK9(String msg) {
    //code to show older-type notification for API level 9
}

@TargetApi(16)
public void createNotificationForSDK16(String msg) {
    //code to show notification for API level 16
}

@TargetApi(21)
public void createNotificationForSDK21(String msg) {
    //code to show newer-type notification for API level 21
}
DeeV
  • 35,865
  • 9
  • 108
  • 95