6

我的问题弹出了一个非常相似的问题,this one。但是接受的答案(单一的)指向另一个问题,这个问题并没有真正回答原始问题。

Android 文档指出:

构建目标指定您希望构建应用程序的 Android 平台。

但这究竟意味着什么?

在我看来,我可以minSdkVersion=4targetSdkVersion=10构建目标设置为 API 级别 4。会发生什么?Eclipse 假设我正在为 API 级别 4 进行开发,并且任何方法、常量或任何在 API 级别高于 4 上定义的任何东西都对我不可用。如果我尝试使用它们,应用程序将无法编译。我知道这一点。

但让我换个说法……

假设我只有 set minSdkVersion=4targetSdkVersion 没有定义。我也没有使用任何方法或仅在 API 级别高于 4 时可用的常量。在这种情况下,我选择的构建目标真的很重要吗?它会对最终的APK有什么影响吗?

4

3 回答 3

6

构建目标

构建目标是 API 级别的 Eclipse/IntelliJ/您正在使用的任何 IDE 构建。IDE/构建系统仅使用它来了解要为您提供哪些 API。如果您针对 API 级别 14 构建,应用程序仍然能够在 API 级别 7 上运行,前提是您不调用任何 API 级别 7 上不可用的 API。

我主要将构建目标设置为与 android:targetSdkVersion 相同,但这不是必需的。

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

于 2012-04-08T13:59:07.327 回答
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).

于 2012-03-27T23:49:51.893 回答
2

例如,如果您使用更高的构建目标,那么您可以编写适用于早期版本的代码,例如使用反射。如果您想仅限于 API 4,那么不要担心构建目标。

有关在为更高级别编译时针对早期 api 级别的示例,您可以查看以下问题:

Android:如何根据 API 的版本进行编码?

于 2012-03-27T23:42:12.740 回答