Eclipse 中的 SDK 版本规范

SDK Version Specifications in Eclipse

我是一位使用 Eclipse 的 Android 经验丰富的开发人员,但有一件事仍然让我感到困惑。即SDK版本规范。特别是,我发现了几个指定 SDK 版本的地方:

  1. 项目属性:Android:项目构建目标
  2. 清单:minSdkVersion
  3. 清单:targetSdkVersion
  4. 清单:maxSdkVersion

以下是我对用法的猜测(可能是错误的):

项目属性:项目构建类型
好的,看起来很简单。我相信这指定了要使用的 API "library"。

清单:minSdkVersion
我的猜测是,这是检查所用方法的最低级别(如上所述)。好的,但似乎多余。如果我使用不受支持的方法,编译器不会对我咆哮吗?

清单:targetSdkVersion
想不通这一点。如果我只调用支持的方法,这个设置还有什么作用?

清单:maxSdkVersion
更加神秘。你为什么要限制最大版本?它们不是向后兼容的吗?

官方文档很少介绍这些设置的功能。谁能给我一些见解?

项目属性:项目构建类型 - 是的,这将选择 android.jar 以及构建工具版本。

Manifest: minSdkVersion - 这不仅用于编译目的,还被 google play 用来过滤应用程序。 其次,如果您有任何功能在特定版本以下不可用,但您的应用程序需要。

Manifest: targetSdkVersion - 随着 Android 随着每个新版本的发展,一些行为甚至外观可能会发生变化。但是,如果平台的 API 级别高于您应用的 targetSdkVersion 声明的版本,系统可能会启用兼容性行为以确保您的应用继续按照您期望的方式运行。您可以通过指定 targetSdkVersion 以匹配其 运行 平台的 API 级别来禁用此类兼容性行为。例如,将此值设置为“11”或更高允许系统在 Android 3.0 或更高版本上 运行 时将新的默认主题 (Holo) 应用到您的应用程序,并且当 运行 在更大的屏幕上(因为支持 API 级别 11 隐式支持更大的屏幕)。

清单:maxSdkVersion - 不推荐。这会将您的应用限制在高于指定 maxSdkVersion 的任何版本上。

在Android1.5、1.6、2.0 和2.0.1 中,系统在安装应用程序时以及系统更新后重新验证应用程序时会检查此属性的值。 但是,Android 的未来版本(超过 Android 2.0.1)将不再在安装或重新验证期间检查或强制执行 maxSdkVersion 属性

无需将该属性设置为阻止将您的应用程序部署到 Android 平台新版本发布的方式。按照设计,新版本的平台是完全向后兼容的。如果您的应用程序仅使用标准 APIs 并遵循开发最佳实践,那么您的应用程序应该可以在新版本上正常运行。其次,请注意,在某些情况下,声明该属性可能会导致您的应用程序在系统更新到更高 API 级别后从用户设备中删除。大多数可能安装您的应用程序的设备都会通过无线方式定期接收系统更新,因此您应该在设置此属性之前考虑它们对您的应用程序的影响。

https://developer.android.com/guide/topics/manifest/uses-sdk-element.html 包罗万象

Android:项目构建目标

当你select项目构建目标版本时,这意味着你是apk或类将根据selected SDK编译。例如 - 如果您 select 项目构建目标 16 并尝试使用注释 @JavaScriptInterface 它将找不到,因为该注释在该目标之上可用。

清单:minSdkVersion

如果您定义最低 SDK 版本,Android 使用低于指定最低 SDK 版本的用户将无法使用您的应用。

有关 google 文档的更多信息:

An integer designating the minimum API Level required for the application to run. The Android system will prevent the user from installing the application if the system's API Level is lower than the value specified in this attribute. You should always declare this attribute.

清单:targetSdkVersion

targetSdkVersion 与您的应用程序的编译方式或您可以使用的 API 无关。 targetSdkVersion 应该表示您已经在您指定的版本上(大概达到并包括)测试了您的应用程序。这更像是您给予 Android OS 的认证或签字,以提示它应如何根据 OS 功能处理您的应用程序。

例如,如文档所述:

For example, setting this value to "11" or higher allows the system to apply a new default theme (Holo) to your app when running on Android 3.0 or higher...

Android OS 在运行时可能会根据此值更改您的应用程式化或在 OS 上下文中执行的方式。还有其他一些受此值影响的已知示例,并且该列表可能只会随着时间的推移而增加。

出于所有实际目的,大多数应用程序都希望将 targetSdkVersion 设置为 API 的最新发布版本。这将确保您的应用在最新的 Android 设备上看起来尽可能好。如果不指定targetSdkVersion,则默认为minSdkVersion.

清单:maxSdkVersion

如果您定义 maxSdkVersion 用户使用的 android phone 运行 SDK 版本高于您在 maxSdkVersion 中定义的版本将无法使用你的应用程序。

有关 google 文档的更多信息:

An application declaring maxSdkVersion="5" in its manifest is published on Google Play. A user whose device is running Android 1.6 (API Level 4) downloads and installs the app. After a few weeks, the user receives an over-the-air system update to Android 2.0 (API Level 5). After the update is installed, the system checks the application's maxSdkVersion and successfully re-validates it. The application functions as normal. However, sometime later, the device receives another system update, this time to Android 2.0.1 (API Level 6). After the update, the system can no longer re-validate the application because the system's own API Level (6) is now higher than the maximum supported by the application (5). The system prevents the application from being visible to the user, in effect removing it from the device.