关于android:如何使用Lint Option StopShip使Grade Release版本失败?

How to make Grade release build fail using Lint Option StopShip?

我已经阅读了很多有关StopShip Android Lint Check和Gradle支持的内容

  • http://tools.android.com/tips/lint-checks
  • http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Lint-support
  • http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.LintOptions.html#com.android.build.gradle.internal.dsl.LintOptions:checkReleaseBuilds
  • Android Lint使用Gradle启用检查
  • gradle构建失败在皮棉任务上
  • http://developer.android.com/tools/help/lint.html
  • http://developer.android.com/tools/debugging/improving-w-lint.html

我想使用SO此处已经提到的一些内容,而不是TODO或FIXME注释,而是使用它来确保用于开发/调试/测试的代码块不生产。

为此,我想做两件事:
-启用StopShip检查,因为默认情况下它处于禁用状态
-将严重性从警告(默认)更改为错误

(假设我们在gradle配置中使用abortOnError true)。

我无法实现这一目标!如果我在代码中添加// STOPSHIP注释,无论我尝试了什么,android build都不会失败。这很奇怪,因为在textEditor中将其突出显示为错误,并且如果我运行Lint检查(分析>检查代码...),则将其列为问题之一。

这是我在build.gradle中尝试过的方法

1
2
3
4
5
6
7
8
lintOptions {
    checkReleaseBuilds true
    // Or, if you prefer, you can continue to check for errors in release builds,
    // but continue the build even when errors are found:
    abortOnError true
    enable 'StopShip'
    error 'StopShip'
}

我还尝试在"文件">"设置">"项目设置">"检查"(或在Mac上," Android Studio">"首选项">"检查")中更改我的Android Studio首选项。在这里,我检查了Code contains STOPSHIP marker并将严重性更改为错误,但仍然没有任何反应。

这是我的lint.xml的样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="BackButton" severity="warning" />
    <issue id="EasterEgg" severity="warning" />
    <issue id="FieldGetter" severity="warning" />
    <issue id="IconExpectedSize" severity="warning" />
    <issue id="RtlCompat" severity="error" />
    <issue id="RtlEnabled" severity="warning" />
    <issue id="RtlHardcoded" severity="warning" />
    <issue id="SelectableText" severity="warning" />
    <issue id="StopShip" severity="error" />
    <issue id="TypographyQuotes" severity="warning" />
    <issue id="UnusedIds" severity="warning" />
</lint>

我终于破解了! fatal 'StopShip'。 终于做到了! 留下我的发现以防万一。

在我的build.gradle配置中用fatal 'StopShip'替换error 'StopShip'解决了该问题。

我不完全理解为什么以前使用error 'StopShip'的尝试不起作用,因为abortOnError文档明确指出:

Whether lint should set the exit code of the process if errors are found

并且我将StopShip检查严重性标记为错误。 看来abortOnError只会使Gradle构建因致命错误而中止。 谁能确认?

当然,如果有人提供更好的解决方案/解释,请分享。