How to make Grade release build fail using Lint Op

2020-05-23 08:28发布

问题:

I've read a lot about the StopShip Android Lint Check and Gradle support for it

  • 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 enable checks with gradle
  • gradle build fails on lint task
  • http://developer.android.com/tools/help/lint.html
  • http://developer.android.com/tools/debugging/improving-w-lint.html

I would like to use as some here in SO have already mentioned, instead of a TODO or FIXME comment, use it to ensure a code block intended for development/debugging/testing does not reach production.

For this, I would like to do 2 things: - enable StopShip check, as it is disable by default - change severity from warning (default) to error

(assuming that we use abortOnError true on our gradle config).

I am failing to achieve this! No matter what I tried android build does not fails if I add a // STOPSHIP comment in my code. Which is odd, since in the textEditor its highlighted as an error and if I run a Lint check (Analyze > Inspect Code...) it is listed as one of the issues.

Here's what I've tried in my build.gradle

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'
}

I have also tried to change my Android Studio preferences in File > Settings > Project Settings > Inspections (or Android Studio > Preferences > Inspections on Mac). Here I checked the Code contains STOPSHIP marker and changed the severity to error but still nothing.

Here's what my lint.xml looks like:

<?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>

回答1:

I finally cracked it! fatal 'StopShip'. That's what finally did it! Leaving my discovery in case it helps anyone.

Replacing error 'StopShip' with fatal 'StopShip' in my build.gradle config solved the problem.

I don't fully understand why my previous attempts with error 'StopShip' didn't work, as the abortOnError docs clearly state:

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

and I was marking the StopShip check severity as error. It looks like abortOnError will only make the Gradle build abort for FATAL errors. Can anyone confirm?

Of course, if it anyone offers a better solution/explanation, please do share.