首页 > 解决方案 > 使用 android gradle 插件 3.4.0 定义 Checkstyle 任务

问题描述

我使用 gradle将我的build.gradle文件升级到了 android gradle 插件。3.4.05.11

我的build.gradle

apply plugin: 'com.android.application'
apply plugin: 'checkstyle'

android {
    compileSdkVersion 28
    buildToolsVersion buildVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "myapp"
        minSdkVersion 21
        targetSdkVersion 28
        versionName "5.20.0"
        versionCode 520

    }

    dataBinding {
        enabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {    
    implementation "com.android.support:support-v4:$supportLibVersion"
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

task checkstyle(type: Checkstyle) {
    source 'src/'
    include '**/*.java'
    exclude '**/gen/**'
    classpath = files()
    reports {
        xml {
            destination "build/outputs/reports/checkstyle-results.xml"
        }
    }
    group = JavaBasePlugin.VERIFICATION_GROUP
    description = 'Performs checkstyle verification on the code.'
}

task checkstyleReport(dependsOn: 'checkstyle', group: JavaBasePlugin.VERIFICATION_GROUP)  {
    if (file("build/outputs/reports/checkstyle-results.xml").exists()) {
        ant.xslt(in: "build/outputs/reports/checkstyle-results.xml",
                style: "./config/checkstyle/checkstyle.xsl",
                out: "build/outputs/reports/checkstyle-results.html"
        )
    }
}

同步时,我收到以下错误消息:

FAILURE:构建失败并出现异常。

  • 其中:构建文件'/Users/cs/Development/project/app/build.gradle'

  • 出了什么问题:评估项目“:app”时出现问题。

    在 org.gradle.api.reporting.internal.TaskGeneratedSingleFileReport 类型的报告 xml 上找不到参数 [build/outputs/reports/checkstyle-results.xml] 的方法 destination()。

  • 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。

  • 在https://help.gradle.org获得更多帮助

BUILD FAILED in 1s ERROR: Gradle DSL method not found: 'destination()' 可能的原因:项目 'project' 可能正在使用不包含该方法的 Android Gradle 插件版本(例如添加了“testCompile”在 1.1.0 中)。将插件升级到 3.4.0 版本并同步项目

项目“项目”可能正在使用不包含该方法的 Gradle 版本。打开 Gradle 包装文件

构建文件可能缺少 Gradle 插件。应用 Gradle 插件

错误指向这一行:

reports {
        xml {
            destination "build/outputs/reports/checkstyle-results.xml"
        }
    }

destinationgradle 中的语法是否已更改?

标签: androidandroid-gradle-pluginandroid-gradle-3.4.0

解决方案


解决方案

reports {
        xml {
            destination file("build/outputs/reports/checkstyle-results.xml")
        }
    }

该方法用于获取对象,但现在需要文件类型作为参数 - 因此参数错误。所以传入一个文件反而解决了这个问题。

您可以在此处查看预期的参数类型:

https://docs.gradle.org/current/javadoc/org/gradle/api/reporting/ConfigurableReport.html#setDestination-java.io.File-


推荐阅读