自动检查android:minSdkVersion的违规行为

时间:2013-02-08 23:02:42

标签: android android-manifest

我正在开发一个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列表或一些可以使审计更容易的事情?

2 个答案:

答案 0 :(得分:4)

看起来android lint工具执行了一些检查(感谢上面评论中的@CommonsWare):

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

$ lint myProjectDir --check NewApi

Scanning .: .....

No issues found.

从SDK Tools Revision 21开始,您似乎也可以将其作为ant lint运行。

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


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

java.compilerargs=-Xlint:all

目前还不清楚javac是否报告像lint这样的事件 - 检查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的一些有用链接:

答案 1 :(得分:2)

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

覆盖APP_MIN_PLATFORM_LEVEL = 99