24

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?

2 Answers 2

37

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

4
  • I can't make this work. Is this still the way to debug custom lint code? Commented Jun 20, 2020 at 17:50
  • 2
    The other option is to debug with tests
    – italankin
    Commented Jun 23, 2020 at 7:37
  • 2
    In the mean time "Attach to Local Process..." changed to "Attach to Process..."
    – JU5T1C3
    Commented Jul 27, 2021 at 8:34
  • This worked great for me. Thanks! Commented Sep 24, 2021 at 22:42
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.