如果我使用Android 4平台编写应用程序,那么这个应用程序能在Android 2设备上运行吗?

3

我想使用Android SDK和Eclipse编写一个应用程序。

我使用SDK管理器安装了Android 4平台,但我想知道,这个应用程序只能在Android 4设备上运行吗?还是可以在Android 2设备上运行?

谢谢。


我认为你需要关注的是API。 "除了以上所有内容,Android 4.0自然支持之前版本的所有API" <- 来自http://developer.android.com/about/versions/android-4.0.html - 0gravity
1
@0gravity:但相反的情况当然不成立:Android 2设备不支持后续版本中的所有API。 - Thilo
@Thilo 是的,我很高兴你提到了它。 - 0gravity
3个回答

2

这取决于您所使用的系统调用。一定要在运行不同版本的设备上进行测试,因为某些调用仅适用于特定的API级别。

您可以在SDK网站上查看此信息。

(请参见getNumberOfCameras函数灰色条的右侧“Since: API Level 9”)


1
除了API之外,可能还有其他的东西,比如打包选项。最安全的方法是同时安装旧版本的Android SDK,并查看您的代码是否也能在其中运行。 - Thilo

2
在您的应用清单XML文件中,必须指定最低和期望的目标SDK版本。我正在开发一个目标为Android 4.0.3(SDK v15)的应用程序,但必须在2.3.3(SDK v10)上运行。
<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="15" />

当然,您必须仅使用可用的较低SDK功能。您还应该查看Google支持库,该支持库为旧版SDK提供了一些新功能。

http://developer.android.com/tools/extras/support-library.html

// Marcello


1
谢谢,那真的很有帮助。 但是当我在Eclipse中创建一个新项目时,我应该选择v15还是v10? - Hani
1
通常情况下,您希望targetSdkVersion是最新的sdk(现在是v15),除非有特定原因要使用早期版本进行构建。然后,您将使用@TargetAPI、支持库或ActionBarSherlock等装饰器来支持旧系统上先前的sdk版本,直到minSdkVersion。如果这两个值不同,Lint会发出警告,并提醒您注意向后兼容性。 - dar

2

Android Lint是ADT r16引入的新工具,它会自动扫描和检查您的项目中是否使用了新的API,并在Eclipse编辑器中显示错误标记。

有关检查新API的规则,请参见此处

NewApi
------
Summary: Finds API accesses to APIs that are not supported in all targeted API
versions

Priority: 6 / 10
Severity: Error
Category: Correctness

This check scans through all the Android API calls in the application and
warns about any calls that are not available on *all* versions targeted by
this application (according to its minimum SDK attribute in the manifest).

If your code is *deliberately* accessing newer APIs, and you have ensured
(e.g. with conditional execution) that this code will only ever be called on a
supported platform, then you can annotate your class or method with the
@TargetApi annotation specifying the local minimum SDK to apply, such
as@TargetApi(11), such that this check considers 11 rather than your manifest
file's minimum SDK as the required API level.

在Eclipse中:

在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接