【问题标题】:How to debug java source code when I implement a custom Detector for Lint?当我为 Lint 实现自定义检测器时如何调试 java 源代码?
【发布时间】:2016-01-14 08:52:29
【问题描述】:

我是一名 Android 开发人员。 我已经通过实现新的 XXXDetector 和 XXXIssueRegistry 设计了自己的 lint 规则,这是我的源代码片段:

我的 XXXIssueRegistry 文件:

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);
  }
}

我的 XXXDetector 文件:

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`");
    }
}
}

现在我可以通过 runnig 命令运行我的自定义 lint 规则:

$gradle lint

我会在控制台中得到预期的输出消息。

但我想调试我的 XXXDetector 源文件。我怎样才能做到这一点? 如果我单击“调试”或“运行”或“构建”,我的自定义 lint 规则将不会运行!所以我必须在不支持调试的控制台中运行它。 我该如何解决这个问题?

【问题讨论】:

    标签: java android lint


    【解决方案1】:

    要调试自定义 lint 检查,您需要使用 -Dorg.gradle.debug=true 参数运行 Gradle lint 任务,例如:

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

    Gradle 将停止执行,直到附加了调试器。

    将调试器附加到本地进程:

    并选择一个对应的Java进程:

    附加调试器后,Gradle 将继续执行,您将能够调试自定义 lint 规则:

    【讨论】:

    • 不错的答案??
    • 我无法完成这项工作。这仍然是调试自定义 lint 代码的方法吗?
    • 另一种选择是使用测试进行调试
    • 同时“附加到本地进程...”更改为“附加到进程...”
    • 这对我很有用。谢谢!
    【解决方案2】:

    以下是在 AndroidStudio 中调试 lint 规则的方法:

    点击Edit configurations...

    添加新的运行配置“gradle”:

    然后,选择您的项目并为任务输入lintDebug(对我来说是lintLiveDebug,因为我有多个不同的调试构建变体,其中一个称为liveDebug)。

    现在像您习惯的那样单击调试按钮来启动此配置。这对我来说效果很好。

    我还建议创建一个test suite for your lint code,以加快开发周期和调试。

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 2011-10-08
      • 2019-09-30
      相关资源
      最近更新 更多