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.
Related
I'm curious about what is the sense in setting both target and minimal supported API version when starting a new Android Studio project. I mean, if I set that minimal API is, say, 8, then I won't be able to use features from 22 (which could be my target), because it would break compatibility with API 8.
if I set that minimal API is, say, 8, then I won't be able to use
features from 22
You can use API level >= 8 features in application, but you have to check OS version of the device first, see following code, that's how you can maintain compatibility
if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB){
//use features of API 3.0
} else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB_MR1){
//use features of API 3.1
} else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB_MR2){
//use features of API 3.2
}else{
// and so on....
}
Other then code, use can use resource folders on the basis of API level, like:
values-v11
values-v12
values-14
....
and
drawable-v11
drawable-v12
drawable-14
....
This is not correct. By setting target API to 8 you can't use any features added after, but specifying minimum API is just a guarantee that you application will work on such devices. You don't guarantee that it provide all features on such devices, nor minimum API level restricts what features it could use when running on later versions of OS.
Look at PE history - all win32 executable files are compatible with MSDOS, but all they say after executing is just "This is not a MSDOS program.". Similar to this, it is your decision what features you provide on which OS you support.
I am working with a large project, which has a minimum API level:16. however, I came across API usages that are above API level 16.
Is there any tool in Android studio or elsewhere, other than testing with a device, to check if the code doesn't violate the minimum required API level or better point it out like an error etc.?
Thank you.
The IDE will use the minimum android SDK, thus you will not get compile errors. If you there are classes in SDK 14 which are moved in sdk 16, yet you are using the imports from SDK 14, it will give a standard compile error.
So no, not that I am aware of.
You can use something like this:
public static boolean supports(final int version) {
return Build.VERSION.SDK_INT >= version;
}
Like this,
if (supports(Build.VERSION_CODES.HONEYCOMB)) {
// do something HONEYCOMB+ compatible here
}
More codes here,
http://developer.android.com/reference/android/os/Build.VERSION_CODES.html
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.
Question: What code should i use instead of
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){ //perform something }
Build.VERSION_CODES.JELLY_BEAN_MR1 error can not be resolved.
If compiled is set to Jelly Bean Mr1 and higher no errors. If compiler is set below Jelly Bean Mr1 errors occur.
minSdk = 9, targetSdk = 19, compiler = 2.3.1(Ginger Bread) see the below screen shot URL
https://www.evernote.com/shard/s283/sh/401b9ed5-d51d-4d55-b23d-6ebe8eeb8d03/212a791182b162df8dadb614445f2d2d
if Build.VERSION_CODES.JELLY_BEAN_MR1 gives a problem, only write:
import android.os.Build;
if you don't want to write it you can use
if( Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
{
//perform something
} else {
// perform other actions for other versions (below than jelly..)
// find another way to do what you want.
}
if the problem persist maybe you don't have the necessary libraries, please update your libraries with Android SDK Manager.
JELLY_BEAN_MR1 ~ Android 4.2
You should install and use Android 4.2 or higher as a compiler.
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