【问题标题】:Run android checkstyle on all modules在所有模块上运行 android checkstyle
【发布时间】:2019-01-21 06:44:45
【问题描述】:

我有一个包含多个库的 android 项目。我想对所有源代码运行 checkstyle 任务。项目结构:

app (com.android.application),
lib1 (com.android.library),
lib2 (com.android.library),
... 

我遵循了这个配置教程:

https://github.com/Piasy/AndroidCodeQualityConfig

项目的 build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

subprojects {
    apply from: "$rootProject.projectDir/quality.gradle"

    afterEvaluate {
        check.dependsOn 'checkstyle'
    }
}

quality.gradle

apply plugin: 'checkstyle'

checkstyle {
    toolVersion '7.4'

    configFile file("${project.rootDir}/checkstyle/checkstyle.xml")
    configProperties.checkstyleSuppressionFilterPath = file(
            "${project.rootDir}/checkstyle/suppressions.xml")
            .absolutePath
}

task checkstyle(type: Checkstyle, group: 'verification') {
    source 'src'
    include '**/*.java'
    exclude '**/gen/**'
    exclude '**/test/**'
    exclude '**/androidTest/**'
    exclude '**/R.java'
    exclude '**/BuildConfig.java'
    classpath = files()
}

如果我对根项目运行 gradle 检查,它只会在 :app 模块上运行,而不是整个项目。

我错过了什么?谢谢。

【问题讨论】:

    标签: android module android-gradle-plugin checkstyle


    【解决方案1】:

    这可能不是您正在寻找的答案,但我的解决方案是添加

    apply from: rootProject.file("gradle/quality.gradle")
    

    到我想在其上运行 checkstyle 的每个模块的 build.gradle。就我而言,有一两个模块是我不希望在其上运行的。

    这是我的 quality.gradle 文件

    apply plugin: "checkstyle"
    
    checkstyle {
        configFile rootProject.file('checkstyle.xml')
        ignoreFailures false
        showViolations true
        toolVersion = "8.15"
    }
    
    /** Checkstyle task for new files (not in exclude list). Fail build if a check fails **/
    task checkstyle(type: Checkstyle) {
        configFile rootProject.file('checkstyle/checkstyle.xml')
    
        //fail early
        ignoreFailures false
        showViolations true
    
        source 'src'
        include '**/*.java'
        exclude rootProject.file('checkstyle/checkstyle-exclude-list.txt') as String[]
        classpath = files()
    }
    
    /** Checkstyle task for legacy files. Don't fail build on errors **/
    task checkstyleLegacy(type: Checkstyle) {
        configFile rootProject.file('checkstyle.xml')
    
        ignoreFailures true
        showViolations true
    
        source 'src'
        include '**/*.java'
        exclude '**/gen/**'
        classpath = files()
    }
    
    afterEvaluate {
        preBuild.dependsOn 'checkstyle'
    }
    

    【讨论】:

    • 谢谢,我自己解决了这个问题,但这个解决方案对我来说似乎也不错。
    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 2012-11-05
    • 2017-03-20
    • 2015-07-28
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 2019-03-17
    相关资源
    最近更新 更多