23

I am a Android developer. I have already design my own lint rules by implementing new XXXDetector and XXXIssueRegistry, here is my source code snip:

My XXXIssueRegistry file:

public class MyIssueRegistry extends IssueRegistry {
  @Override
  public List<Issue> getIssues() {

    System.out.println("!!!!!!!!!!!!! ljf MyIssueRegistry lint rules works");
    return Arrays.asList(AttrPrefixDetector.ISSUE,
            LoggerUsageDetector.ISSUE);
  }
}

My XXXDetector file:

public class LoggerUsageDetector extends Detector
    implements Detector.ClassScanner {
public static final Issue ISSUE = Issue.create("LogUtilsNotUsed",
        "You must use our `LogUtils`",
        "Logging should be avoided in production for security and performance reasons. Therefore, we created a LogUtils that wraps all our calls to Logger and disable them for release flavor.",
        Category.MESSAGES,
        9,
        Severity.ERROR,
        new Implementation(LoggerUsageDetector.class,
                Scope.CLASS_FILE_SCOPE));

@Override
public List<String> getApplicableCallNames() {
    return Arrays.asList("v", "d", "i", "w", "e", "wtf");
}

@Override
public List<String> getApplicableMethodNames() {
    return Arrays.asList("v", "d", "i", "w", "e", "wtf");
}

@Override
public void checkCall(@NonNull ClassContext context,
                      @NonNull ClassNode classNode,
                      @NonNull MethodNode method,
                      @NonNull MethodInsnNode call) {
    String owner = call.owner;
    if (owner.startsWith("android/util/Log")) {
        context.report(ISSUE,
                method,
                call,
                context.getLocation(call),
                "You must use our `LogUtils`");
    }
}
}

Now I can run my custom lint rules by runnig command:

$gradle lint

And I will get output message like I expected in console.

But I want to debug my XXXDetector source file. How can I do that? If I click "debug" or "run" or "build" , my custom lint rules will NOT run! So I have to run it in console, which don't support debug. How can I solve this?

ljfxyj2008
  • 231
  • 1
  • 4

2 Answers2

35

To debug a custom lint check you need to run a Gradle lint task with -Dorg.gradle.debug=true parameter, for example:

./gradlew --no-daemon -Dorg.gradle.debug=true lintDebug

The Gradle will stop execution until a debugger is attached.

Attach a debugger to a local process:

Attach to local process

And select a corresponding Java process:

Select process

After debugger has been attached, the Gradle will continue execution and you will able to debug your custom lint rule:

Debugger window

italankin
  • 779
  • 8
  • 14
2

Here's how you can debug your lint rules in AndroidStudio:

Click on Edit configurations... edit configurations

Add a new run configuration "gradle":

add configuration

Then, select your project and enter lintDebug for the task (it's lintLiveDebug for me because i have multiple different debug build variants and one of them is called liveDebug).

enter image description here

Now start this configuration with a click on the debug button like you are used to. This worked very well for me.

Also i can recommend creating a test suite for your lint code, for faster development cycles and debugging.

Falco Winkler
  • 1,082
  • 1
  • 18
  • 24