Recent Changes‎ > ‎

Lint On Save

posted Jun 1, 2012, 9:27 AM by Tor Norbye
In ADT 20, Lint can now run incrementally on not just XML files, but Java files as well. As soon as you save a file, Lint runs a lint check on the current file and updates error markers on the file. Certain types of checks require "global" analysis, search as "Unused Resources", and those rules are skipped; only checks which can be performed on a single file in isolation are checked. However, it turns out that that's the case for nearly all the Java related checks.

For example, a very common problem for people using the Toast class is forgetting to call "show()" on the result of makeText:

Here's another example. Let's say you're trying to set a text color on a button. It has a setTextColor() method, which takes an int. It expects a packed RGB int. However, the resource id for color also happens to be an int, so you might accidentally do this, which compiles just fine:

The error underline here is from lint, not the compiler. And yes -- the above has happened a lot! One of the fun things about writing new lint rules is running lint on large codebases and seeing all the places the error is found!

There are several new lint rules in ADT 20 Preview 3. One of them finds problems with fragments, where either the fragment is a non-static inner class, or it does not have a default constructor, which means that the fragment cannot be reinstantiated by the system if the configuration changes. The lint rule will also warn if the fragment contain other constructors, since this is strongly discouraged.

Other recent rules in lint include 
  • a duplicate activity registration check (since if you accidentally register an activity more than once, some really subtle errors can occur. The two elements do not get merged, so you may be adding attributes which are ignored, and so on.)
  • a handler leak check, which looks for handler inner classes. If they are not static, the compiler will include a reference to the outer instance, which means the outer object will be retained.