2016-07-29 81 views
2

我想整合checkstyle與Android項目 - 我的build.gradle在下面。我想基本上看到警告標識缺少構建文檔的代碼。使用這個配置,我看到一個名爲checkstyle的gradle任務,我可以手動執行,但在重建項目時不會調用它(即使我右鍵單擊該任務並說重建時執行)我怎樣才能執行checkstyle每當我建立一個Android應用程序

我必須缺少因爲它看起來像其他人有完全相反的問題,並試圖阻止它在構建上運行。我究竟做錯了什麼?

// 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 
 
}

+0

你打算存檔什麼?在每個構建上運行checkstyle? – Divers

回答

3

看來我已經找到了答案,以我自己的問題 - 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

我不得不幾個月前做到這一點...大量的研究和照看。

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,則構建將失敗,如果只有warn,則不會。

BTW這需要在例如的build.gradle模塊gradle這個文件(模塊:APP)

+0

我看到了一些解決方案,表明afterEvaluate,但那是拋出另一個錯誤,就好像它是未定義的。我上面發佈的解決方案終於爲我工作。 –

+0

對我來說,它顯示無法解析符號'preBuild',但它的工作。不錯,你找到了自己的解決方案。 – STARGATEBG

相關問題