what is android #TargetApi annotations means - android

does TargetApi mean that code must be called under a specific version or
must be called greater than or equal specific version?for example
TargetApi(23) means that use for (23 and below ) or (23 and above)?

Target Api 23 means that the annotated method should run only on api 23+ devices. You should use it with IF check for api level of the device. If your app's minimum api level is lower than 23 consider providing a method for older devices.

From TargetApi docs:
Indicates that Lint should treat this type as targeting a given API level, no matter what the project target is.
It's used purely for the linter. Instead of using targetSdkVersion specified in your build.gradle, it will use this API.
When should you use this?
Perhaps when you're using a deprecated API, but you don't want the linter to continue warning you. You explicitly tell the linter, I know that this is deprecated but I don't care, I am using it as if I was on an older API level. You might (should) also add a comment nearby.

Related

Can reflection be used with methods that belong to greater than minimum sdk?

I am developing an app which has to support at minimum level 10 sdk. Today, I was coding when I came across a method setLayerType() which I need to use. But this method is introduced in API level 11. So, I used a workaround and used Reflection to use this method.
So, my question is, won't my application still crash on device which runs on API level 10?
This question came to my mind because, even though I am using Reflection here, still, I am calling a method that was introduced in API level 11. Will this method run successfully when I run my app on Android devices that run on API level 10 or will it crash?
As stated in source, the method will run independent of OS level. But, why should it run successfully on API level 10 device which is still using Android.jar of API level 10 and that file doesn't even contain this method?
I tried to search it but couldn't find an explanation.
P.S. I wanted to test it on a device with API level 10, but as building full app will take some time, so it is not possible to test app on API level 10 device right now.
No, of course the app will crash if you call a method that is not present in its API level. This is true whether you call by reflection or not.
The point of reflection is that you choose whether or not to call it based on the API level. Compare http://developer.android.com/reference/android/os/Build.VERSION.html#SDK_INT to http://developer.android.com/reference/android/os/Build.VERSION_CODES.html in your code and only proceed with the reflective call if the SDK number is high enough.
The point is that you isolate code with bytecode references to any method or constant or class that's not present in all API levels into a separate class that is only loaded by reflection.
Here's an example of some utility code I created just to manage this kind of thing; it loads one or the other implementation of an interface based on API level:
https://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/common/PlatformSupportManager.java?spec=svn2361&r=2361
EDIT: #FD_'s answer works too which surprises me. Reflection is safer, but much more code. And it seems like the reflection approach is in fact unnecessary on recent, if not all, Android JVMs.
You don't even need Reflection for that. Just specify your target sdk as high as possible, and your min sdk as low as it is now. Then, before calling a method that was introduced after the min version, just check which api version your app is running on:
if (android.os.Build.VERSION.SDK_INT >= 11) {
//Your code here
}
Trying to use methods that were introduced in a later api version will lead to a crash otherwise, whether you call it using Reflection or not.

Android: If an element is deprecated at a certain API level, is it only for that API level and above?

If an element was deprecated in API level 11, is it considered a deprecated element (not to be used if it can be helped) in API levels below 11 also or just 11 and above? If I am writing an app for API level 8, should I care about an element that was deprecated in API level 11?
This is slightly confusing to me. In Android API, for the SQLiteDatabase class for example, the method setLockingEnabled(boolean lockingEnabled) says:
This method was deprecated in API level 16. This method now does nothing. Do not use.
Does that mean it does nothing for all API levels below also or just API level 16 and above?
And if you are working on professional, commercial software, how important is it to avoid deprecated elements?
If an element was deprecated in API level 11, is it considered a deprecated element (not to be used if it can be helped) in API levels below 11 also or just 11 and above?
Just 11 and above.
If I am writing an app for API level 8, should I care about an element that was deprecated in API level 11?
"Deprecated" in Android usually means "we think that there is a better solution that you should consider, but we will support this old approach as best we can as long as we can". While you should endeavor to stop using deprecated methods, that's not something you should panic about and drop everything to address right away, and often times you need the deprecated materials for older Android OS levels, before they were deprecated.
Does that mean it does nothing for all API levels below also or just API level 16 and above?
Since they said "now does nothing", it is safe to say that it did something (non-nothing?) previously.
And if you are working on professional, commercial software, how important is it to avoid deprecated elements?
In the specific case of this method, I'd skip using it, though that's more due to the nature of this method.
As a counter-example, the way preference screens are set up differs substantially in API Level 11+ (PreferenceFragment and PreferenceActivity) versus before (PreferenceActivity alone). You can't avoid the deprecated element on older devices, simply because there is no viable alternative (e.g., backport of PreferenceFragment).
From the Docs
Annotation type used to mark program elements that should no longer be used by programmers. Compilers produce a warning if a deprecated program element is used.
I would suggest trying not to use them at all when possible. When you do I would take into consideration that this object, method, etc...may not work at some point in the future. When you build an app you typically want to build it so it can be easily maintained and flexible in the future. For this reason, I wouldn't use them if not necessary and there is almost always another way to do something
It's worth mentioning that sometimes a method may become "undeprecated" (if that is a real word). For example, the WebSettings setLayoutAlgorithm method was deprecated in 4.0.1, then later reinstated in 4.0.3 just with one of the algorithms deprecated.
But in general it's safe to use the method on versions of the API prior to when it was deprecated, and not recommended afterwards. Just be aware that it may not always be as simple as that.
When an item is deprecated that means that it should no longer be used after the API level in which it was deprecated. It is safe to use it for earlier API levels but if you are developing for newer API levels it should not be used. A deprecated method/element/etc. can be removed at a future point, so though it may still work now, you could end up with unwanted results in the future.
When it says that the method now does nothing that means that it no longer does anything in API 16 and above (or above 16 can't remember but to be safe I'll say 16+).

Dynamically choose api in mono for android

Is there any way to detect the current version of android and use one code path for one version, and another code path for a later version?
Something like ClipboardManager, there is a new version in API level 11, but the only way of accessing this new class is by telling mono for android that the minimum requirements is API level 11.
I "think" you can get around this using java reflection, but I imagine this wouldn't work when using Mono for Android. Has anyone got any advice on how you can use both ClipboardManager's, the early one on earlier versions, the later one on later versions, without setting the API level as 11 or higher.
This is a generic example, not limited to ClipboardManager. I am sure I will come across more classes that I could optionally use if it is available.
Sure there is. Just make sure to set your target API level in the manifest.
When wanting to use API 11 for instance you could do something like this:
if ((int)Build.VERSION.SdkInt >= 11)
{
//Execute API 11 and up code here
}
However if you are using methods and classes that are only present in API 11, VS might complain that it cannot find them, so you can surround them with a pre-processor symbol like:
#if __ANDROID_11__
// your API 11 and up code here
#endif

android - SDK 4.0 on android 2.1

Even though I have done some app on android, I am still confused. Is it possible to use functions in SDK 4.0, and run the app on android 2.1 or lower?
I tried methods you guys mentioned but got an error -
Field requires API level 11 (current min is 7): android.os.AsyncTask#THREAD_POOL_EXECUTOR, if I change min to 11, the app can't install on android 2.1, so even I can use higher API, but it still can't run on android lower version...how to fix that?
From Kzinch's advice, set TargetApi to 11, then it's working!
If you want a program that runs on both SDK4 and SDK 2.1 you have two possibilities. One is to provide alternative implementations on your code when they are needed i.e., if some function from SDK4 is not available in the SDK2.1 then you add a conditional block to your code that check the SDK version and provide code for each branch.
The other possibility is to use the Android Support Libaries in order to use the same code for both SDKs (no conditional blocks required). If you need a function provided by the SDK4 but not for the SDK2.1 you can check if that function is provided by a support library. If it is you can use it and your code will run fine on both SDK4 and SDK2.1 without requiring any version checking. For instance, if you need to use the LruCache class which is available since API level 12 (and so not available on SDK2.1) you can use the v4 support library which provide that function and works on SDK2.1 and SDK4. So in your code you would use
import android.support.v4.util.LruCache;
instead of
import android.util.LruCache;
Yes, you can use functions from the higher API in your code, but you must make sure they are never called on the lower API in runtime.
You should make checks for API level in runtime and provide alternative implementation that exists for that API level.
Let me provide some simple example:
SharedPreferences.Editor edit = PreferenceManager
.getDefaultSharedPreferences(getActivity()).edit();
edit.putInt(KEY, VALUE);
if (Build.VERSION.SDK_INT >= 9) {
edit.apply();
} else {
edit.commit();
}
apply() method is a faster (asynchronous) implementation of commit() method, but it not supported on the API level less than 9. With the help of API version check it all works perfect for all devices.
Update #TargetApi annotaion may be used to suppress Lint warnings/errors for API checks.
it doesn't matter what SDK level you compile your code against. Important is which methods/classes are you calling/instantiating.
If you use any newer classes or methods your code WILL crash running on older devices.
The suggested method to deal with it is Lazy Loading: http://android-developers.blogspot.co.uk/2010/07/how-to-have-your-cupcake-and-eat-it-too.html
and remember, I'm saying this about the SDK.
The compatibility pack is a library developed by google that you can add to any project and use the functions of the library without issues.
Furthermore, there're 3rd party libraries (such as the ActionBar Sherlock http://actionbarsherlock.com/ that aims to bring higher SDK level functionalities to lower SDK levels)
No. You cannot use methods from higher API, because the code to handle it is simply not present on lower version of API. You can, however target as high API version as possible, but you may take care to call these methods on right API. You can easily check that at runtime with. i.e.
f( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
// code to be run on Honeycomb and higher versions
}
If you are using the API which are specific to higher version, then the app wont work in older version.As those are not defined in the older version it will throw an error.That is the reason we restrict apps before uploading into market using minSDK in AndroidManifest.xml.

Higher API calls when lower SDK targeted

My app supports minSdkVersion=10 and targeting 16. I want to call methods specific to API level >= 14 if a specific device supports them. I could check running OS version at runtime and whether call or not higher API methods but when I specify min SDK version, methods that exist only in versions higher than 10 are not visible.
Is there any way to use higher API methods than minSdkVersion?
You can test the device's API with this:
if(android.os.Build.VERSION.SDK_INT >= 14) {
// Do something fancy
}
else {
// Do something regular
}
In addition of checking the current version you should also add #SuppressLint("NewApi")to your method so the compiler want yell about it.
Methods from higher API are invisible and inaccessible because project's target SDK is lower than SDK which methods are going to be used. For example: if you want to use methods from API 14 Android project target SDK should be at least 14 or even better the latest (currently 16). That is kind of obvious but I missed it. After that the solution Sam gave a reference to is in use.

Categories

Resources