【问题标题】:If I write an app using Android 4 Platform, will the app work in Android 2 devices?如果我使用 Android 4 平台编写应用程序,该应用程序可以在 Android 2 设备上运行吗?
【发布时间】:2012-07-04 01:13:00
【问题描述】:

我想使用 Android SDK 和 Eclipse 编写一个应用程序。 我使用 SDK 管理器安装了 Android 4 平台,但我想知道,这个应用程序是否适用于 Android 2 设备?还是只有 Android 4 设备?

谢谢。

【问题讨论】:

  • 我想你要关心的是API。 “除了以上所有内容,Android 4.0 自然支持以前版本的所有 API”developer.android.com/about/versions/android-4.0.html
  • @0gravity:但相反的情况当然不是这样:Android 2 设备支持更高版本的所有 API。
  • @Thilo 是的,我很高兴你提到了。

标签: android eclipse sdk


【解决方案1】:

在您的 App Manifest XML 文件中,您必须指定最低和所需目标 SDK 版本。 我正在开发一个面向 Android 4.0.3 (SDK v15) 但必须在 2.3.3 (SDK v10) 上运行的应用程序。

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="15" />

当然,您只能使用较低的 SDK 可用功能。 您还应该查看为旧 SDK 提供一些新功能的 Google 支持库。 http://developer.android.com/tools/extras/support-library.html

// 马塞洛

【讨论】:

  • 谢谢,这真的很有帮助。但是当我在Eclipse中新建项目时,我应该选择v15还是v10?
  • 通常您希望 targetSdkVersion 是最新的 sdk(现在是 v15),除非有特定原因要使用早期版本构建。然后,您将使用诸如 @TargetAPI、支持库或 ActionBarSherlock 之类的装饰器来支持旧系统上以前的 sdk 版本回到您的 minSdkVersion。如果这两个值不同,Lint 会给你一个警告,这个警告是提醒你注意向后兼容性,据我所知。
【解决方案2】:

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

sdk website 上,您可以看到此信息。

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

【讨论】:

  • +1。除了 API 之外,可能还有其他内容,例如打包选项。最安全的方法是同时安装旧版本的 Android SDK,并查看您的代码是否也适用。
【解决方案3】:

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

新API检查规则,见here

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 中:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2015-07-27
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    相关资源
    最近更新 更多