Different lint.xml for different build types in Android Studio?

前端 未结 4 1198
[愿得一人]
[愿得一人] 2021-02-13 13:39

I want to use different lint.xml files for release and debug build types in Android Studio. So, how can this be achieved?

When

4条回答
  •  太阳男子
    2021-02-13 14:12

    This is summary from Android studio new build system guide, lint support.

    Lint support

    As of version 0.7.0, you can run lint for a specific variant, or for all variants, in which case it produces a report which describes which specific variants a given issue applies to.

    You can configure lint by adding a lintOptions section like following. You typically only specify a few of these; this section shows all the available options.

    android {
    
        lintOptions {
    
            // set to true to turn off analysis progress reporting by lint
            quiet true
            // if true, stop the gradle build if errors are found
            abortOnError false
            // if true, only report errors
            ignoreWarnings true
            // if true, emit full/absolute paths to files with errors (true by default)
            //absolutePaths true
            // if true, check all issues, including those that are off by default
            checkAllWarnings true
            // if true, treat all warnings as errors
            warningsAsErrors true
            // turn off checking the given issue id's
            disable 'TypographyFractions','TypographyQuotes'
            // turn on the given issue id's
            enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
            // check *only* the given issue id's
            check 'NewApi', 'InlinedApi'
            // if true, don't include source code lines in the error output
            noLines true
            // if true, show all locations for an error, do not truncate lists, etc.
            showAll true
            // Fallback lint configuration (default severities, etc.)
            lintConfig file("default-lint.xml")
            // if true, generate a text report of issues (false by default)
            textReport true
            // location to write the output; can be a file or 'stdout'
            textOutput 'stdout'
            // if true, generate an XML report for use by for example Jenkins
            xmlReport false
            // file to write report to (if not specified, defaults to lint-results.xml)
            xmlOutput file("lint-report.xml")
            // if true, generate an HTML report (with issue explanations, sourcecode, etc)
            htmlReport true
            // optional path to report (default will be lint-results.html in the builddir)
            htmlOutput file("lint-report.html")
    
           // set to true to have all release builds run lint on issues with severity=fatal
    
           // and abort the build (controlled by abortOnError above) if fatal issues are found
    
           checkReleaseBuilds true
    
            // Set the severity of the given issues to fatal (which means they will be
            // checked during release builds (even if the lint target is not included)
            fatal 'NewApi', 'InlineApi'
            // Set the severity of the given issues to error
            error 'Wakelock', 'TextViewEdits'
            // Set the severity of the given issues to warning
            warning 'ResourceAsColor'
            // Set the severity of the given issues to ignore (same as disabling the check)
            ignore 'TypographyQuotes'
        }
    
    }
    

    EDIT: add the real and workable example

    As we all know, the new Android build system is based on gradle. The core component of the gradle build system is task. There are different lint tasks if you project has different build variant. You can get those tasks from android studio All task list, or from commandline ./gradlew tasks. A example show as following, two build flavors demo and full.

    lint - Runs lint on all variants.
    lintDemoDebug - Runs lint on the DemoDebug build
    lintDemoRelease - Runs lint on the DemoRelease build
    lintFullDebug - Runs lint on the FullDebug build
    lintFullRelease - Runs lint on the FullRelease build
    

    These lint task are dependency on other tasks, here let's say preBuild.

    Before run the lint task, it will run the task preBuild firstly. The task preBuild is an already existing task, but we can manipulate this pre-defined task and add more action to this task. android lintOptions property will be added and modified dynamically based on different build variants as the following code demonstrate in the file app/build.gradle.

    preBuild.doFirst {
    android.applicationVariants.each { variant ->
        if (variant.name == 'demoDebug') {
            println variant.name
            android.lintOptions.quiet = true
            android.lintOptions.lintConfig = new File('app/lint_demo_debug.xml')
            // you can add more properties
        } else if (variant.name == 'fullDebug') {
            println variant.name
            android.lintOptions.quiet = false
            android.lintOptions.lintConfig = new File('app/lint_full_debug.xml')
            // you can add more properties
        } // more variants...
    }
    

    In order to run the code above successfully, the corresponding lint configuration file must exist under app directory.

提交回复
热议问题