Lint:来自命令行的 abortOnError

Lint: abortOnError from command line

有没有办法从命令行设置abortOnError?不改变 build.gradle:

lintOptions {
  abortOnError false
}

Lint 有 --exitcode 显然应该可以工作的参数,但 ./gradlew lint 不接受任何参数。

错误:

FAILURE: Build failed with an exception.
* What went wrong:
Problem configuring task :app:lint from command line.
> Unknown command-line option '--exitcode'.

lint 文档:

--exitcode: Set the exit code to 1 if errors are found.

看来没有办法了。

荒谬的解决方法是覆盖 build.gradle 并添加 lintOptions { abortOnError false }。工作脚本:

import os
import sys


def is_app_build_gradle(file_path):
    gradle_file = open(file_path)
    file_content = gradle_file.read()

    if file_content.__contains__('android') and file_content.__contains__('signingConfigs'):
        return True
    else:
        return False


def find_build_gradle(project_path):
    name = 'build.gradle'
    walk = os.walk(project_path)
    for root, dirs, files in walk:
        if name in files:
            file_path = os.path.join(root, name)

            if is_app_build_gradle(file_path):
                return file_path

    raise Exception('Not found app build.gradle in ' + project_path)


def write_abort_on_error_false(file_path):
    gradle_file = open(file_path)
    file_content = gradle_file.read()

    if file_content.__contains__('abortOnError false'):
        print 'already false'
        return

    file_content = file_content.replace('android {', 'android { lintOptions {abortOnError false}')
    to_write = open(file_path, "w")
    to_write.write(file_content)
    to_write.close()


if __name__ == '__main__':

    if len(sys.argv) == 1:
        raise Exception('Android project path is not defined')

    project_path = sys.argv[1]
    gradle_file_path = find_build_gradle(project_path)
    write_abort_on_error_false(gradle_file_path)
    print 'success'

用法

python add_abort_on_error.py "path/to/android/project"

这样修改你的lintOptions

lintOptions {
    abortOnError project.getProperties().getOrDefault("abortOnError", false)
}

然后您将能够 运行 通过带有 -PabortOnError=true 参数的命令行进行 lint:

./gradlew lint -PabortOnError=true