12

Simple question: In Eclipse "New Android project" command always tries to uses last available sdk. Is it always a good idea?

UPDATE:

I try to explain. Now in Eclipse, "New project" produces a manifest with:

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

So, target to the last Android (Jelly Bean, 4.2.x) but it can run also on Froyo (2.2). It's ok for me. What could I do very wrong with this choice?

UPDATE

Is it ok if I set android:minSdkVersion="8" + android:targetSdkVersion="17" and the build target to 2.2 to be sure my app will run on older devices (no NoSuchMethodError exception)?

Seraphim's
  • 11,736
  • 17
  • 80
  • 126
  • It depends on what devices do you want to target – v0d1ch Feb 18 '13 at 14:45
  • 1
    Read this description on what "target sdk version" means: http://developer.android.com/guide/topics/manifest/uses-sdk-element.html For this it follows that you tested your app on this sdk version and verified it works ok. So device do not need to emulate older sdk for you. – tuxSlayer Jul 09 '14 at 09:16
  • @tuxSlayer Yes, thanks, a lot of time passed since I wrote this question. Now I always target the last sdk version – Seraphim's Jul 09 '14 at 09:37
  • 1
    I meant that probably you don't always want last version in that place. I.e. if you create new project (writing app from scratch) - this is probably the good way to start. Otherwise lets imagine your app accesses some device features and you tested it on device with API level 18. Then you see that API Level 20 is available and just change this target version. After this some features might not work on newer devices, however, if you leave target sdk on 18, new devices will emulate it for you and everything will work just fine. – tuxSlayer Jul 09 '14 at 13:23

3 Answers3

12

What could I do very wrong with this choice?

There is nothing wrong with this choice, which is why the build tools default to it.

Setting the targetSdkVersion opts you into specific behaviors that you might not get with an older targetSdkVersion. You can see some of what you get by reading the documentation for Build.VERSION_CODES.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • Previously, targetSdk was 8, and now (with 17) I get NetworkOnMainThreadException exception. Changing the target helped me to discover those "bad" lines. – Seraphim's Feb 18 '13 at 14:57
  • Is it ok if I set android:minSdkVersion="8" android:targetSdkVersion="17" and the build target to 2.2 to be sure my app will run on older devices? – Seraphim's Feb 18 '13 at 20:12
  • 1
    @Seraphim: I would not recommend that approach. Lint will tell you when there are classes and methods that you are using that exceed your `minSdkVersion`. With ~40% of Android devices running Android 3.0+, you should be starting to use capabilities that are newer than Android 2.2, just conditionally (e.g., via `Build.VERSION.SDK_INT`). In those cases, you should set your build target to be the version of Android that has all the classes and methods that are you trying to use. – CommonsWare Feb 18 '13 at 20:14
  • Really? I tried but with getfragmentmanager() and a Emulator with 2.2 no lint error/warning but in runtime I get an "NoSuchMethodError" exception. Do you think it's a lint problem? – Seraphim's Feb 18 '13 at 20:26
  • 1
    @Seraphim: Possibly. Make sure you are on a current version of the SDK tools (and the ADT plugin for Eclipse, if you use Eclipse). You may need to manually run Lint, though usually it automatically runs when you save the file. – CommonsWare Feb 18 '13 at 20:32
  • Please, could you make an example of a method that will surely produce a Lint warning in my case? – Seraphim's Feb 18 '13 at 20:47
  • @Seraphim: Since I do not know what your "case" is, I have no way to answer that. – CommonsWare Feb 18 '13 at 20:48
  • Ok, forget it. Now I understand what you've told me: minSdkVersion="8", targetSdkVersion="17" and the build target as max as I can "manage". Ok learned something interesting today. For example, using DreamService (API 17): If I keep the build target lower than 17 I get the Lint warning. – Seraphim's Feb 18 '13 at 21:02
4

If you want to develop applications for an older version, it is not a good idea. You can always check what the most used version is and make your application against that version. In some cases it is needed to use the latest version because some functionality is not yet implemented in an older version.

TimVK
  • 1,136
  • 6
  • 16
2

You can read more about it here: http://developer.android.com/guide/topics/manifest/uses-sdk-element.html

But you should almost always have target as the latest and then change minimum to the oldest you want.

unzoomed
  • 560
  • 1
  • 5
  • 22