简体   繁体   English

如何创建特定于风味的 Android Lint 基线文件

[英]How to create flavor specific Android Lint baseline file

As per https://developer.android.com/studio/write/lint.html#snapshot we can create a Lint warning baseline file.根据https://developer.android.com/studio/write/lint.html#snapshot,我们可以创建一个 Lint 警告基线文件。 The problem is that I have multiple flavors, each having their own sourceSets.问题是我有多种口味,每种口味都有自己的 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?有没有人设法生成特定于风味的 lint 基线文件? And if so how?如果是这样怎么办?

Thanks!谢谢!

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我正在尝试同样的事情并找到了一种方法。这为发布和调试创建了差异文件。您可以将自定义逻辑放在 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"
}

I couldn't make the above answer exactly work as I got errors when trying to define the method in the build.gradle file.我无法使上述答案完全有效,因为我在 build.gradle 文件中定义方法时遇到错误。

Using himanshu saluja 's answer this is what worked for me.使用Himanshu saluja的回答这对我有用

My root project's gradle file has:我的根项目的 gradle 文件有:

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: android 块中子项目的 gradle 文件使用如下值:

 lintOptions {
    baseline file(rootProject.ext.getLintFileName)
    checkDependencies true
    abortOnError true
    absolutePaths false
}

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.鉴于baseline功能在LintOptions 上,而这个功能是 AFAIK 无法识别变体的,这将无法开箱即用。

You could file a feature request on https://b.android.com though.不过,您可以在https://b.android.com上提交功能请求。

according to my GitHub sample code :根据我的 GitHub 示例代码

1- add the following function to your app-level build.gradle file: 1- 将以下函数添加到您的应用程序级 build.gradle 文件中:

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.您可以通过更改“fileName”变量来自定义文件名。

2- add the following line to lintOption scop of your app-level build.gradle file: 2- 将以下行添加到应用程序级 build.gradle 文件的 lintOption 范围:

 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. 3- 为每个构建变体运行 linter。 for example type the following command in the terminal tab of Android studio in the root of project:例如,在项目根目录的 Android Studio 的终端选项卡中键入以下命令:

gradlew app:lintMyAppRelease

app = your module name app = 你的模块名称

MyAppRelease = your build varient MyAppRelease = 您的构建MyAppRelease

4- Done 4-完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM