我想在Android Studio中为发布和调试构建类型使用不同的 lint.xml 文件。那么,如何实现呢?
当我将 lint.xml 放在 someModule / src / debug 和 someModule / src / release 文件夹中时(同样,这些文件夹仅包含那个 lint.xml 文件,没有更多)gradle作出反应,因为没有任何 lint.xml 文件,如果我放置单个 lint.xml 文件在核心模块文件夹下(someModule /) - gradle识别它,但在这种情况下我不能使用不同的设置,具体取决于构建类型......
答案 0 :(得分:4)
我没有尝试过,但也许这样的事情可以帮到你。
tasks.whenTaskAdded { task ->
if (task.name == 'lintDebug') {
task.ext.lintXmlFileName = "lint-debug.xml"
} else if (task.name == 'lintDemo') {
task.ext.lintXmlFileName = "lint-demo.xml"
}
}
编辑:评论反馈:
答案 1 :(得分:3)
这对我有用:
barmax
在我的情况下,我需要为版本构建启用tasks.whenTaskAdded { task ->
if (task.name.startsWith("lint")) {
if (task.name.toLowerCase().endsWith("release")) {
task.doFirst {
android.lintOptions.abortOnError = true
}
} else {
task.doFirst {
android.lintOptions.abortOnError = false
}
}
}
}
,以便我可以自由开发但在我的CI上快速捕获lint错误(如果它们滑落)。
答案 2 :(得分:0)
使用kotlin构建脚本(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"))
}
}
}
}
答案 3 :(得分:-1)
这是Android studio新版系统指南lint support的摘要。
Lint支持
从版本0.7.0开始,您可以为特定变体或所有变体运行lint,在这种情况下,它会生成一个报告,描述给定问题适用的特定变体到。
您可以通过添加lintOptions部分来配置lint,如下所示。您通常只指定其中的一些;此部分显示所有可用选项。
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'
}
}
编辑:添加真实可行的示例
众所周知,新的Android构建系统基于gradle。 gradle构建系统的核心组件是task
。如果您的项目具有不同的构建变体,则有不同的lint任务。您可以从android studio All task list
或命令行./gradlew tasks
获取这些任务。示例如下所示,两个构建版本demo
和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
这些lint任务依赖于其他任务,这里假设preBuild
。
在运行lint任务之前,它将首先运行任务preBuild
。任务preBuild
是已存在的任务,但我们可以操作此预定义任务并为此任务添加更多操作。将根据不同的构建变体动态添加和修改android lintOptions
属性,如下面的代码在文件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...
}
为了成功运行上面的代码,相应的lint配置文件必须存在于app目录下。