首页 文章

我们如何配置Android Studio以在每个构建上运行其Lint?

提问于
浏览
61

曾几何时,特别是在Eclipse-land中,Lint会在每次构建时运行,因此如果你没有Lint检查,你会立即发现 . 使用Android Studio(在1.3上测试),Lint默认情况下不会在构建上运行 . 新人可能会犯下Lint会检查的错误,但由于Lint实际上没有运行,新人并没有发现它们 .

(IOW,如果Lint测试没有在森林中运行,是否真的有Lint测试?)

this blog post的评论显示了如何将Lint作为构建的一部分运行:

  • 编辑项目配置

  • 在配置的“常规”选项卡中,向下滚动并折叠"Before launch"面板

  • 在该面板中,为模块添加一个名为 lint 的"Run Gradle Task"步骤

但是,这会运行命令行Lint,从而将以XML和HTML格式编写的报告发送到硬盘驱动器 . 这样可行,但让Android Studio运行in-IDE Lint检查会更简洁,因此结果显示在IDE面板中 .

有没有办法设置项目构建来执行in-IDE Lint检查?

奖励积分如果可以设置为仅运行Lint,而不是分析>检查代码完成的分析 . 虽然完整的分析偶尔会有用,但Lint足够慢,更不用说Android Studio执行的可疑的其他分析(例如,拼写检查) .

虽然设置它对于所有项目来说都不是一个好的计划(Lint扫描速度很慢),对于Android新手来说,这可能是一个合适的举措 .

4 回答

  • 1

    为Android Lint创建检查配置文件

    • 转到文件 - >设置 - >编辑器/检查

    • 选择管理 - >复制

    • 将其命名为"Android Lint Profile",然后按Enter键

    • 在此选项卡上仅保留标记的Android Lint规则

    Android Lint Profile

    现在,您可以通过Analyze-> Inspect Code ...仅选择Android Lint规则进行检查,方法是选择“Android Lint Profile” .

    在下一步中,让我们记录宏(编辑 - >宏 - >开始宏录制)以及后续步骤 .

    • 分析 - >检查代码......

    • 选择"Android Lint Profile"

    • 按Enter键( important 使用键盘,因为宏录制器无法在此窗口上捕获鼠标)

    • 点击"Run Selected Configuration"(绿色播放按钮)

    停止宏录制并将其命名为“Lint and Run” .

    应该做的最后一件事是将“Shift F10”映射到我们的宏 . 转到文件 - >设置...->键盘映射 . 找到我们的宏并更改快捷方式 .

    macro keymapping

    现在lint将在每个构建 when you press Shift+F10 之前运行,结果将在Android Studio面板中 .

    但是这个解决方案有一个缺点 . 如果通过单击“运行”按钮运行构建,则不会执行lint分析 .

    可能这个信息会有所帮助,有人会提供更好的解决方案 .

  • 6

    一种可能(但难以实现)的解决方案是编写一个IDEA插件来执行此操作 . 您可以通过从存储库下载插件或从github下载代码来避免这种情况 . 以下代码片段将按顺序执行“编译”和“检查代码”操作 .

    public class BuildAndLint extends AnAction {
        public void actionPerformed(AnActionEvent e) {
            String[] actions = {"CompileProject", "InspectCode"};
            for(String action: actions) {
                AnAction anAction = ActionManager.getInstance().getAction(action);
    
                DataContext context = e.getDataContext(); 
                AnActionEvent anActionEvent = new AnActionEvent(null, context, "", anAction.getTemplatePresentation(), ActionManager.getInstance(), 0);
    
                anAction.actionPerformed(anActionEvent);
            }
        }
    }
    

    该代码已经过测试,可在Android Studio 1.3中使用 . 它将打开一个窗口来选择要检查的内容,而不是完成所有操作 .

    Links

    Source code on github

    Built and Lint Jar

  • 12

    Lint 应该在 Android Studio 中运行,除非您已通过 build.gradle 文件中的 lintOptions 将其配置为关闭 .

    这是来自http://developer.android.com/tools/debugging/improving-w-lint.html#studio的文档

    在Android Studio中,配置的lint和IDE检查会在您构建应用程序时自动运行 . IDE检查与lint检查一起配置,以运行IntelliJ代码检查以简化代码审查 . 注意:要查看和修改检查严重性级别,请使用“文件”>“设置”>“项目设置”菜单打开“检查配置”页面,其中包含支持的检查列表 . 使用Android Studio,您还可以为特定的构建变体或build.gradle文件中的所有构建变体运行lint检查 . 将lintOptions属性添加到构建文件中的android设置 . Gradle构建文件中的此代码段显示如何将quiet选项设置为true,将abortOnError选项设置为false . android {
    lintOptions {
    //设置为true以关闭lint的分析进度报告
    安静的
    //如果为true,请在发现错误时停止gradle构建
    abortOnError为false
    //如果为true,则仅报告错误
    ignoreWarnings是的
    }
    ...
    }
    要在Android Studio中手动运行检查,请从应用程序或右键单击菜单中选择“分析”>“检查代码” . 将出现“指定检查范围”对话框,以便您指定所需的检查范围和配置文件 .

    以下是您可以添加到gradle build.gradle文件中 lintOptions 块的其他lint选项:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Lint-support

    这里有关于android lint的更多信息:http://developer.android.com/tools/help/lint.html

    它可以说是你可以在android studio中的某些操作后添加gradle任务 .

    • 打开屏幕右侧的Gradle选项卡

    • 选择您的任务

    Android Studio Gradle Tab

    • 右键单击任务

    • 选择执行操作

    Android Studio Execution Action

    这应该在计划执行时运行任务 .

  • 1

    我之前通过添加一个pre-push git hook来完成此操作,该钩子将在push上自动运行lint,如果找到Lint错误则无法推送 . 预推钩脚本存储在Android项目仓库中,并通过gradle自动安装到用户的本地计算机上 .

    安装-混帐hooks.gradle

    def hookDest = new File("${rootProject.rootDir}/.git/hooks/pre-push")
    def prePushHook = new File("${rootProject.rootDir}/pre-push")
    
    task installGitHooksTask(type: Copy) {
        hookDest.delete()
        hookDest << prePushHook.text
    }
    
    task gitExecutableHooks() {
        Runtime.getRuntime().exec("chmod -R +x ${hookDest}");
        println "gitExecutableHooks"
    }
    
    gitExecutableHooks.dependsOn installGitHooksTask
    

    比你的应用build.gradle

    apply from: rootProject.file('gradle/install-git-hooks.gradle')
    

    前推

    #!/bin/sh
    #
    # This hook is for Android project git repos.
    #
    # You can use git config variables to customize this hook.
    # -----------------------------------------------------------
    # Change hooks.lintTargetDirectory to point at a non . directory
    # Change hooks.lintArgs to add any custom lint arguments you prefer
    
    # Get custom info
    dirToLint=$(git config hooks.lintTargetDirectory)
    lintArgs=$(git config hooks.lintArgs)
    projectDir=$(git rev-parse --show-toplevel)
    lintReportPath="/app/lint-report.html"
    
    # If user has not defined a preferred directory to lint against, make it .
    if [ -z "$dirToLint"]
      then
      dirToLint="."
    fi
    
    # Perform lint check
    echo "Performing pre-commit lint check of ""$dirToLint"
    ./gradlew lint
    lintStatus=$?
    
    if [ $lintStatus -ne 0 ]
    then
      echo "Lint failure, git push aborted."
      echo "Open ${projectDir}${lintReportPath} in a browser to see Lint Report"
      exit 1
    fi
    
    exit $lintStatus
    

相关问题