minSdkVersion="5"/>
...
这样做的主要原因是,应用程序会通过android:
miniSdkVersion声明的API级别来告知系统——该应用程序使用的API是指定引入的API级别。
假如没有这种属性声明,一个应用程序如果因为某些不知名的原因被安装在低级别API系统之上,那么该应用程序会在运行时因为尝试访问不存在的API而崩溃。
为此,Android系统通过不允许应用程序被安装在不满足其最低API级别要求的目标硬件上来防止此类结果发生。
例如,android.appwidget类包是在API级别3中开始引入的。
如果一个应用程序使用了这个API,就必须通过指明android:
minSdkVersion属性为3来声明运行的最低要求。
于是,该应用程序就可以在Android1.5(API级别3级)、Android1.6(API级别4级)等平台上安装,但是在Android1.1(API级别2级)和 Android1.0 平台(API级别1级)上却是无法安装的。
欲知关于如何声明应用程序API级别需求的更多信息,请阅读文档中关于清单文件的章节。
开发者需要考虑的内容
本节属于应用程序开发者需要了解的内容。
应用程序的向前兼容性
Android应用程序一般向前兼容于Android平台的新版本。
这是因为几乎所有的框架API变更都是添加性质的,一个以某种版本的API开发的应用程序是向前兼容与后续Android系统及高版本API。
应用程序应当可以运行于高版本的平台,除非使用了未来因某种原因移除的API。
向前兼容性的特性如此重要,是因为很多的Android驱动的设备能够使用OTA远程下载技术进行系统更新。
用户开始时能够很好的安装和使用你开发的应用程序,后来进行了OTA升级到新版本的Android平台。
一旦升级完毕,你的应用程序将运行在一个新的版本环境下,而这个环境应当包含了你的应用程序所需的API和系统功能。
某些情况下,API背后的变化,例如系统底层的变化,可能会影响到你的应用程序运行在新的环境下。
为此,作为应用程序开发人员,应当了解应用程序在不同系统环境下将会有如何的外观和行为。
AndroidSDK包含了多个平台版本,开发人员可以下载下来,用于测试自己的应用程序在不同版本下的运行情况。
每个版本的平台都提供了兼容的系统镜像,你可以将应用程序运行在对应的Android虚拟设备(模拟器)上进行测试。
应用程序的向后兼容性
Android应用程序不一定向后兼容于那些比这些应用程序开发环境低的平台环境。
Android平台的每个新版本包含了新的框架API,例如新的平台功能,或者替代部分已有的API。
应用程序在新平台上可以访问这些新的API,就像之前提到的,在后续更新的版本中,这些新功能API也是可以继续使用。
相反的,因为早期版本的平台不会包含新的API功能,使用了新API的应用程序在那些旧平台上是无法运行的。
虽然一个Android平台设备不太可能降级到以前的版本,但是需要了解的是:
实际生活中可能会有很多设备运行的是较旧的平台版本。
即使在设备接收到OTA远程下载升级,有些人仍然可能会滞后甚至可能不会收到更新。
平台版本和API级别的选择
当你开发应用程序时,你需要选择一个平台用于编译该应用程序。
通常,你应该在你的应用程序可以支持的最低版本的平台上进行编译。
你可以通过寻找最低的可以支持你的应用程序编译的平台方式来决定你将会支持的最低平台版本。
一旦你确定了支持的最低版本后,你就可以创建对应版本的AVD模拟器来全面测试你的应用程序。
你需要注意的是不要忘记在应用程序清单文件中申明android:
minSdkVersion属性,并赋上正确的API级别标识。
声明最低API级别
如果你开发的应用程序使用了最新平台上引入的API或平台特性,你就需要将android:
minSdkVersion属性赋最新平台的API级别标识。
这将保证用户只能在他们的设备与你的应用程序平台版本兼容情况下安装。
反过来说,这样能够保证你的应用程序在他们的设备上表现正常。
如果你用了最新的API或平台特性,但是没有申明android:
minSdkVersion属性,那么你的应用程序在最新平台上运行是没有稳定,但是在早期版本的平台上是会出错的。
在后一种情况,应用程序在尝试访问旧平台上不存在的API时会崩溃。
针对高版本的API级别测试
编译完你的应用程序,你应当在支持的最低版本平台上详细测试应用程序的表现。
可以通过创建对应平台的AVD模拟器进行测试。
此外,要保证向前兼容性,你应当在你的应用程序可能运行的更高平台版本上进行运行和测试。
AndroidSDK包含了多个平台版本供开发者使用,其中包括了最新的版本,并且提供了升级工具使得开发者可以获取其他的版本。
要使用升级工具,可以通过Android命令行方式,该工具位于/tools路径下。
你可以用敲入android指令运行该升级工具,而不用指定任何参数。
你也可以简单地通过双击Windows下的android.bat或OSX/Linux下的android文件来启动升级工具。
在ADT工具界面,你可以通过菜单的Window > AndroidSDKandAVDManager来访问升级工具。
要想将你的应用程序在不同平台版本的模拟器上运行,需要创建对应的AVD设备。
关于AVD的更多信息,请参见CreatingandManagingVirtualDevices。
如果你用物理设备进行测试,确保清楚运行的具体平台版本。
通过本文开始部分的映射表格可以知道平台版本对应的API级别标识。
使用临时的API级别
某些情况下,有可能会发布“早期预览(EarlyLook)”版的Android 平台SDK。
在此平台版本上开发使用的API将不会是最终发布版,平台的API级别标识数字就不会指定。
你必须在程序清单文件中使用临时的API级别进行标识,以便建立对应的平台应用。
临时API级别不是一个整数,而是一个表示未发布的平台版本的字符串代号。
“早期预览”版本的API级别在发布说明中会申明,该代号是大小写敏感的。
临时API级别标识的用途是保护开发者和设备使用者免于无意中发布或安装在“早期预览”平台版本上开发的应用程序,这些应用程序可能在最终的发布系统上运行不稳定。
临时API级别标识仅仅在使用“早期预览”版本的SDK下有效,并且只能运行应用程序于模拟器之中。
实际的Android设备上是无法安装使用临时API级别标识的应用程序的。
在最终版本平台发布后,你必须将程序清单文件中用到的临时API级别标识替换成最终平台的实际API级别整数值。
通过API级别进行文档内容过滤
Android开发者站点上的参考文档的每个页面的右上方提供了“FilterbyAPILevel”控件。
你可以用这个控件来仅仅显示你应用程序相关的那部分API文档,可以通过应用程序清单文件中的android:
minSdkVersion属性值进行过滤。
要使用过滤功能,可以通过页面搜索框下面的选择框来启用过滤功能。
比如设定“FilterbyAPILevel”控件内容与你应用程序申明的相同。
注意:
在选定级别之后引入的心API会以灰色显示,并且内容被隐藏,因为你的应用程序是无法访问这些新功能。
API级别过滤方法不会提供每个API级别中新添加的功能视图,仅仅展现对应API级别的整个内容,其中去除了后续新版本中的内容。
如果你不想过滤API文档了,只需要关闭该选择框的特性。
API级别过滤默认是关闭的,因此你可以看到整个框架API的全貌,而不管具体的API级别。
还要注意的是个别的API元素申明了该API参考文档在哪一级引入的。
这通过在每个文档页面正文部分的右上角的“Since”进行声明该包和类的引入信息。
类成员的API级别在其详细描述文件头部的右边进行描述。
AndroidAPILevels
AsyoudevelopyourapplicationonAndroid,it'susefultounderstandtheplatform'sgeneralapproachtoAPIchangemanagement.It'salsoimportanttounderstandtheAPILevelidentifierandtheroleitplaysinensuringyourapplication'scompatibilitywithdevicesonwhichitmaybeinstalled.
ThesectionsbelowprovideinformationaboutAPILevelandhowitaffectsyourapplications.
Forinformationabouthowtousethe"FilterbyAPILevel"controlavailableintheAPIreferencedocumentation,see Filteringthedocumentation attheendofthisdocument.
WhatisAPILevel?
APILevelisanintegervaluethatuniquelyidentifiestheframeworkAPIrevisionofferedbyaversionoftheAndroidplatform.
TheAndroidplatformprovidesaframeworkAPIthatapplicationscanusetointeractwiththeunderlyingAndroidsystem.TheframeworkAPIconsistsof:
∙Acoresetofpackagesandclasses
∙AsetofXMLelementsandattributesfordeclaringamanifestfile
∙AsetofXMLelementsandattributesfordeclaringandaccessingresources
∙AsetofIntents
∙Asetofpermissionsthatapplicationscanrequest,aswellaspermissionenforcementsincludedinthesystem
EachsuccessiveversionoftheAndroidplatformcanincludeupdatestotheAndroidapplicationframeworkAPIthatitdelivers.
UpdatestotheframeworkAPIaredesignedsothatthenewAPIremainscompatiblewithearlierversionsoftheAPI.Thatis,mostchangesintheAPIareadditiveandintroduceneworreplacementfunctionality.AspartsoftheAPIareupgraded,theolderreplacedpartsaredeprecatedbutarenotremoved,sothatexistingapplicationscanstillusethem.Inaverysmallnumberofcases,partsoftheAPImaybemodifiedorremoved,althoughtypicallysuchchangesareonlyneededtoensureAPIrobustnessandapplicationorsystemsecurity.AllotherAPIpartsfromearlierrevisionsarecarriedforwardwithoutmodification.
TheframeworkAPIthatanAndroidplatformdeliversisspecifiedusinganintegeridentifiercalled"APILevel".EachAndroidplatformversionsupportsexactlyoneAPILevel,althoughsupportisimplicitforallearlierAPILevels(downtoAPILevel1).TheinitialreleaseoftheAndroidplatformprovidedAPILevel1andsubsequentreleaseshaveincrementedtheAPILevel.
ThefollowingtablespecifiestheAPILevelsupportedbyeachversionoftheAndroidplatform.
PlatformVersion
APILevel
Android3.0
11
Android2.3.3
10
Android2.3
9
Android2.2
8
Android2.1
7
Android2.0.1
6
Android2.0
5
Android1.6
4
Android1.5
3
Android1.1
2
Android1.0
1
UsesofAPILevelinAndroid
TheAPILevelidentifierservesakeyroleinensuringthebestpossibleexperienceforusersandapplicationdevelopers:
∙ItletstheAndroidplatformdescribethemaximumframeworkAPIrevisionthatitsupports
∙ItletsapplicationsdescribetheframeworkAPIrevisionthattheyrequire
∙Itletsthesystemnegotiatetheinstallationofapplicationsontheuser'sdevice,suchthatversion-incompatibleapplicationsarenotinstalled.
EachAndroidplatformversionstoresitsAPILevelidentifierinternally,intheAndroidsystemitself.
ApplicationscanuseamanifestelementprovidedbytheframeworkAPI— —todescribetheminimumandmaximumAPILevelsunderwhichtheyareabletorun,aswellasthepreferredAPILevelthattheyaredesignedtosupport.Theelementoffersthreekeyattributes:
∙android:
minSdkVersion —SpecifiestheminimumAPILevelonwhichtheapplicationisabletorun.Thedefaultvalueis"1".
∙android:
targetSdkVersion —SpecifiestheAPILevelonwhichtheapplicationisdesignedtorun.Insomecases,thisallowstheapplicationtousemanifestelementsorbehaviorsdefinedinthetargetAPILevel,ratherthanbeingrestrictedtousingonlythosedefinedfortheminimumAPILevel.
∙android:
maxSdkVersion —SpecifiesthemaximumAPILevelonwhichtheapplicationisabletorun. Important:
Pleasereadthe documentationbeforeusingthisattribute.
Forexample,tospecifytheminimumsystemAPILevelthatanapplicationrequiresinordertorun,theapplicationwouldincludeinitsmanifesta elementwitha android:
minSdkVersion attribute.Thevalueof android:
minSdkVersion wouldbetheintegercorrespondingtotheAPILevelofth