简体   繁体   English

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

[英]Checking for violations of android:minSdkVersion automatically

I'm working on an android app, and worried that we have introduced code that uses functionality from API levels later than its minSdkVersion . 我正在开发一个Android应用程序,并担心我们引入的代码使用API​​级别的功能,而不是minSdkVersion

I'd like to know if there's any way to detect some of these violations automatically. 我想知道是否有办法自动检测这些违规行为。

In this app's AndroidManifest.xml file, it specifies: 在此应用程序的AndroidManifest.xml文件中,它指定:

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

ndk-build is seems to be giving me a warning about this (or is this warning for another reason?): 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    

While I realize any tool cannot be 100% accurate due to runtime dispatch, other than: 虽然我发现由于运行时调度,任何工具都不能100%准确,除了:

  • a full audit of the source code, or 对源代码进行全面审核,或
  • a complete testing of all code paths on a device running minSdkVersion (in this case android-8 = 2.2 = Froyo) 对运行minSdkVersion的设备上的所有代码路径进行完整测试(在本例中为android-8 = 2.2 = Froyo)

...is there a lint-like tool, a build setting change, or anything else I can use to identify at least the most obvious/blatant violations? ...是否有类似lint的工具,构建设置更改,或者我可以用来识别至少最明显/明显违规的任何其他内容?

If no such thing exists, is there a comprehensive API list or something that will make an audit easier? 如果不存在这样的事情,是否有一个全面的API列表或一些可以使审计更容易的东西?

It looks like the android lint tool does some of these checks (thanks @CommonsWare in the comments above): 看起来android lint工具执行了一些检查(感谢上面评论中的@CommonsWare):

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

$ lint myProjectDir --check NewApi

Scanning .: .....

No issues found.

It appears you can also run this as ant lint as of SDK Tools Revision 21. 从SDK Tools Revision 21开始,您似乎也可以将其作为ant lint运行。

Lint has various nice output formats, it seems! 似乎Lint有各种不错的输出格式! Integration with Jenkins via --xml , etc... 通过--xml等与Jenkins集成......


In addition, in ant.properties , you can set something like this to have some lint-like checks performed by javac : 另外,在ant.properties ,你可以设置这样的东西,让javac执行一些类似lint的检查:

java.compilerargs=-Xlint:all

It's unclear whether javac reports incidents like lint --check NewApi does, but it does report [deprecation] entries at least. 目前还不清楚javac是否报告像lint这样的事件 - 检查NewApi,但它至少会报告[deprecation]条目。 (If you find a reference for what javac reports, please post a comment.) (如果您找到javac报告的参考,请发表评论。)


For posterity, here's the description of lint's NewApi checker: 对于后代,这里是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.

And here are some helpful links regarding lint : 这里有一些关于lint有用链接:

easiest solution I found was to add this line to the Application.mk file 我找到的最简单的解决方案是将此行添加到Application.mk文件中

override APP_MIN_PLATFORM_LEVEL=99 覆盖APP_MIN_PLATFORM_LEVEL = 99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM