每当我构建Android应用程序时,如何执行checkstyle

时间:2016-07-29 13:02:31

标签: android gradle checkstyle

我试图将checkstyle与Android项目集成 - 我的build.gradle位于下方。我基本上会看到警告,这些警告标识了缺少构建文档的代码。使用此配置,我看到一个名为checkstyle的gradle任务,我可以手动执行,但在重建项目时没有调用它(即使我右键单击任务并说重建执行')

我必须遗漏一些东西,因为似乎其他人遇到了完全相反的问题,并试图阻止它在构建上运行。我究竟做错了什么?



// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
    apply plugin: 'checkstyle'

    task checkstyle(type: Checkstyle) {
        configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
        source 'src'
        include '**/*.java'
        exclude '**/gen/**'

        reports {
            xml.enabled = true
        }

        classpath = files()
    }
}

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




2 个答案:

答案 0 :(得分:5)

看来我找到了自己问题的答案 - here

首先在根级别创建任务

allprojects {
   repositories {
       jcenter()
   }

   task checkstyle(type: Checkstyle) {
       showViolations = true
       configFile file("../settings/checkstyle.xml")

       source 'src/main/java'
       include '**/*.java'
       exclude '**/gen/**'
       exclude '**/R.java'
       exclude '**/BuildConfig.java'

       // empty classpath
       classpath = files()
   }
}

然后在模块级别添加依赖项。这些只是附加在项目的现有build.gradle文件的末尾。

apply plugin: 'checkstyle'

preBuild.dependsOn('checkstyle')
assemble.dependsOn('lint')
check.dependsOn('checkstyle')

答案 1 :(得分:3)

几个月前我不得不这样做......经过大量的研究和寻找。

apply plugin: 'checkstyle'

task checkstyle(type: Checkstyle) {
    // Cleaning the old log because of the creation of the new ones (not sure if totaly needed)
    delete fileTree(dir: "${project.rootDir}/app/build/reports")
    source 'src'
    include '**/*.java'
    exclude '**/gen/**'
    // empty classpath
    classpath = files()
    //Failing the build
    ignoreFailures = false
}

checkstyle {
    toolVersion = '6.18'
}

project.afterEvaluate {
    preBuild.dependsOn 'checkstyle'
}

这个很重要的部分很重要。 preBuild是每次构建时执行的第一个任务,但是在gradle运行之前它是不可见的,所以你需要.afterEvaluate。使用这种checkstyle是第一个运行的东西。在上面的代码中,您可以将ignorefailures设置为true,如果检查的严重性设置为Error,则它将使构建失败,如果只有warrnings则不会。

BTW这需要在模块gradle文件中,例如build.gradle(Module:app)

相关问题