If I specify the minimum SDK as 2.0 and the target SDK as 4.1,will I have to write explicit code to ensure backward compatibility. Like if I want to use a gesture detection feature introduced in SDK level 7 and I set the target as 7 and minimum to 3. I am asking will I need to write explicit code(which doesn't include the gesture detection features) so that it runs in targets less than 7 but greater than 3?
Yes, you will have to avoid calling future APIs when running on older versions of Android. You can organize your code to select an appropriate code path at runtime, depending on the version of your OS.
An example of how to preserve backwards-compatibility when using a new API.
Yes, you will need to, but I don't see any logical reason to still support any API before 8 (2.2).
It's literally 1.6% of the market. It's not worth the effort to maintain and support such early versions any more.
That said, if you need to do version specific code, this is the way to handle it:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
//Use API 7+ code
} else {
//Use backwards compatible code
}
You should check out the Android official site on how to guarantee backwards compatibility to minimum level 4 (which is negligible up until api level 7 anyway. The slightly more significant share is usually api level 8 upwards), and always use the latest support library.
Of course there are some minor code changes (such as getSupportFragmentManager() in replace of getFragmentManager(),etc). The worst you it can happen is NoSuchMethodException so you have to be real careful not to call API's that does not exist in lower platforms. The sdk should be very clearly advising that when it happens.
Related
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.
I would like to know whether android has supports for the older versions. There are 15 different levels for android currently and I wonder about the followings.
Can a project made on level 5 be used on the devices whose level higher than 5?
How can I find the detailed differences between all the levels. For example, android.widget.VideoView starts with which level?
Assume I made a project based on level 8 nowadays and after a while like 2 years, level 20 has been introduced by that time. And I would like to use one of the classes that belongs to the level 20. I guess there is no way for me to use that class without upgrading my project level to the 20. In this case, is it possible that level 20 doesn't accept some of the classes I used with level 8? If yes, what can be the solution? Can I download the jar file of level 20 and reference it in my project manually? If possible, does this mean that I don't have to upgrade my project level to 20 in order to use classes of level 20?
I am going to start a project in a few days. However, I didn't decide the level yet. I got confused the differences of levels.
Yes Until know all Levels are backward compatible and most likley this will continue a while.
http://developer.android.com/reference/packages.html -> Filter by API Level
Yes you have to upgrade. But you can set the compatibility to a level below that. Than you have to take care that none of the Methods of lvl 20 will be executed in lvl <=19. You can do this by determine the current Version while running your App Build.VERSION.SDK_INT gets you the API level.
Hope this helps:
1- Yes A project made on lower levels will work on higher levels.
2- In the page of the component. For example for android video view you can check in the top right corner SINCE: API LEVEL 1
3-You are right, if you want to use level 20 classes your project will have to be upgraded to level 20, but as far as I know there are no compatibility issues for higher versions. You should take note about the deprecated classes though. Avoid using them because they might not be available later.
1 - yes. Android devices support running code from older levels.
2 - look through the Android docs. It even lets you filter by API level.
3 - Higher levels always let you use the lower level classes. It's just not recommended.
To decide what to support, I looked at the current distribution dashboard to see what was really out there and using the market. I went with level 7 for what I was doing, but that's just me. Level 8 would also be a good place to work from.
Also, if you want to access the better parts of the API on supported devices, but still work on older versions of the API, there's advice in the answers to this question.
I am kind of new to Android world and have a simple question.
If I develop an app using libraries on API 13 (or API10) will this app still run on Android phones which have only API 7 installed?
When I try to run my current app with EClipse on my Android phone with API 7 I get an error message that my app requires at least API 10 - so I am afraid to develop on API 13.
Should I better just stick to API 7 and not use the new features?
Whats the best thing to do in this situation?
Many thanks
It will work if you are not using any function from API10 which is not present in the older APIs.
You can still use the new functions with reflection.
First check if the function is available, then execute it with reflection.
Example:
// try to override the transition animation
if (VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) {
try {
Method method = getClass().getMethod("overridePendingTransition",
new Class[]{int.class, int.class});
method.invoke(this, 0, 0);
} catch (Throwable e) {
// can not override transition animation, so do nothing
}
}
Also make sure you set android:minSdkVersion.
You generally want to develop using the minimum API version possible for the features your program requires. This will allow it to work on the most devices.
API 7 devices can run API 7 and lower.
The message you get is just a response to what is set in the manifest file. You can change the min API version for your application:
<manifest android:minSdkVersion="9"> </manifest>
As Heuristic said, you should use the minimal API version because of compatibility reasons, but if your application uses libraries from a newer version of the one installed on the phone, the app will not work.
If you have a true minimum API level you should develop for that level and with the appropriate minSDKVersion flag set so that your application won't even appear in the marketplace for devices that do not meet that standard. In general answer to your question, the minimum possible requirements will depend on what features of the API you take advantage of. If you want to provide backwards compatibility but still want to use some of the features in the newer API I think you're going to need to create two versions of your application and set the min/target/max SDK flags appropriately so customers get the version best suited to their device.
You can check out the Android API listing to see what the differences are between each level to determine if there's some functionality you'll really want to use.
http://developer.android.com/guide/appendix/api-levels.html
I currently have a application targeted at android 2.3 (api level 10), but went thinking that probably people using android 2.1/2.2 or older won't even be able to see the application in the market.
So I thought that using api level 3 would be the best to use, but I don't know if this will maybe make certain elements in my app work less good, and probably buggier, since it actually uses old android code. Is there a good way to find out which API level I should use, and also, how do I convert my application to that level?
You can find a breakdown of the different versions of Android in use here. Currently, if you target 2.1 (API 7) you'll only miss out on about 3% of the market. Targeting 2.2 will miss just under 20%, or a fifth of the market.
As for converting your app, you can check what API level things are available for in the Android Reference. You'll note a checkbox and dropdown menu on the top right of the browsing area that allows you to filter by API level - that's one way to help converting. The other is to set the target and minimum SDK versions which will help Eclipse know what to complain about in your code. The easiest way to see if something needs conversion, however, is to emulate and test.
EDIT: The API level dropdown moved from the top right to the top left, over the list of APIs.
This graph may help you make up your mind.
It is good to look at backward compatibility and in general, you should compile your application against the lowest possible version of the platform that your application can support.
You can determine the lowest possible platform version by compiling the application against successively lower build targets. After you determine the lowest version, you should create an AVD using the corresponding platform version (and API Level) and fully test your application. Make sure to declare a android:minSdkVersion attribute in the application's manifest and set its value to the API Level of the platform version. Good luck!
If you have
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="10"/>
then it will appear right down to 1.6, but make sure you don't use antyhing that they don't have in their API
edit: Don't bother with 3! :)
I started out developing for 1.5, but soon realized that only a small percentage of people still run 1.5. I generally develop at the 2.1 level, and leave it at that (unless you need features at a higher level). To change it, open your project properties, and while I don't have my environment open, there is an Android option where you will select what API level you want to target.
The Best API Level is contains follows
1) The best API covers 100% market but all are not prefect so our app should be covered at least 90% with all devices .
2)We have to care about backward compatibility issues and our code would adapt itself for upcoming versions.
3) Using the new Android SDK is a requirement to use any of the new APIs added in that level. It should be emphasized ... It is ultimately a business decision on whether supporting an additional 3% of devices is worth the development and testing .
4) check out this link https://developer.android.com/guide/practices/compatibility.html#defined
5) Finally choose the best API you will find best API
I have purchased an HTC Incredible and have dived into the world of android! Only to find myself totally confused about the API levels and backward compatibility.
My device runs the 2.1 OS, but I know that most of the devices out there run 1.5 or 1.6; and soon the 2.2 OS will be running on new devices. The SDK has gone through such enormous changes, that even constants have been renamed (from VIEW_ACTION to ACTION_VIEW for example). Methods have been added and removed (onPause replacing the earlier call, etc al).
So, If I want to write an application that will work from 1.6+, does that mean I have to install and write my code using the 1.6 API; then test on later versions? Or can I write using the 2.1 SDK and just set the minSDK level and not use "new" features?
I have never worked with an SDK that changes SO drastically from release to release! So I am not sure what to do....
I read through an article on the Android Development site(and this posting on stack overflow that references it: Should a legacy Android application be rebuilt using SDK 2.1?), but it was still not very clear to me.
Any help would be appreciated
The SDK has gone through such enormous
changes, that even constants have been
renamed (from VIEW_ACTION to
ACTION_VIEW for example). Methods have
been added and removed (onPause
replacing the earlier call, etc al).
Those were two years ago, on a beta version of the platform, before there were any shipping devices. Since Android 1.0, there has been very little that breaks forward compatibility, mostly in the area of settings that were moved into a secure API so SDK applications cannot mess with them.
So, If I want to write an application
that will work from 1.6+, does that
mean I have to install and write my
code using the 1.6 API; then test on
later versions? Or can I write using
the 2.1 SDK and just set the minSDK
level and not use "new" features?
You make it seem like those are mutually exclusive. In fact, they are largely identical.
Keep your toolset on the latest version of the Android development tools
Put the minSdkVersion in your manifest to state what is the lowest API level you want to support
Put the targetSdkVersion in your manifest to state what your "target" API level is, so Android can apply some compatibility helpers if your app runs on a newer version of Android (typically, you "target" the then-current API level)
Write your code mostly to the API level you specified in minSdkVersion, optionally using reflection or conditional class loading to access newer APIs on devices that support them
Test on everything you can get your hands on, at least emulators for the different API levels
You can use the current SDK and set minSDK level to whatever level you want. If you do this then you cannot use any functionality that is not in the minSDK. It is also a good idea though to test it on all versions of the SDK with the emulator.
<uses-sdk minSDK="4" targetSDK="8"/>
That lets it know that you are targeting 2.2 but the minimum SDK level you want your app to run on is 1.6. By doing that you can use some of the new xml stuff in the newer versions like supports-screen and different drawables for different screens, etc.