4

As per https://developer.android.com/studio/write/lint.html#snapshot we can create a Lint warning baseline file. The problem is that I have multiple flavors, each having their own sourceSets. Some files are used in a single flavor.

When I generate the baseline file, it's always specific to a variant. Which means that it's invalid for the other variants, ie it will miss some existing issues.

I have tried putting the

lintOptions {
  baseline file("lint-baseline.xml")
}

in the build and flavor blocks, but it won't generate multiple baselines.

Has anyone managed to generate flavor specific lint baseline file? And if so how?

Thanks!

Fabien Demangeat
  • 935
  • 8
  • 12

4 Answers4

2

I was trying the same thing and found a way of doing it.This create diff file for release and debug.You can put your custom logic in getfileName

lintOptions {
        baseline file(getFileName())
        checkAllWarnings true
        warningsAsErrors true
        abortOnError true
    }

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().args.toString()
    println tskReqStr
    if (tskReqStr.contains("Debug")) {
        return "debug"
    } else {
        return "release"
    }
}

private String getFileName(String command) {
    return getCurrentFlavor() + "-lint-baseline.xml"
}
  • 1
    I don't think this will work if you use multiple gradle commands at once. For instance `gradle lintDebug lintRelease`. – BajaBob Feb 28 '21 at 14:11
1

I couldn't make the above answer exactly work as I got errors when trying to define the method in the build.gradle file.

Using himanshu saluja's answer this is what worked for me.

My root project's gradle file has:

ext.getLintFileName = {
    Gradle gradle = getGradle()
    String taskReq = gradle.getStartParameter().getTaskRequests().args.toString()
    if(taskReq.contains("Debug")) {
        return "debug-lint-baseline.xml"
    } else {
        return "release-lint-baseline.xml"
    }
}

And the sub project's gradle file inside the android block uses the value like this:

 lintOptions {
    baseline file(rootProject.ext.getLintFileName)
    checkDependencies true
    abortOnError true
    absolutePaths false
}
PKDev
  • 21
  • 4
0

Given that the baseline feature is on LintOptions and this one is AFAIK not capable of being variant aware, this will not work out of the box.

You could file a feature request on https://b.android.com though.

Niklas
  • 23,674
  • 33
  • 131
  • 170
0

according to my GitHub sample code:

1- add the following function to your app-level build.gradle file:

def getPath() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    Pattern pattern
    String path
    String fileName = "lint-baseline"

    if (tskReqStr.contains("assemble"))
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    else if (tskReqStr.contains("generate"))
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
    else if (tskReqStr.contains("lint"))
        pattern = Pattern.compile("lint(\\w+)(Release|Debug)")

    if(pattern != null) {
        Matcher matcher = pattern.matcher(tskReqStr)

        if (matcher.find()) {
            path = matcher.group(1).toLowerCase() + matcher.group(2).toLowerCase()
            return "lint-baselines/${path}-${fileName}.xml"
        } else {
            return "lint-baselines/${fileName}.xml"
        }
    }
    return "lint-baselines/${fileName}.xml"
}

this function creates a specific path for each build variants. you can customize the file name by changing the "fileName" variable.

2- add the following line to lintOption scop of your app-level build.gradle file:

 lintOptions {
        ...
        // Use (or create) a baseline file for issues that should not be reported
        baseline file("${getPath()}")
        ...
    }

3- run linter for each of build varients. for example type the following command in the terminal tab of Android studio in the root of project:

gradlew app:lintMyAppRelease

app = your module name

MyAppRelease = your build varient

4- Done

Iman Dolatkia
  • 186
  • 1
  • 7