Rob*_*ert 38 android gradle android-lint android-studio
当我用android studio构建项目时,我希望能够运行lint任务,以确保遵循lint规则.
我尝试过使用任务依赖但没有运气.我的teamcity构建服务器使用运行lint任务的构建任务,因此效果很好.但是,当我选择调试版本变体时,android studio似乎可以互换使用generateDebugSources和compileDebugJava任务.
这是我在build.gradle中尝试过的:
assemble.dependsOn lint
Run Code Online (Sandbox Code Playgroud)
Ros*_*ick 35
如果您只想将Android Studio项目配置为在默认运行配置之前运行lint检查,而不影响您的gradle任务的配置方式,则可以按照以下步骤操作.



:app:check)
check在现有Gradle-aware make步骤之前移动新步骤
现在,如果发生任何lint错误,Android Studio将运行lint检查并使构建失败.
And*_*ble 32
要破坏lint并分析你的项目,只需选择Analyze > Inspect Code.
你应该得到一个包含所有问题的好窗口.

另请参阅Android Studio中的运行lint以获取更多信息.
我做了一些研究,试着把它添加到你的build.gradle.
lintOptions {
abortOnError true
}
Run Code Online (Sandbox Code Playgroud)
有许多选项可以应用于build.gradle
Yoe*_*der 15
要在build.gradle中执行此操作,请将以下行添加到build.gradle:
android {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def lintTask = tasks["lint${variant.name.capitalize()}"]
output.assemble.dependsOn lintTask
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
这使得所有组装任务都依赖于在Android Studio执行的每个汇编调用之前有效运行它们的lint任务.
编辑
使用Android Gradle Plugin 3.3和Gradle 5.x这是使用Kotlin脚本的修订版:
applicationVariants.all {
val lintTask = tasks["lint${name.capitalize()}"]
assembleProvider.get().dependsOn.add(lintTask)
}
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案,当您在 Android Studio 中单击Build - Make Project时也可以使用该解决方案:
android {
..
afterEvaluate {
applicationVariants.all {
variant ->
// variantName: e.g. Debug, Release
def variantName = variant.name.capitalize()
// now we tell gradle to always start lint after compile
// e.g. start lintDebug after compileDebugSources
project.tasks["compile${variantName}Sources"].doLast {
project.tasks["lint${variantName}"].execute()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想强制Android Studio项目在默认运行配置之前运行lint检查,而不影响gradle任务的配置方式,并且想要在gradle构建系统中执行此操作,则可以在代码块外部添加以下代码android块在应用程序模块的build.gradle中,如下所示:
android {
....
lintOptions {
abortOnError true
}
}
tasks.whenTaskAdded { task ->
if (task.name == 'compileDevDebugSources') {
task.dependsOn lint
task.mustRunAfter lint
}
}
Run Code Online (Sandbox Code Playgroud)
替换compileDevDebugSources 为您已经定义的所需构建版本,例如。compileReleaseSources,compileDebugSources,compileStagingDebugSources,等。
经过测试,可以在Android Studio 3.0上运行
| 归档时间: |
|
| 查看次数: |
24032 次 |
| 最近记录: |