Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

When I place lint.xml in someModule/src/debug and someModule/src/release folders (also, these folders contain only that lint.xml file and nothing more) gradle reacts as there is no any lint.xml file, If I place single lint.xml file under core module folder (someModule/)- gradle recognizes it, but in this case I can't use different settings depending on build type...

like image 412
Prizoff Avatar asked Apr 30 '14 07:04

Prizoff


People also ask

What is lint XML in Android?

The lint.xml fileA configuration file that you can use to specify any lint checks that you want to exclude and to customize problem severity levels.

What is a build type in Gradle Android?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

What are build variants in Android?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.


Video Answer


2 Answers

With kotlin build scripts (build.gradle.kts):

tasks.withType<LintBaseTask>().configureEach {
    // doFirst is required else we get a Null Pointer Exception on lintOption
    doFirst {
        // This will override the lintOptions from the android extension
        lintOptions.run {
            if (name.toLowerCase().contains("debug")) {
                // Do your configuration here
                // isAbortOnError = true
                // baselineFile = file("baseline.xml")
                // isWarningsAsErrors = true
                // isCheckDependencies = true
                // ignore("MissingTranslation")
                // setLintConfig(file("lint.xml"))
            }
        }
    }
}
like image 186
mbonnin Avatar answered Oct 16 '22 06:10

mbonnin


I did not try it, but maybe something like this could help you.

tasks.whenTaskAdded { task ->
    if (task.name == 'lintDebug') {
        task.ext.lintXmlFileName = "lint-debug.xml"
    } else if (task.name == 'lintDemo') {
        task.ext.lintXmlFileName = "lint-demo.xml"
    }
}

EDIT: comments feedback:

  • task.ext.xxxx is a custom name space for you to use: see https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html
  • "lintXmlFileName" is a made up name. You won't find doc about it.
  • in android{... lintOptions {... lintConfig file("lint.xml")}} you need to read "lintXmlFileName" using ".ext.get("lintXmlFileName")" and set it for "lintConfig file()"
  • I did not test it, but I assume the "whenTaskAdded" goes outside of "android" in your app's build.gradle.
like image 32
Loda Avatar answered Oct 16 '22 05:10

Loda