1

Is it possible to use progressive enhancement when building Android apk's. I'd like to build an app which can be used on SDK 8 + (gingerbread is the dominant android version) and when possible add features from newer version.

I know I can get the SDK version with SDK_INT to do something conditional, however when I use features introduced in later versions, eclipse won't let me build saying I need to increase the min SDK level.
Maybe my web development background is what's causing my thinking this to be possible, it may just be fundamentally impossible, do popular apps have different versions for different SDKS (like min8-max10,min11-max15)? Is progressive enhancement in Android Java code possible?

Moak
  • 12,596
  • 27
  • 111
  • 166
  • "eclipse won't let me build saying I need to increase the min SDK level" -- that is a warning (yellow), not an error (red). – CommonsWare Jul 17 '12 at 13:32
  • in fact it's an error (red) `Call requires API level 11 (current min is 8): android.app.FragmentManager#findFragmentById` – Moak Jul 17 '12 at 13:37
  • Also, this is what I'm using to selectively enable the ActionBar in my application: http://shanetully.com/2011/10/android-3-0-actionbar-class-maintaining-compatibility-with-pre-android-3-0-apps – Kevin Coppock Jul 17 '12 at 13:44

3 Answers3

3

check if the build fails because of Lint errors, if so, and you're certain that a specific method won't be called on non-supported devices, add an annotation to that method with the min api level it can be called on as such:

@TargetApi(14)
public void useSomeNewApis() {
   ...
}

Or, if you're using eclipse, you can hover on the error line, and choose add @TargetApi(14) to useSomeNewApis

marmor
  • 27,641
  • 11
  • 107
  • 150
  • This is a great start, that this is a Lint error is great to know, now it's a question of making sure the functionality only get used when the version is correct – Moak Jul 17 '12 at 13:40
  • @Moak You already have the answer to that in your question: by checking against the SDK version with `SDK_INT`. – Kevin Coppock Jul 17 '12 at 13:42
0

Something like:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>

should be enough. But when program hit code from newer API than is on device then it will crash.

pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • I use this setting, however it does not let me use features from version 11. So my question is, is there a way to use a feature only if the right SDK is available, in other words progressively enhancing the app – Moak Jul 17 '12 at 13:21
  • 1
    It depends on what exactly you need. Look at this answer: http://stackoverflow.com/a/4955612/538169 – pawelzieba Jul 17 '12 at 13:24
0

You need to make sure you are using the correct version of android. If you want to use a feature available in API 15 you have to build it in that API.

So if the feature you want to use requires API 15 make sure that in your project properties you have Project Build Target set to that API.

Right click on the your project and go to Properties. Select the Android section and make sure the correct API is checked.

Zee
  • 673
  • 6
  • 7