jph
jph

Reputation: 2233

how to support supporting older android versions in eclipse

My app need to target API level 7 and above, however I'd like to selectively use functionality from newer releases when it becomes available. For instance, I might have code like this:

if (Build.VERSION.SDK_INT >= 8) {
    base64String = android.util.Base64.encodeToString(bytes, android.util.Base64.DEFAULT);
} else {
    base64String = MyCustomBase64.encodeBytes(bytes);
}

However, when I configure the project in eclipse to target "Android 2.1" (API 7) the class android.util.Base64 isn't found since it only exists for API 8 and above. Eclipse then designates that line as a compile error (red "x") since the class isn't on the build path.

My question: what is the "proper" way to handle this sort of development (supporting features that don't actually exist in one's target environment) when doing Android development using Eclipse?

I can always change the target in Eclipse to 2.2 (API 8), but that's not ideal either. It creates the situation where I might unknowingly use a feature that only exists in 8+ and not be notified of that fact by Eclipse.

Upvotes: 2

Views: 915

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

You have answered your own question.

I can always change the target in Eclipse to 2.2 (API 8)

That is how it is done. You can still set the minSDK/targetSDK in your manifest to 7. AFAIK you'll just have to use the if statements like you've shown around any code that is from a higher API level.

Upvotes: 1

Related Questions