6

My question popped up a very similar question, this one. But the accepted answer (the single one) points to another question, this one, which doesn't really answer the original question.

The Android documentation states:

The Build Target specifies which Android platform you'd like your application built against.

But what does it mean really?

The way I see it, I can have the minSdkVersion=4 and targetSdkVersion=10 but set the build target to API Level 4. What will happen? Eclipse assumes I'm developing for API Level 4 and any method, constant or whatever defined on API Levels above 4 will not be available to me. If I try to use them, the application will not compile. I'm aware of this.

But let me put it differently...

Let's say I have only set minSdkVersion=4, targetSdkVersion is not defined. I am also not using any method or constant only available on API Levels above 4. In this situation, does it really matter the build target I pick? Will it have any impact in the final APK?

Community
  • 1
  • 1
rfgamaral
  • 16,546
  • 57
  • 163
  • 275

3 Answers3

6

Build target

Build target is the API level Eclipse/IntelliJ/whatever IDE you’re using is building against. This is simply used by the IDE/build system to know which APIs to offer you. If you build against API level 14, the application will still be able to run on API level 7, providing you don’t call any APIs that are not available on API level 7.

I mostly set the build target to the same as android:targetSdkVersion, though this is not required.

Source: http://simonvt.net/2012/02/07/what-api-level-should-i-target/

Community
  • 1
  • 1
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
2

If you use a higher build target then you can write code that will work on earlier versions by using reflection, for example. If you want to be restricted to just API 4 then don't worry about the build target.

For an example of targeting earlier api levels when compiling for a higher one you can look at this question:

Android: how to code depending on the version of the API?

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166
  • That still doesn't really answer my question. – rfgamaral Mar 27 '12 at 23:58
  • I mentioned that if you only want to be restricted to API 4 then no target is fine, but if you pick a build target that is higher and don't use any of the new features, it can potentially work on a higher API, using those functions, but since you aren't using them, it doesn't matter. I expect there is a difference in the byte code so that it can use a different API. – James Black Mar 28 '12 at 01:23
2

The way I see it, I can have the minSdkVersion=4 and targetSdkVersion=10 but set the build target to API Level 4. What will happen? Eclipse assumes I'm developing for API Level 4 and any method, constant or whatever defined on API Levels above 4 will not be available to me. If I try to use them, the application will not compile.

When you set the build target to API level 4, Eclipse will not let you compile any methods you use higher than that, because it strictly uses API level 4. However, when you put the build target to a higher API level, in your case API level 10, your APK is available for use for phones from API level 4 to 10.

The 2nd question's answer answers your question, that is the Android build target, both minSdkVersion and targetSdkVersion affects the range of users that are able to use your application.

EDIT:

Since you're not going to define targetSdkVersion and you're not using any features which is above API level 4, the targetSdkVersion will be the same as minSdkVersion. Whatever build target your chose will automatically be specified. It doesn't really matter which build target you pick unless it is below API level 4

From Android documentation of targetSdkVersion:

An integer designating the API Level that the application targets. If not set, the default value equals that given to minSdkVersion. This attribute informs the system that you have tested against the target version and the system should not enable any compatibility behaviors to maintain your app's forward-compatibility with the target version. The application is still able to run on older versions (down to minSdkVersion).

Zerhinne
  • 1,987
  • 2
  • 22
  • 43
  • 1
    None of that answers my question because my question is not about the manifest parameters, something everyone seems to focus on. I know what they do and how they work. That's not what I want to know. – rfgamaral Mar 28 '12 at 00:01
  • Steve Haley's answer fully states how it works and gave an example. The community thinks that it is the correct answer to the 2nd question, which is what I'm refering to. Sorry if I mislead you. – Zerhinne Mar 28 '12 at 00:03
  • 1
    Steve Haley's question fully states how the minSdk/targetSdk attributes work. But my question **is not** about those. – rfgamaral Mar 28 '12 at 00:07
  • I'm sorry but it still doesn't quite answer my question. You are still focusing on minSdk/targetSdk attributes. I'm fully aware of how those work. – rfgamaral Mar 28 '12 at 09:47
  • Then you should know already know the answer to your question, because the attributes determine which phones can your final APK file work on. – Zerhinne Mar 28 '12 at 10:04
  • 1
    Sure, I know that, but that's not my question. You still don't it and I don't know how else to put it. – rfgamaral Mar 28 '12 at 12:38
  • I'm fairly sure that my answers have fulfilled your questions but if that's not what you're looking for, you have to be more specific. – Zerhinne Mar 29 '12 at 02:43
  • 1
    No they haven't. I don't know how to be more specific, I've already told you countless times. **My question is not about the minSdkVersion/targetSdkVersion**, it's about the build target and what impact does it have on the final APK. How more specific do you want me to be? I'm sorry, but I don't know how else to put it... – rfgamaral Mar 29 '12 at 10:19
  • When you specify a build target during the creation of the project, it doesn't matter whether or not you've included the `targetSdkVersion` in the AndroidManifest.xml because Android will automatically do it for you (although it won't show in the xml). Otherwise, the system won't be able to detect which API level is your app built for. – Zerhinne Mar 30 '12 at 00:03
  • @androidnoob -> basically, i think Ricardo is asking more for subtleties... he wants to know what exactly it means to "build against an api version", as well as particularly the little nuances. For example, in my situation, I'm getting a strange error on some devices now, and all i changed was the build target. your answer (although correct) doesn't cover this situation at all. – David T. Aug 30 '13 at 01:34