blob: acf68997d70d7ed588e76bb83e44d96faa214845 [file] [log] [blame]
Scott Mainaf09b672009-07-31 13:11:07 -07001page.title=Android API Levels
2@jd:body
3
Dirk Doughertya6602f12009-08-27 16:26:43 -07004<div id="qv-wrapper">
5<div id="qv">
Scott Mainaf09b672009-07-31 13:11:07 -07006
Dirk Doughertya6602f12009-08-27 16:26:43 -07007 <h2>In this document</h2>
8<ol>
9 <li><a href="intro">What is API Level?</a></li>
10 <li><a href="#uses">Uses of API Level</a></li>
11 <li><a href="#considerations">Development Considerations</a>
12 <ol>
13 <li><a href="#fc">Application forward compatibility</a></li>
14 <li><a href="#bc">Application backward compatibility</a></li>
15 <li><a href="#platform">Selecting a platform version for compiling</a></li>
16 <li><a href="#apilevel">Declaring a minimum API Level</a></li>
17 <li><a href="#testing">Testing against higher API Levels</a></li>
18 </ol>
19 </li>
20 <li><a href="#filtering">Filtering the documentation</a></li>
21</ol>
Scott Mainaf09b672009-07-31 13:11:07 -070022
Dirk Doughertya6602f12009-08-27 16:26:43 -070023 <h2>See also</h2>
24 <ol>
25 <li><a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt;</a> manifest element</li>
26 </ol>
Scott Mainaf09b672009-07-31 13:11:07 -070027
Dirk Doughertya6602f12009-08-27 16:26:43 -070028</div>
29</div>
Scott Mainaf09b672009-07-31 13:11:07 -070030
Dirk Doughertya6602f12009-08-27 16:26:43 -070031<p>As you develop your application on Android, it's useful to understand the
32platform's general approach to API change management. It's also important to
33understand the API Level identifier and the role it plays in ensuring your
34application's compatibility with devices on which it may be installed. </p>
Scott Mainaf09b672009-07-31 13:11:07 -070035
Dirk Doughertya6602f12009-08-27 16:26:43 -070036<p>The sections below provide information about API Level and how it affects
37your applications. </p>
Scott Mainaf09b672009-07-31 13:11:07 -070038
Dirk Doughertya6602f12009-08-27 16:26:43 -070039<p>For information about how to use the "Filter by API Level" control
40available in the API reference documentation, see
41<a href="#filtering">Filtering the documentation</a> at the
42end of this document. </p>
Scott Mainaf09b672009-07-31 13:11:07 -070043
Dirk Doughertya6602f12009-08-27 16:26:43 -070044<h2 id="intro">What is API Level?</h2>
Scott Mainaf09b672009-07-31 13:11:07 -070045
Dirk Doughertya6602f12009-08-27 16:26:43 -070046<p>API Level is an integer value that uniquely identifies the framework API
47revision offered by a version of the Android platform.</p>
Scott Mainaf09b672009-07-31 13:11:07 -070048
Dirk Doughertya6602f12009-08-27 16:26:43 -070049<p>The Android platform provides a framework API that applications can use to
50interact with the underlying Android system. The framework API consists of:</p>
51
52<ul>
53<li>A core set of packages and classes</li>
54<li>A set of XML elements and attributes for declaring a manifest file</li>
55<li>A set of XML elements and attributes for declaring and accessing resources</li>
56<li>A set of Intents</li>
57<li>A set of permissions that applications can request, as well as permission
58enforcements included in the system</li>
59</ul>
60
61<p>Each successive version of the Android platform can include updates to the
62Android application framework API that it delivers. </p>
63
64<p>Updates to the framework API are designed so that the new API remains
65compatible with earlier versions of the API. That is, most changes in the API
66are additive and introduce new or replacement functionality. As parts of the API
67are upgraded, the older replaced parts are deprecated but are not removed, so
68that existing applications can still use them. In a very small number of cases,
69parts of the API may be modified or removed, although typically such changes are
70only needed to ensure API robustness and application or system security. All
71other API parts from earlier revisions are carried forward without
72modification.</p>
73
74<p>The framework API that an Android platform delivers is specified using an
75integer identifier called "API Level". Each Android platform version supports
76exactly one API Level, although support is implicit for all earlier API Levels
77(down to API Level 1). The initial release of the Android platform provided
78API Level 1 and subsequent releases have incremented the API Level.</p>
79
80<p>The following table specifies the API Level supported by each version of the
81Android platform.</p>
Scott Mainaf09b672009-07-31 13:11:07 -070082
83<table>
84 <tr><th>Platform Version</th><th>API Level</th></tr>
Dirk Doughertya6602f12009-08-27 16:26:43 -070085 <tr><td>Android 1.6</td><td>4</td></tr>
Scott Mainaf09b672009-07-31 13:11:07 -070086 <tr><td>Android 1.5</td><td>3</td></tr>
Dirk Doughertya6602f12009-08-27 16:26:43 -070087 <tr><td>Android 1.1</td><td>2</td></tr>
88 <tr><td>Android 1.0</td><td>1</td></tr>
Scott Mainaf09b672009-07-31 13:11:07 -070089</table>
90
91
Dirk Doughertya6602f12009-08-27 16:26:43 -070092<h2 id="uses">Uses of API Level in Android</h2>
Scott Mainaf09b672009-07-31 13:11:07 -070093
Dirk Doughertya6602f12009-08-27 16:26:43 -070094<p>The API Level identifier serves a key role in ensuring the best possible
95experience for users and application developers:
Scott Mainaf09b672009-07-31 13:11:07 -070096
Dirk Doughertya6602f12009-08-27 16:26:43 -070097<ul>
98<li>It lets the Android platform describe the maximum framework API revision
99that it supports</li>
100<li>It lets applications describe the framework API revision that they
101require</li>
102<li>It lets the system negotiate the installation of applications on the user's
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700103device, such that version-incompatible applications are not installed.</li>
Dirk Doughertya6602f12009-08-27 16:26:43 -0700104</ul>
105
106<p>Each Android platform version stores its API Level identifier internally, in
107the Android system itself. </p>
108
109<p>Applications can use a manifest element provided by the framework API &mdash;
110<code>&lt;uses-sdk&gt;</code> &mdash; to describe the minimum and maximum API
111Levels under which they are able to run, as well as the preferred API Level that
112they are designed to support. The element offers two key attributes:</p>
113
114<ul>
115<li><code>android:minSdkVersion</code> &mdash; Specifies the minimum API Level
116on which the application is able to run. The default value is "1".</li>
117<li><code>android:maxSdkVersion</code> &mdash; Specifies the maximum API Level
118on which the application is able to run.</li>
119</ul>
120
121<p>For example, to specify the minimum system API Level that an application
122requires in order to run, the application would include in its manifest a
123<code>&lt;uses-sdk&gt;</code> element with a <code>android:minSdkVersion</code>
124attribute. The value of <code>android:minSdkVersion</code> would be the integer
125corresponding to the API Level of the earliest version of the Android platform
126under which the application can run. </p>
127
128<p>When the user attempts to install an application, the Android system first
129checks the <code>&lt;uses-sdk&gt;</code> attributes in the application's
130manifest and compares them against its own internal API Level. The system
131allows the installation to begin only if these conditions are met:</p>
132
133<ul>
134<li>If a <code>android:minSdkVersion</code> attribute is declared, its value
135must be less than or equal to the system's API Level integer. If not declared,
136the system assumes that the application requires API Level 1. </li>
137<li>If a <code>android:maxSdkVersion</code> attribute is declared, its value
138must be equal to or greater than the system's API Level integer.
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700139If not declared, the system assumes that the application
Dirk Doughertya6602f12009-08-27 16:26:43 -0700140has no maximum API Level. </li>
141</ul>
142
143<p>When declared in an application's manifest, a <code>&lt;uses-sdk&gt;</code>
144element might look like this: </p>
145
146<pre>&lt;manifest&gt;
147 ...
148 &lt;uses-sdk android:minSdkVersion="4" /&gt;
149 ...
150&lt;/manifest&gt;</pre>
151
152<p>The principal reason that an application would declare an API Level in
153<code>android:minSdkVersion</code> is to tell the Android system that it is
154using APIs that were <em>introduced</em> in the API Level specified. If the
155application were to be somehow installed on a platform with a lower API Level,
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700156then it would crash at run-time when it tried to access APIs that don't exist.
Dirk Doughertya6602f12009-08-27 16:26:43 -0700157The system prevents such an outcome by not allowing the application to be
158installed if the lowest API Level it requires is higher than that of the
159platform version on the target device.</p>
160
161<p>For example, the {@link android.appwidget} package was introduced with API
162Level 3. If an application uses that API, it must declare a
163<code>android:minSdkVersion</code> attribute with a value of "3". The
164application will then be installable on platforms such as Android 1.5 (API Level
1653) and Android 1.6 (API Level 4), but not on the Android 1.1 (API Level 2) and
166Android 1.0 platforms (API Level 1).</p>
167
168<p>For more information about how to specify an application's API Level
169requirements, see the <a
170href="{@docRoot}guide/topics/manifest/uses-sdk-element.html"><code>&lt;uses-sdk&gt;</code></a>
171 section of the manifest file documentation.</p>
Scott Mainaf09b672009-07-31 13:11:07 -0700172
173
Dirk Doughertya6602f12009-08-27 16:26:43 -0700174<h2 id="considerations">Development Considerations</h2>
Scott Mainaf09b672009-07-31 13:11:07 -0700175
Dirk Doughertya6602f12009-08-27 16:26:43 -0700176<p>The sections below provide information related to API level that you should
177consider when developing your application.</p>
178
179<h3 id="fc">Application forward compatibility</h3>
180
181<p>Android applications are generally forward-compatible with new versions of
182the Android platform.</p>
183
184<p>Because almost all changes to the framework API are additive, an Android
185application developed using any given version of the API (as specified by its
186API Level) is forward-compatible with later versions of the Android platform and
187higher API levels. The application should be able to run on all later versions
188of the Android platform, except in isolated cases where the application uses a
189part of the API that is later removed for some reason. </p>
190
191<p>Forward compatibility is important because many Android-powered devices
192receive over-the-air (OTA) system updates. The user may install your
193application and use it successfully, then later receive an OTA update to a new
194version of the Android platform. Once the update is installed, your application
195will run in a new run-time version of the environment, but one that has the API
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700196and system capabilities that your application depends on. </p>
Dirk Doughertya6602f12009-08-27 16:26:43 -0700197
198<p>In some cases, changes <em>below</em> the API, such those in the underlying
199system itself, may affect your application when it is run in the new
200environment. For that reason it's important for you, as the application
201developer, to understand how the application will look and behave in each system
202environment. To help you test your application on various versions of the Android
203platform, the Android SDK includes multiple platforms that you can download.
204Each platform includes a compatible system image that you can run in an AVD, to
205test your application. </p>
206
207<h3 id="bc">Application backward compatibility</h3>
208
209<p>Android applications are not necessarily backward compatible with versions of
210the Android platform older than the version against which they were compiled.
211</p>
212
213<p>Each new version of the Android platform can include new framework APIs, such
214as those that give applications access to new platform capabilities or replace
215existing API parts. The new APIs are accessible to applications when running on
216the new platform and, as mentioned above, also when running on later versions of
217the platform, as specified by API Level. Conversely, because earlier versions of
218the platform do not include the new APIs, applications that use the new APIs are
219unable to run on those platforms.</p>
220
221<p>Although it's unlikely that an Android-powered device would be downgraded to
222a previous version of the platform, it's important to realize that there are
223likely to be many devices in the field that run earlier versions of the
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700224platform. Even among devices that receive OTA updates, some might lag and
225might not receive an update for a significant amount of time. </p>
Dirk Doughertya6602f12009-08-27 16:26:43 -0700226
227<h3 id="platform">Selecting a platform version and API Level</h3>
228
229<p>When you are developing your application, you will need to choose
230the platform version against which you will compile the application. In
231general, you should compile your application against the lowest possible
232version of the platform that your application can support.
233
234<p>You can determine the lowest possible platform version by compiling the
235application against successively lower build targets. After you determine the
236lowest version, you should create an AVD using the corresponding platform
237version (and API Level) and fully test your application. Make sure to declare a
238<code>android:minSdkVersion</code> attribute in the application's manifest and
239set its value to the API Level of the platform version. </p>
240
241<h3 id="apilevel">Declaring a minimum API Level</h3>
242
243<p>If you build an application that uses APIs or system features introduced in
244the latest platform version, you should set the
245<code>android:minSdkVersion</code> attribute to the API Level of the latest
246platform version. This ensures that users will only be able to install your
247application if their devices are running a compatible version of the Android
248platform. In turn, this ensures that your application can function properly on
249their devices. </p>
250
251<p>If your application uses APIs introduced in the latest platform version but
252does <em>not</em> declare a <code>android:minSdkVersion</code> attribute, then
253it will run properly on devices running the latest version of the platform, but
254<em>not</em> on devices running earlier versions of the platform. In the latter
255case, the application will crash at runtime when it tries to use APIs that don't
256exist on the earlier versions.</p>
257
258<h3 id="testing">Testing against higher API Levels</h3>
259
260<p>After compiling your application, you should make sure to test it on the
261platform specified in the application's <code>android:minSdkVersion</code>
262attribute. To do so, create an AVD that uses the platform version required by
263your application. Additionally, to ensure forward-compatibility, you should run
264and test the application on all platforms that use a higher API Level than that
265used by your application. </p>
266
267<p>The Android SDK includes multiple platform versions that you can use,
268including the latest version, and provides an updater tool that you can use to
269download other platform versions as necessary. </p>
270
271<p>To access the updater, use the <code>android</code> command-line tool,
272located in the &lt;sdk&gt;/tools directory. You can launch the Updater by using
273the <code>android</code> command without specifying any options. You can
274also simply double-click the android.bat (Windows) or android (OS X/Linux) file.
275In ADT, you can also access the updater by selecting
276<strong>Window</strong>&nbsp;>&nbsp;<strong>Android SDK and AVD
277Manager</strong>.</p>
278
279<p>To run your application against different platform versions in the emulator,
280create an AVD for each platform version that you want to test. For more
281information about AVDs, see <a
282href="{@docRoot}guide/developing/tools/avd.html">Android Virtual Devices</a>. If
283you are using a physical device for testing, ensure that you know the API Level
284of the Android platform it runs. See the table at the top of this document for
285a list of platform versions and their API Levels. </p>
286
Dirk Doughertya6602f12009-08-27 16:26:43 -0700287
288<h2 id="filtering">Filtering the Reference Documentation by API Level</h2>
289
290<p>Reference documentation pages on the Android Developers site offer a "Filter
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700291by API Level" control in the top-right area of each page. You can use the
292control to show documentation only for parts of the API that are actually
293accessible to your application, based on the API Level that it specifies in
294the <code>android:minSdkVersion</code> attribute of its manifest file. </p>
Dirk Doughertya6602f12009-08-27 16:26:43 -0700295
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700296<p>To use filtering, set the control to the same API Level as that specified
297by your application. Notice that APIs introduced in a later API Level are
298then grayed out and their content is masked, since they would not be
299accessible to your application. </p>
Dirk Doughertya6602f12009-08-27 16:26:43 -0700300
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700301<p>Filtering by API Level in the documentation does not provide a view
302of what is new or introduced in each API Level &mdash; it simply provides a way
Dirk Doughertya6602f12009-08-27 16:26:43 -0700303to view the entire API associated with a given API Level, while excluding API
304elements introduced in later API Levels.</p>
305
306<p>By default, API Level filtering is enabled and set to show the latest API
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700307Level. If you do not want to use filtering reference documentation,
308simply select the highest available API Level. </p>
Dirk Doughertya6602f12009-08-27 16:26:43 -0700309
Dirk Dougherty4c8a16a2009-09-10 10:45:41 -0700310<p>Also note that the reference documentation for individual API elements
311specifies the API Level at which the elements were introduced. The API Level
312for packages and classes is specified as "Since &lt;api level&gt;" at the
313top-right corner of the content area on each documentation page. The API Level
314for class members is specified in their detailed description headers,
315at the right margin. </p>