6

我正在开发一个 android 应用程序,并担心我们引入的代码使用 API 级别的功能晚于minSdkVersion.

我想知道是否有任何方法可以自动检测其中一些违规行为。

在此应用程序的 AndroidManifest.xml文件中,它指定:

<uses-sdk android:minSdkVersion="8"
          android:targetSdkVersion="17" />

ndk-build 似乎在给我一个警告(或者这个警告是出于其他原因?):

Android NDK: Found stable platform levels: 14 3 4 5 8 9
Android NDK: Found max platform level: 14
Android NDK: Parsing ./jni/Application.mk
Android NDK:   Found APP_PLATFORM=android-17 in ./project.properties
Android NDK:   Adjusting APP_PLATFORM android-17 to android-14 and enabling -fPIE
/android-ndk/build/core/add-application.mk:128: Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml    

虽然我意识到由于运行时调度,任何工具都不能 100% 准确,除了:

  • 对源代码进行全面审核,或
  • 在运行 minSdkVersion 的设备上对所有代码路径进行完整测试(在本例中为 android-8 = 2.2 = Froyo)

...是否有类似 lint 的工具、构建设置更改或其他任何我可以用来识别至少最明显/公然违规的东西?

如果不存在这样的东西,是否有一个全面的 API 列表或可以使审计更容易的东西?

4

2 回答 2

4

看起来 androidlint工具做了一些检查(感谢上面评论中的@CommonsWare):

$ which lint
/android-sdk-macosx/tools/lint

$ lint myProjectDir --check NewApi

Scanning .: .....

No issues found.

看来您也可以ant lint从 SDK Tools Revision 21 开始运行它。

看来,Lint 有各种不错的输出格式!--xml通过等与 Jenkins 集成


此外,在 中ant.properties,您可以设置类似这样的内容,以通过以下方式执行一些类似 lint 的检查javac

java.compilerargs=-Xlint:all

目前尚不清楚 javac 是否会报告像 lint --check NewApi 这样的事件,但它[deprecation]至少会报告条目。(如果您找到 javac 报告内容的参考,请发表评论。)


对于后代,这是 lint 的 NewApi 检查器的描述:

$ lint --show NewApi
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 you really want to use this API and don't need to support older devices
just set the minSdkVersion in your AndroidManifest.xml file.
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.

这里有一些有用的链接lint

于 2013-02-11T18:11:02.870 回答
2

我找到的最简单的解决方案是将此行添加到 Application.mk 文件

覆盖 APP_MIN_PLATFORM_LEVEL=99

于 2013-04-03T01:47:54.780 回答