Is there any way to clear lintOption vauldes defined in build.gradle?

723 views
Skip to first unread message

Ming-Ta Yu

unread,
Feb 14, 2018, 9:44:54 AM2/14/18
to lint-dev
Hi,
If we have a project with lintOptions defined in buld.gradle, is there any way to clear them or change them when doing gradle build, such as doing this in afterProject function in init.gradle?

Thanks a lot!
Ming-Ta

Tor Norbye

unread,
Feb 14, 2018, 9:49:55 AM2/14/18
to lint-dev
It might be possible using Gracle Hacks™, but it's probably a bad idea. What's the use case you're after? Maybe there's something additional we should provide directly from the plugin. (For example, right now lintOptions are not per-variant so you can't have separate settings for debug and release for example; is that something you're trying to do?)

-- Tor

Ming-Ta Yu

unread,
Feb 15, 2018, 3:01:02 AM2/15/18
to lint-dev
Hi Tor,

Thanks for the response. My case is we have several teams working on different Android projects. Some of them define lintOptions in build.gradle but some do not. For those with lintOptions defined in build.gradle, they might ignore something which we might have concern. We use Jenkins do perform the build continuously.  I am thinking if it is possible to have something in init.gradle to override the definition for lintOptions in each of build.gradle file. I tried to write something as the following in init.gradle to re-configure lintOptions in the afterProject callback. It seems to work but I am not sure if this is good or if there is another way to achieve this.

gradle.afterProject { project ->
    gradle.println("gradle.afterProject for the project: ${project.name}")

    if (project.pluginManager.hasPlugin('com.android.application')
            || project.pluginManager.hasPlugin('com.android.library')
            || project.pluginManager.hasPlugin('com.android.test')
            || project.pluginManager.hasPlugin('com.android.feature') ) {
        gradle.println("The Android plugin is applied in the project ${project.name}.")
        gradle.println("Restore android.lintOptions to default values for the project ${project.name} and set them as what we want in common.")
        project.android {
            lintOptions {
                abortOnError false // Keep going even we have errors.
                absolutePaths true
                check.clear()
                checkAllWarnings false
                checkReleaseBuilds true
                disable.clear()
                enable.clear()
                explainIssues true
                if (htmlOutput != null) {
                    htmlOutput file("${project.projectDir.absolutePath}/build/reports/lint-results.html")
                }
                htmlReport true
                ignoreWarnings false
                if (lintConfig != null) {
                    lintConfig file("${project.projectDir.absolutePath}/lint.xml")
                }
                noLines false
                quiet false
                if (severityOverrides != null ) {
                    severityOverrides.each { key, value ->
                        enable(key)
                    }
                }
                showAll false
                if (textOutput != null) {
                    textOutput 'stdout'
                }
                textReport false
                warningsAsErrors false
                if (xmlOutput != null) {
                    xmlOutput file("${project.projectDir.absolutePath}/build/reports/lint-results.xml")
                }
                xmlReport true
            }
        }
    }
}

Thanks a lot!
Ming-Ta

Ming-Ta Yu於 2018年2月14日星期三 UTC+8下午10時44分54秒寫道:

Tor Norbye

unread,
Feb 15, 2018, 12:28:06 PM2/15/18
to Ming-Ta Yu, lint-dev
Forgot to Reply-All:

On Thu, Feb 15, 2018 at 9:27 AM, Tor Norbye <tno...@google.com> wrote:
If I understood this correctly, what you'd really like is a way to let you run lint on a number of Gradle projects and *override* the configuration for each project. 

The simplest way I can imagine this working is one where you either set an environment variable, or define a system property (in gradle.properties), which points to a lint.xml to be used in all projects, e.g.
LINT_OVERRIDE_CONFIG=/path/to/my/master/lint.xml

If you set that up (in your Jenkins job or on the command line before invoking Gradle etc), then lint will look for it and if found, will use *only* that configuration, ignoring anything else found in the project tree (lintOptions, local lint.xml files, etc).

Does that sound about right?
`
The advantage of doing it this way instead of requiring/support some sort of gradle init magic, is that this lets you override targets in a general way without touching the project, running it on arbitrary sub targets etc. I've actually wanted to do this a few times in the past; one of the codebases I run lint on to look for false positives after writing a new check (or broadening an existing one) is the support library, which has a large number of sub projects as well as a bunch of custom lintOptions targets. And I'd like to be able to override all of that and just have it search all projects for all issues.

-- Tor


--
You received this message because you are subscribed to the Google Groups "lint-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lint-dev+unsubscribe@googlegroups.com.
To post to this group, send email to lint...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lint-dev/53ade250-e3d7-4630-b318-95d13e40320c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


Ming-Ta Yu

unread,
Feb 16, 2018, 12:27:48 PM2/16/18
to lint-dev
Hi Tor,

Yes, this is what I want. Then, I can control all the lint options when doing build regardless what are defined in each of the projects. By the way, is option like  abortOnError can also be dfined in lint.xml?

Thanks a lot!
Leslie

Tor Norbye於 2018年2月16日星期五 UTC+8上午1時28分06秒寫道:
Forgot to Reply-All:

To unsubscribe from this group and stop receiving emails from it, send an email to lint-dev+u...@googlegroups.com.

To post to this group, send email to lint...@googlegroups.com.

Tor Norbye

unread,
Mar 12, 2018, 11:36:34 AM3/12/18
to lint-dev
I implemented this recently; I think it's in one of the latest canaries for 3.2.

export LINT_OVERRIDE_CONFIGURATION="/home/tnorbye/dev/android/ub-supportlib-master/frameworks/support/lint.xml"

Here's the file I'm using above to for example turn off all checks except for one that I'm making fatal (I ran this on the support library source code to look for false positives in a check I had recently been tweaking.)

$ cat /home/tnorbye/dev/android/ub-supportlib-master/frameworks/support/lint.xml
<?xml version="1.0" encoding="UTF-8"?>
<lint
    checkAllWarnings='false'
    ignoreWarnings='false'
    warningsAsErrors='false'
    fatalOnly='false'
    checkTestSources='false'
    checkGeneratedSources='true'
    checkDependencies='false'
    explainIssues='true'
    removeFixedBaselineIssues='true'
    abortOnError='true'
    baseline='none'
>
    <issue id="all" severity="ignore" />
    <issue id="WrongViewCast" severity="fatal" />
</lint>

Robert Melo

unread,
Dec 2, 2019, 2:23:42 PM12/2/19
to lint-dev
Tor,

I've implemented an override configuration to ignore all lints, except my predefined list. I've followed your sample here by defining <issue id="all" severity="ignore" />, then added some checks. However, it does not work. Based on the result, it seems all lints were ignored, since the result was No warnings, though I'm sure there were errors. The predefined list just worked when I put severity="ignore" for all +300 checks.

Is that expected? See the lint file below:

<?xml version="1.0" encoding="UTF-8"?>
<lint abortOnError="false" baseline="none" checkAllWarnings="true" checkDependencies="false"
checkGeneratedSources="true" checkTestSources="false" explainIssues="true" fatalOnly="false"
htmlReport="true" ignoreWarnings="false" removeFixedBaselineIssues="true"
warningsAsErrors="false" xmlReport="true">


<issue id="all" severity="ignore" />

    <!-- Sample of predefined enabled checks -->
<issue id="RtlCompat" />
<issue id="RtlEnabled" />
<issue id="RtlHardcoded" />
<issue id="RtlSymmetry" />
</lint>

If I'm not doing correctly, is there a way to define the check scope, without insert ignore tag for the ones I want to ignore.

Thanks in advance,
Robert.
Reply all
Reply to author
Forward
0 new messages