Entropy1024
Entropy1024

Reputation: 7921

Making an Android 2.2 app work on 1.6

I have written a simple app for Android and when I first created the project I selected v2.2 for the build target. I have found out that my app does not work on v1.6, it runs for a few seconds then crashes. So I guess I am using some commands that were not implemented in 1.6. So I need to find out what these commands are and find the 1.6 equivalent. Should not be too hard I guess.

My real question is; to make a 1.6v is the only way to do this to create a new project and set the build target to 1.6 then copy and paste the source code and re-compile? Or is there an easier way? ie can I take my existing 2.2 build and then tell it to compile to 1.6?

Many thanks for the help.

Upvotes: 1

Views: 1407

Answers (2)

Reuben Scratton
Reuben Scratton

Reputation: 38707

The rule is that you always build with the latest SDK, but use the android:minSdkVersion value of the <uses-sdk> attribute to indicate the lowest API level your app can support.

Note that this attribute is only a clue to the installer and the Market... setting it to 4 (for 1.6) won't mean your app can magically run on 1.6. It's up to you to devise nice fallbacks if more modern APIs are unavailable (or raise your minSdkVersion appropriately if the app cant function without them).

Assuming your app can run on 1.6 without 2.x APIs, then you need to be sure that your 2.x API usage is done from a class that won't get loaded on 2.x. Here's an example:

import android.view.Display;

// Class for wrapping APIs only available in 
// API level 8 and up. (i.e. 2.2, a.k.a. Froyo)    
class ApiLevel8Only {
    public static int getDisplayRotation(Display display) {
        return display.getRotation();
    }

}

That getRotation() call will cause a force-close on 1.6. To avoid this you inspect the current version before your API-wrapping class is referenced:

if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
    ApiLevel8Only.getDisplayRotation(...);
}

Hope that makes sense.

Upvotes: 1

Bryan Denny
Bryan Denny

Reputation: 27596

All you have to do is set the minsdk value in your manifest. This sets 1.5 as your minsdk, and 1.6 as your target, for example:

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="4"/>.

Versioning your app.

android:minSdkVersion — The minimum version of the Android platform on which the application will run, specified by the platform's API Level identifier.

So you set this to the API # for 1.5 or 1.6 and you're app will run on those older versions of Android. But you MUST check for API calls to newer version of Android. You can't use 2.2 features in 1.5, etc.

So you can set your target to Android 2.2, and have your minsdk set to Android 1.5. You can then use Build.VERSION to determine what version of Android the user is using to avoid using newer API calls in older versions of Android.

Upvotes: 1

Related Questions