全国统一24小时服务热线:400-0000-889400-0000-889  / 13999887799

架构说

当前位置:主页 > 架构说 >

Android 官方文档:(二)应用清单 —— 2.27 <uses-sdk>标签

发布时间:2019-01-28 18:24 作者:AG88 来源:AG集团 点击: 字号:

syntax:
<uses-sdk android:minSdkVersion="integer"
          android:targetSdkVersion="integer"
          android:maxSdkVersion="integer" />
contained in:
<manifest>
description:

Lets you express an application's compatibility with one or more versions of the Android platform,by means of an 鼎盛娱乐API Level integer. The API Level expressed by an application will be compared to theAPI Level of a given Android system, which may vary among different Android devices.

Despite its name, this element is used to specify the API Level, notthe version number of the SDK (software development kit) or Android platform.The API Level is always a single integer. You cannot derive the API Level fromits associated Android version number (for example, it is not the same as themajor version or the sum of the major and minor versions).

Also read the document aboutVersioning Your Applications.

attributes:
android:minSdkVersion
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.

Caution: If you do not declare this attribute, the system assumes a default value of "1", which indicates that your application is compatible with all versions of Android. If your application is not compatible with all versions (for instance, it uses APIs introduced in API Level 3) and you have not declared the proper minSdkVersion, then when installed on a system with an API Level less than 3, the application will crash during runtime when attempting to access the unavailable APIs. For this reason, be certain to declare the appropriate API Level in the minSdkVersion attribute.

android:targetSdkVersion
An integer designating the API Level that the application targets. If not set, the defaultvalue equals that given to minSdkVersion.

This attribute informs the system that you have tested against the target version and thesystem should not enable any compatibility behaviors to maintain your app's forward-compatibilitywith the target version. The application is still able to run on older versions (down to minSdkVersion).

As Android evolves with each new version, some behaviors and even appearances might change.However, if the API level of the platform is higher than the version declared by your app's targetSdkVersion, the system may enable compatibility behaviors to ensure that your appcontinues to work the way you expect. You can disable such compatibilitybehaviors by specifying targetSdkVersion to match the APIlevel of the platform on which it's running. For example, setting this value to "11" or higherallows the system to apply a new default theme (Holo) to your app when running on Android 3.0 orhigher and also disables screencompatibility mode when running on larger screens (because support for API level 11 implicitlysupports larger screens).

There are many compatibility behaviors that the system may enable based on the value you setfor this attribute. Several of these behaviors are described by the corresponding platform versionsin the Build.VERSION_CODES reference.

To maintain your application along with each Android release, you should increasethe value of this attribute to match the latest API level, then thoroughly test your application onthe corresponding platform version.

Introduced in: API Level 4

android:maxSdkVersion
An integer designating the maximum API Level on which the application is designed to run.

In Android 1.5, 1.6, 2.0, and 2.0.1, the system checks the value of this attribute when installing an application and when re-validating the application after a system update. In either case, if the application's maxSdkVersion attribute is lower than the API Level used by the system itself, then the system will not allow the application to be installed. In the case of re-validation after system update, this effectively removes your application from the device.

To illustrate how this attribute can affect your application after system updates, consider the following example:

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, some time 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.

Warning: Declaring this attribute is not recommended. First, there is no need to set the attribute as means of blocking deployment of your application onto new versions of the Android platform as they are released. By design, new versions of the platform are fully backward-compatible. Your application should work properly on new versions, provided it uses only standard APIs and follows development best practices. Second, note that in some cases, declaring the attribute can result in your application being removed from users' devices after a system update to a higher API Level. Most devices on which your application is likely to be installed will receive periodic system updates over the air, so you should consider their effect on your application before setting this attribute.

Introduced in: API Level 4

Future versions of Android (beyond Android 2.0.1) will nolonger check or enforce the maxSdkVersion attribute duringinstallation or re-validation. Google Play will continue to use the attributeas a filter, however, when presenting users with applications available fordownload.
introduced in:
API Level 1
What is API Level?

API Level is an integer value that uniquely identifies the framework APIrevision offered by a version of the Android platform.

The Android platform provides a framework API that applications can use tointeract with the underlying Android system. The framework API consists of:

  • A core set of packages and classes
  • A set of XML elements and attributes for declaring a manifest file
  • A set of XML elements and attributes for declaring and accessing resources
  • A set of Intents
  • A set of permissions that applications can request, as well as permissionenforcements included in the system

Each successive version of the Android platform can include updates to theAndroid application framework API that it delivers.

Updates to the framework API are designed so that the new API remainscompatible with earlier versions of the API. That is, most changes in the APIare additive and introduce new or replacement functionality. As parts of the APIare upgraded, the older replaced parts are deprecated but are not removed, sothat existing applications can still use them. In a very small number of cases,parts of the API may be modified or removed, although typically such changes areonly needed to ensure API robustness and application or system security. Allother API parts from earlier revisions are carried forward withoutmodification.

The framework API that an Android platform delivers is specified using aninteger identifier called "API Level". Each Android platform version supportsexactly one API Level, although support is implicit for all earlier API Levels(down to API Level 1). The initial release of the Android platform providedAPI Level 1 and subsequent releases have incremented the API Level.

The table below specifies the API Level supported by each version of theAndroid platform. For information about the relative numbers of devices thatare running each version, see the PlatformVersions dashboards page.

Platform VersionAPI LevelVERSION_CODENotes
Android 4.4 19 KITKAT PlatformHighlights
Android 4.3 18 JELLY_BEAN_MR2 PlatformHighlights
Android 4.2, 4.2.2 17 JELLY_BEAN_MR1 PlatformHighlights
Android 4.1, 4.1.1 16 JELLY_BEAN PlatformHighlights
Android 4.0.3, 4.0.4 15 ICE_CREAM_SANDWICH_MR1 PlatformHighlights
Android 4.0, 4.0.1, 4.0.2 14 ICE_CREAM_SANDWICH
Android 3.2 13 HONEYCOMB_MR2  
Android 3.1.x 12 HONEYCOMB_MR1 Platform Highlights
Android 3.0.x 11 HONEYCOMB Platform Highlights
Android 2.3.4
Android 2.3.3
10 GINGERBREAD_MR1 PlatformHighlights
Android 2.3.2
Android 2.3.1
Android 2.3
9 GINGERBREAD
Android 2.2.x 8 FROYO Platform Highlights
Android 2.1.x 7 ECLAIR_MR1 PlatformHighlights
Android 2.0.1 6 ECLAIR_0_1
Android 2.0 5 ECLAIR
Android 1.6 4 DONUT Platform Highlights
Android 1.5 3 CUPCAKE Platform Highlights
Android 1.1 2 BASE_1_1 
Android 1.0 1 BASE  
Uses of API Level in Android

The API Level identifier serves a key role in ensuring the best possibleexperience for users and application developers:

  • It lets the Android platform describe the maximum framework API revisionthat it supports
  • It lets applications describe the framework API revision that theyrequire
  • It lets the system negotiate the installation of applications on the user'sdevice, such that version-incompatible applications are not installed.

Each Android platform version stores its API Level identifier internally, inthe Android system itself.

Applications can use a manifest element provided by the framework API —<uses-sdk> — to describe the minimum and maximum APILevels under which they are able to run, as well as the preferred API Level thatthey are designed to support. The element offers three key attributes:

  • android:minSdkVersion — Specifies the minimum API Levelon which the application is able to run. The default value is "1".
  • android:targetSdkVersion — Specifies the API Levelon which the application is designed to run. In some cases, this allows theapplication to use manifest elements or behaviors defined in the targetAPI Level, rather than being restricted to using only those definedfor the minimum API Level.
  • android:maxSdkVersion — Specifies the maximum API Levelon which the application is able to run. Important: Please read the <uses-sdk>documentation before using this attribute.

For example, to specify the minimum system API Level that an applicationrequires in order to run, the application would include in its manifest a<uses-sdk> element with a android:minSdkVersionattribute. The value of android:minSdkVersion would be the integercorresponding to the API Level of the earliest version of the Android platformunder which the application can run.

When the user attempts to install an application, or when revalidating anappplication after a system update, the Android system first checks the<uses-sdk> attributes in the application's manifest andcompares the values against its own internal API Level. The system allows theinstallation to begin only if these conditions are met:

  • If a android:minSdkVersion attribute is declared, its valuemust be less than or equal to the system's API Level integer. If not declared,the system assumes that the application requires API Level 1.
  • If a android:maxSdkVersion attribute is declared, its valuemust be equal to or greater than the system's API Level integer.If not declared, the system assumes that the applicationhas no maximum API Level. Please read the <uses-sdk>documentation for more information about how the system handles this attribute.

When declared in an application's manifest, a <uses-sdk>element might look like this:

<manifest>
  <uses-sdk android:minSdkVersion="5" />
  ...
</manifest>

The principal reason that an application would declare an API Level inandroid:minSdkVersion is to tell the Android system that it isusing APIs that were introduced in the API Level specified. If theapplication were to be somehow installed on a platform with a lower API Level,then it would crash at run-time when it tried to access APIs that don't exist.The system prevents such an outcome by not allowing the application to beinstalled if the lowest API Level it requires is higher than that of theplatform version on the target device.

For example, the android.appwidget package was introduced with APILevel 3. If an application uses that API, it must declare aandroid:minSdkVersion attribute with a value of "3". Theapplication will then be installable on platforms such as Android 1.5 (API Level3) and Android 1.6 (API Level 4), but not on the Android 1.1 (API Level 2) andAndroid 1.0 platforms (API Level 1).

For more information about how to specify an application's API Levelrequirements, see the <uses-sdk> section of the manifest file documentation.

Development Considerations

The sections below provide information related to API level that you shouldconsider when developing your application.

Application forward compatibility

Android applications are generally forward-compatible with new versions ofthe Android platform.

Because almost all changes to the framework API are additive, an Androidapplication developed using any given version of the API (as specified by itsAPI Level) is forward-compatible with later versions of the Android platform andhigher API levels. The application should be able to run on all later versionsof the Android platform, except in isolated cases where the application uses apart of the API that is later removed for some reason.

Forward compatibility is important because many Android-powered devicesreceive over-the-air (OTA) system updates. The user may install yourapplication and use it successfully, then later receive an OTA update to a newversion of the Android platform. Once the update is installed, your applicationwill run in a new run-time version of the environment, but one that has the APIand system capabilities that your application depends on.

In some cases, changes below the API, such those in the underlyingsystem itself, may affect your application when it is run in the newenvironment. For that reason it's important for you, as the applicationdeveloper, to understand how the application will look and behave in each systemenvironment. To help you test your application on various versions of the Androidplatform, the Android SDK includes multiple platforms that you can download.Each platform includes a compatible system image that you can run in an AVD, totest your application.

Application backward compatibility

Android applications are not necessarily backward compatible with versions ofthe Android platform older than the version against which they were compiled.

Each new version of the Android platform can include new framework APIs, suchas those that give applications access to new platform capabilities or replaceexisting API parts. The new APIs are accessible to applications when running onthe new platform and, as mentioned above, also when running on later versions ofthe platform, as specified by API Level. Conversely, because earlier versions ofthe platform do not include the new APIs, applications that use the new APIs areunable to run on those platforms.

Although it's unlikely that an Android-powered device would be downgraded toa previous version of the platform, it's important to realize that there arelikely to be many devices in the field that run earlier versions of theplatform. Even among devices that receive OTA updates, some might lag andmight not receive an update for a significant amount of time.

Selecting a platform version and API Level

When you are developing your application, you will need to choosethe platform version against which you will compile the application. Ingeneral, you should compile your application against the lowest possibleversion of the platform that your application can support.

You can determine the lowest possible platform version by compiling theapplication against successively lower build targets. After you determine thelowest version, you should create an AVD using the corresponding platformversion (and API Level) and fully test your application. Make sure to declare aandroid:minSdkVersion attribute in the application's manifest andset its value to the API Level of the platform version.

Declaring a minimum API Level

If you build an application that uses APIs or system features introduced inthe latest platform version, you should set theandroid:minSdkVersion attribute to the API Level of the latestplatform version. This ensures that users will only be able to install yourapplication if their devices are running a compatible version of the Androidplatform. In turn, this ensures that your application can function properly ontheir devices.

If your application uses APIs introduced in the latest platform version butdoes not declare a android:minSdkVersion attribute, thenit will run properly on devices running the latest version of the platform, butnot on devices running earlier versions of the platform. In the lattercase, the application will crash at runtime when it tries to use APIs that don'texist on the earlier versions.

Testing against higher API Levels

After compiling your application, you should make sure to test it on theplatform specified in the application's android:minSdkVersionattribute. To do so, create an AVD that uses the platform version required byyour application. Additionally, to ensure forward-compatibility, you should runand test the application on all platforms that use a higher API Level than thatused by your application.

The Android SDK includes multiple platform versions that you can use,including the latest version, and provides an updater tool that you can use todownload other platform versions as necessary.

To access the updater, use the android command-line tool,located in the <sdk>/tools directory. You can launch the SDK updater byexecuting android sdk. You canalso simply double-click the android.bat (Windows) or android (OS X/Linux) file.In ADT, you can also access the updater by selectingWindow > Android SDKManager.

To run your application against different platform versions in the emulator,create an AVD for each platform version that you want to test. For moreinformation about AVDs, see Creating and Managing Virtual Devices. Ifyou are using a physical device for testing, ensure that you know the API Levelof the Android platform it runs. See the table at the top of this document fora list of platform versions and their API Levels.

Using a Provisional API Level

In some cases, an "Early Look" Android SDK platform may be available. To letyou begin developing on the platform although the APIs may not be final, theplatform's API Level integer will not be specified. You must instead use theplatform's provisional API Level in your application manifest, in orderto build applications against the platform. A provisional API Level is not aninteger, but a string matching the codename of the unreleased platform version.The provisional API Level will be specified in the release notes for the EarlyLook SDK release notes and is case-sensitive.

The use of a provisional API Level is designed to protect developers anddevice users from inadvertently publishing or installing applications based onthe Early Look framework API, which may not run properly on actual devicesrunning the final system image.

The provisional API Level will only be valid while using the Early Look SDKand can only be used to run applications in the emulator. An application usingthe provisional API Level can never be installed on an Android device. At thefinal release of the platform, you must replace any instances of the provisionalAPI Level in your application manifest with the final platform's actual APILevel integer.

Filtering the Reference Documentation by API Level

Reference documentation pages on the Android Developers site offer a "Filterby API Level" control in the top-right area of each page. You can use thecontrol to show documentation only for parts of the API that are actuallyaccessible to your application, based on the API Level that it specifies inthe android:minSdkVersion attribute of its manifest file.

To use filtering, select the checkbox to enable filtering, just below thepage search box. Then set the "Filter by API Level" control to the same APILevel as specified by your application. Notice that APIs introduced in a laterAPI Level are then grayed out and their content is masked, since they would notbe accessible to your application.

Filtering by API Level in the documentation does not provide a viewof what is new or introduced in each API Level — it simply provides a wayto view the entire API associated with a given API Level, while excluding APIelements introduced in later API Levels.

If you decide that you don't want to filter the API documentation, justdisable the feature using the checkbox. By default, API Level filtering isdisabled, so that you can view the full framework API, regardless of API Level.

Also note that the reference documentation for individual API elementsspecifies the API Level at which each element was introduced. The API Levelfor packages and classes is specified as "Since <api level>" at thetop-right corner of the content area on each documentation page. The API Levelfor class members is specified in their detailed description headers,at the right margin.

http://www.bkjia.com/Androidjc/817128.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/817128.htmlTechArticlesyntax: uses-sdk android: minSdkVersion = integer android: targetSdkVersion = integer android: maxSdkVersion = integer / contained in: manifest description: Lets you express an app...

本文源自: 环亚娱乐

相关文章Related Articles

如果您觉得本站还不错,就请分享给身边的好友吧


分享成功还有机会获得精美礼品哦