Check api version programatically ICS doesnt work - android

I want to check if the android version is above ICS:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
I have a code error,
ICE_CREAM_SANDWICH cannot resolved or it is not a field
But I can read here
http://developer.android.com/reference/android/os/Build.VERSION_CODES.html
this is the correct way, where is the mistake?
Edit after Andy Res answer
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
But if I check Project->properties->Android, ICS doesn't appear, from Android 3.1 to Android 4.1. I suppose this is the error, how can I solve it?

But if I check Project->properties->Android, ICS doesn't appear, from Android 3.1 to Android 4.1. I suppose this is the error, how can I solve it?
Use the SDK Manager and download the SDK(s) that you want. They will then appear in the list for you to set your build target.

Simply use this
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 14){

Make sure the project build target is Android 4.0 or higher.
(Assuming you use Eclipse, you would do this by: Right clicking on the project -> Properties -> From the left menu click on Android -> then choose the target)

Related

Android Version Codes : new codes on older API? [duplicate]

Can I use Android version code constants with older Android runtimes? For instance,
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.HONEYCOMB) ...
would this run without crash on old devices running Android operating system before HONEYCOMB when this constant has been first defined? Assuming we compile it with recent enough SDK?
Yes, this will work.
The reason for this is that android.os.Build.VERSION_CODES.HONEYCOMB is an int. android.os.Build.VERSION_CODES.HONEYCOMB is just an alias (the int equals 11) for 11, as can be seen in an IDE such as Eclipse:
int android.os.Build.VERSION_CODES.HONEYCOMB = 11 [0xb]
So this will work as it'll just check if the android.os.Build.VERSION.SDK_INT is greater than or equal to 11.
Yes you can. It works because the int values are static final. The compiler will inline them into the bytecode at compile time. No symbol import is required at runtime.

Do Android version code constants work with older versions?

Can I use Android version code constants with older Android runtimes? For instance,
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.HONEYCOMB) ...
would this run without crash on old devices running Android operating system before HONEYCOMB when this constant has been first defined? Assuming we compile it with recent enough SDK?
Yes, this will work.
The reason for this is that android.os.Build.VERSION_CODES.HONEYCOMB is an int. android.os.Build.VERSION_CODES.HONEYCOMB is just an alias (the int equals 11) for 11, as can be seen in an IDE such as Eclipse:
int android.os.Build.VERSION_CODES.HONEYCOMB = 11 [0xb]
So this will work as it'll just check if the android.os.Build.VERSION.SDK_INT is greater than or equal to 11.
Yes you can. It works because the int values are static final. The compiler will inline them into the bytecode at compile time. No symbol import is required at runtime.

Lint and old API level warning

I compile against Android 4.2 (API 17), in my Manifest I have:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10"/>
In code I use:
String first = sdf.format(new Date(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime));
Field firstInstallTime was introduced in API 9.
Lint does not warn me, i.e. that this field is not valid in API 8. What am I missing, how should one detect this?
If I compile against Android 2.2 (API 8), I find the error and a bunch of extra errors due to new features used (> API 8) and the project won't compile.
(I'm aware of handling such things in runtime with for example Build.VERSION.SDK_INT)
What's the best way of working?
Why is lint not working?
Thanks!
This answer may be late but You should check your lint preferences
Right click on project -> Properties -> Android Lint Preferences
then search for min in the searchbox and select "UsesMinSDKAttributes"
Finally select "Include all" button. Hopefully the check had just been surpressed (Something I've had to do just to fix a silly lint error)
Good luck.
If it is intoduced in api level9 then try this.
< uses-sdk android:minSdkVersion="9" android:targetSdkVersion="10"/>
Probably it will works when the https://code.google.com/p/android/issues/detail?id=56427 will be solved

Android Compatibility Build Problems

BACKGROUND
I am developing an android application that needs to work on API levels from 7 to 16.
THE PROBLEM
Whenever I go to build my project this is the process I have to go through.
Clean project
Run Project
"Errors in project" > Click OK > Run Project Again
Runs fine on any API
I think the problem is due to the fact I am including code (such as the ActionBar) that API < 3.0 can't use but I am checking for it and running something else if thats the case.
THE QUESTION
Does anyone know a way round this because it is very time consuming considering I have to do this every time I want to run it.
I suppose you are using Eclipse for your development. You can annotate the offending methods as follows:
private final int VERSION = android.os.Build.VERSION.SDK_INT;
private File myDir;
// some stuff here
#SuppressLint("NewApi")
private void doSomething() {
if (VERSION < 8) {
// Uses a method available since API 1
myDir = Environment.getExternalStorageDirectory();
else {
// Uses a method available since API 8
myDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
}
// Do more stuff
}
You can add in your manifest:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="16" />
Then any API between and including 7 and 16 versions will compile because your target is 16.
Then if your device is 7 any API > 7 will fail on your device but you have said you are taking measures against that in your code.

Supporting one build for Android 3.0,4.0 and Android 2.2

I am facing a problem, I have to use one new API "BluetoothProfile" which is available for Android 3.0+. However if I use it I cant use my app anymore on Froyo or GingerBread.
Can someone suggest what are the options if I want to still support one build for all platforms.
build your application usng Android 3.0+ (API 11)
define minSdkVersion = 8 in AndroidManifest.xml
retrieve and compare device android version, and restrict application function.
In your code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
Log.i(TAG, "Your android is too old, please upgrade.")
} else{
Log.i(TAG, "meet the minimum system requirement")
}
check out the API Build.VERSION and Build.VERSION_CODES

Categories

Resources