简体   繁体   English

获取Android gradle插件和checkstyle协同工作/命令行用法

[英]Get Android gradle plugin & checkstyle working together / command line usage

I'm evaluating the ability of the new gradle-based build system to reproduce our current ant-based build process and, as a gradle beginner, I failed to get checkstyle running with the android gradle plugin. 我正在评估新的基于gradle的构建系统重现我们当前基于ant的构建过程的能力,并且作为一个初学者,我没有使用android gradle插件运行checkstyle。

Environment: 环境:

  • gradle 1.6 running fine on a standard java project (checkstyle check target included) gradle 1.6在标准java项目上正常运行(包括checkstyle检查目标)

  • up-to-date android SDK (22.0.1 with platform tools and build tools 17) 最新的Android SDK(带平台工具和构建工具的22.0.1)

  • no eclipse, no android studio, only my lovely terminal 没有日食,没有安卓工作室,只有我可爱的终端

Symptom: 症状:

The target project is https://github.com/nibua-r/LigoTextDemo and I succeeded to build it using gradle but if I naively add apply plugin: checkstyle to my build.gradle : 目标项目是https://github.com/nibua-r/LigoTextDemo ,我成功使用gradle构建它,但如果我天真地添加apply plugin: checkstyle到我的build.gradle

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:0.4.2'
  }
}
apply plugin: 'android'
apply plugin: 'checkstyle'

android {
  buildToolsVersion '17'
  compileSdkVersion 15
  testBuildType 'debug'

  defaultConfig {
    versionCode = 1
    versionName = '1.0'
    minSdkVersion 12
    targetSdkVersion 15
  }

  buildTypes {
    debug {
      packageNameSuffix = '.debug'
    }
  }
}

then gradle check doesn't even complain on not finding the checkstyle.xml file (at the default config/checkstyle location) and returns: 然后gradle check甚至没有找到checkstyle.xml文件(在默认的config/checkstyle位置)并且返回:

:check UP-TO-DATE

BUILD SUCCESSFUL

What's needed: 需要什么:

First, I just need a running checkstyle target. 首先,我只需要一个运行的checkstyle目标。 Then, I need to automate checkstyle running as a dependency of the compilation (but lets get the chekstyle target up and running first). 然后,我需要将checkstyle运行自动化作为编译的依赖项(但是让我们首先启动并运行chekstyle目标)。

Assumption: 假设:

This may be related to the fact that (from the [user guide][1]): 这可能与(来自[用户指南] [1])的事实有关:

The Android plugin […] uses its own sourceSets Android插件[...]使用自己的sourceSets

but I'm not enough gradle-efficient to understand what I'm missing there. 但是,我不足以理解我在那里缺少的东西。 Please, gradle Master, enlighten me with your valuable knowledge! 请高手掌握,用你宝贵的知识启发我!

I got pmd, findbugs, and checkstyle working with Gradle 1.12 android plugin 0.12.+ using the following script: 我得到pmd,findbugs和checkstyle使用Gradle 1.12 android插件0.12。+使用以下脚本:

apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'pmd'

check.dependsOn 'checkstyle', 'findbugs', 'pmd'

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

    classpath = files()
}

task findbugs(type: FindBugs) {
    ignoreFailures = true
    effort = "max"
    reportLevel = "high"
    excludeFilter = new File("${project.rootDir}/config/quality/findbugs/findbugs-filter.xml")
    classes = files("$project.buildDir/intermediates/classes/")

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    reports {
        xml {
            destination "$project.buildDir/reports/findbugs/findbugs.xml"
            xml.withMessages true
        }
    }

    classpath = files()
}

task pmd(type: Pmd) {
    ruleSetFiles = files("${project.rootDir}/config/quality/pmd/pmd-ruleset.xml")
    ignoreFailures = true
    ruleSets = ["basic", "braces", "strings"]

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    reports {
        xml.enabled = true
        html.enabled = false
    }
}

Running gradle build in command line will run all code quality plugins and generate xml reports in app/build/reports/ which are then ready to be viewed or parsed by CI tools. 在命令行中运行gradle build将运行所有代码质量插件,并在app / build / reports /中生成xml报告,然后CI工具可以查看或解析这些报告

Someone has a great answer to solve integrating PMD, findbugs and checkstyle with Gradle for Android. 有人解决了使用Gradle for Android集成PMD,findbugs和checkstyle的好方法。 Unfortunately, the only solution for now is based on ant : http://sethrylan.org/2013/07/14/gradle-android-findbugs.html 不幸的是,目前唯一的解决方案是基于ant: http//sethrylan.org/2013/07/14/gradle-android-findbugs.html

I wish gradle will one day allow to do as much as maven for Android. 我希望gradle有一天能够像Android一样多做maven。

--- Update as of October 2013 ---自2013年10月起更新

With Gradle 1.8 and Android plugin for Gradle 0.6.+, you don't need this anymore. 使用Gradle 1.6。+ Gradle 1.6。+的Android插件,你不再需要它了。 Android sourcesets and configurations are now compatible with the java plugin and all quality plugin work out of the box. Android源码集和配置现在与java插件兼容,所有质量插件都可以开箱即用。

This includes pmd, findbugs, checkstyle and classycle. 这包括pmd,findbugs,checkstyle和classycle。

--- Update A configuration, largely inspired from the project mentioned above, is proposed in this open source project as well, plus other tools. ---更新在这个开源项目中 ,还提出了一个很大程度上受上述项目启发的配置,以及其他工具。

To get this to work with my Android project, I had to declare the task explicitly. 为了使这与我的Android项目一起使用,我必须明确声明任务。

Here's what worked for me: 这对我有用:

apply plugin: 'checkstyle'

task checkstyle(type: Checkstyle) {
    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    // empty classpath
    classpath = files()
}

Be mindful that the Android plugin may choose to create a task of the same name in the future or work in conjunction with the checkstyle plugin in different ways. 请注意,Android插件可能会选择在将来创建同名的任务,或者以不同的方式与checkstyle插件一起使用。

You can try Android Check plugin ( https://github.com/noveogroup/android-check ): 您可以尝试Android Check插件( https://github.com/noveogroup/android-check ):

  • Checkstyle Checkstyle的
  • PMD PMD

Configuration: 组态:

buildscript {
    repositories { jcenter() }
    dependencies {
        ...
        classpath 'com.noveogroup.android:check:+'
        ...
    }
}

apply plugin: 'com.noveogroup.android.check'

You can use hardcore configuration: 您可以使用硬核配置:

check {
    abortOnError true
    checkstyle { config hard() }
    pmd { config hard() }
}

I found by digging on the web that the Android plugin depends on java-base and not java (ie the sourceSets management is specific) and the checkstyle plugin rely on java. 我发现通过在网上挖掘Android插件依赖于java-base而不是java(即sourceSets管理是特定的),checkstyle插件依赖于java。 As a consequence, some gradle upstream modification are needed to get the thing done. 因此,需要一些gradle上游修改才能完成任务。 The gradle team is working on that, as seen on twitter: gradle团队正在努力,正如在twitter上看到的那样:

@anzix Android source sets will be soon understood by the generic code quality plugins. @anzix Android源集很快就会被通用代码质量插件所理解。 Work on that has already started. 关于这方面的工作已经开始。

— Gradle Build System (@Gradleware) May 26, 2013 - Gradle Build System(@ Gradleware) 2013年5月26日

看看Soter Gradle插件,可以无缝地添加对Findbugs,Checkstyle和PMD的支持到Android项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM