Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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:

<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?):

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:

  • 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)

...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?

If no such thing exists, is there a comprehensive API list or something that will make an audit easier?

like image 667
leander Avatar asked Feb 08 '13 23:02

leander


2 Answers

It looks like the android lint tool does some of these checks (thanks @CommonsWare in the comments above):

$ 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.

Lint has various nice output formats, it seems! Integration with Jenkins via --xml, etc...


In addition, in ant.properties, you can set something like this to have some lint-like checks performed by javac:

java.compilerargs=-Xlint:all

It's unclear whether javac reports incidents like lint --check NewApi does, but it does report [deprecation] entries at least. (If you find a reference for what javac reports, please post a comment.)


For posterity, here's the description of lint's NewApi checker:

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

  • http://developer.android.com/tools/help/lint.html
  • http://developer.android.com/tools/debugging/improving-w-lint.html
like image 196
leander Avatar answered Oct 19 '22 19:10

leander


easiest solution I found was to add this line to the Application.mk file

override APP_MIN_PLATFORM_LEVEL=99

like image 2
pt123 Avatar answered Oct 19 '22 19:10

pt123