使用 android gradle 插件 3.4.0 定义 Checkstyle 任务

Define Checkstyle Task with android gradle plugin 3.4.0

我将 build.gradle 文件升级为 android gradle 插件 3.4.0 gradle 5.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: Build failed with an exception.

  • Where: Build file '/Users/cs/Development/project/app/build.gradle'

  • What went wrong: A problem occurred evaluating project ':app'.

    Could not find method destination() for arguments [build/outputs/reports/checkstyle-results.xml] on Report xml of type org.gradle.api.reporting.internal.TaskGeneratedSingleFileReport.

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

  • https://help.gradle.org

  • 获取更多帮助

1 秒内构建失败错误:Gradle 未找到 DSL 方法:'destination()' 可能的原因:项目 'project' 可能正在使用 Android Gradle 插件版本不包含 方法(例如 'testCompile' 是在 1.1.0 中添加的)。升级插件到 版本 3.4.0 和同步项目

项目 'project' 可能使用的是 Gradle 不包含该方法。打开 Gradle 包装文件

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

错误指向这一行:

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

destination 的语法是否已在 gradle 中更改?

解决方案

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-