2013-06-14 10 views
9

私はテストケースを持つ簡単なアンドロイドプロジェクトを持っています。Android Gradleコードカバレッジ

ProjNameProject 
--build.gradle 
--ProjName 
----build.gradle 

デフォルトでは、アンドロイドの新しいビルドシステムはデフォルトで基本的なテスト結果を提供しています。

今、私はコードカバレッジも見たいと思っています。私はEmmaとAntスクリプトを使ってこれを設定する方法を知っていますが、ここでAntスクリプトを実行したくありません。私は新しいビルドシステムを使って私の目的を破ると思う。

Githubで見つかったいくつかのCoberturaプラグインを試しました。特にワン: https://github.com/stevesaliman/gradle-cobertura-plugin

しかし、私は、私はjavaプラグインに関するエラーが出ProjNameビルドファイルでプラグインを使用しようとします。 tools.android.comでjavaプラグインを追加するとこの動作が生成されます。私はそれを適用していないので、coberturaプラグインが必要です。私はメインのビルドファイルでプラグインを使用しようとすると
、私はJavaのエラーが表示されていないが、今、私は以下を参照してください。

Could not find net.sourceforge.cobertura:cobertura:1.9.4.1. 
    Required by: 
     :ProjNameProject:unspecified 

私は何をしますか?

+0

誰もがアンドロイドプロジェクトでクローバーgradleプラグインを使用しようとしましたか? – SoH

+0

[Android Studioを使用してコードカバレッジを取得する方法]の複製が可能ですか?(http://stackoverflow.com/questions/18683022/how-to-get-code-coverage-using-android-studio) – 030

+2

リンクされた質問が尋ねられました私の約3ヶ月後...現実には私の潜在的な重複です。 – Sababado

答えて

7

Androidのgradleプラグインv0.10(http://tools.android.com/tech-docs/new-build-system)にJaCoCoのサポートが追加されました。

Enable in the tested Build Type with testCoverageEnabled = true 

android { 
    jacoco { 
    version = '0.6.2.201302030002' 
    } 
} 

私はhttp://chrisjenx.com/gradle-robolectric-jacoco-dagger/に従うことによってRobolectricで作業JaCoCoカバレッジを得ることができました。

apply plugin: 'android' 
apply plugin: 'robolectric' 
apply plugin: 'jacoco' 

dependencies { 
    compile fileTree(dir: 'libs', include: '*.jar') 
    compile 'com.android.support:appcompat-v7:19.1.+' 

    androidTestCompile fileTree(dir: 'libs/test', include: '*.jar') 
    androidTestCompile 'junit:junit:4.11' 
    androidTestCompile 'org.robolectric:robolectric:2.3' 
    androidTestCompile 'com.squareup:fest-android:1.0.+' 
} 

robolectric { 
    // Configure the set of classes for JUnit tests 
    include '**/*Test.class' 
    exclude '**/*AbstractRobolectricTestCase.class' 

    // Configure max heap size of the test JVM 
    maxHeapSize = "2048m" 
} 

jacoco { 
    toolVersion = "0.7.1.201405082137" 
} 

//Define coverage source. 
//If you have rs/aidl etc... add them here. 
def coverageSourceDirs = [ 
    'src/main/java', 
    'src/gen' 
] 

... 

// Add JaCoCo test reporting to the test task 
// http://chrisjenx.com/gradle-robolectric-jacoco-dagger/ 
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") { 
    group = "Reporting" 
    description = "Generate Jacoco coverage reports after running tests." 
    reports { 
     xml.enabled = true 
     html.enabled = true 
    } 

    // Class R is used, but usage will not be covered, so ignore this class from report 
    classDirectories = fileTree(
     dir: './build/intermediates/classes/debug', 
     excludes: ['**/R.class', 
        '**/R$*.class' 
    ]) 
    sourceDirectories = files(coverageSourceDirs) 
    executionData = files('build/jacoco/testDebug.exec') 
} 
+0

改良されたスクリプトを見るhttps://gist.github.com/ultraon/54cca81ca159ed0a4a9ebf62e89c26ba – ultraon

6

エマ・サポートは新しいAndroidのビルドシステム内間もなくリリースされる予定されています。今までhttp://tools.android.com/tech-docs/new-build-system/roadmap

のGradleを経由してアンドロイドでエマを実行するための公式な方法はありません。私は計器がかなり簡単に達成できると思うが、それであなたはカバレッジを持つテストを実行するためにAndroidに指示する方法が欠けているだろう。さらに、現時点では、デバイスからemmaランタイムカバレッジデータをプルダウンする方法はありません。

このプロジェクトで興味を持っているのはhttps://github.com/stephanenicolas/Quality-Tools-for-Androidです。 emmaがAndroid Gradleプラグインにするとすぐに更新されます。

---- UPDATE

このプラグインは、Androidで動作する機会を持っていないが、それはAndroidのプラグインと互換性のないJavaプラグインを使用しています。

+0

[Emma plugin for Gradle](http://wiki.gradle.org/display/GRADLE/Plugins#Plugins-Emmaplugin)はJavaプラグインに依存しているため、Androidプラグインでは動作しません(http: //code.google.com/p/android/issues/detail?id=56232))、[JaCoCo Plugin for Gradle](http://wiki.gradle.org/display/GRADLE/Plugins#Plugins-GradleJacocoPlugin) Javaプラグインに依存していないようで、*動作するはずです。 – sschuberth

+0

上記のプロジェクトQATにJacocoを装備したGradleビルドの例があります。前回私が試みたとき、回帰はJacocの最新バージョンが動作することを妨げましたが、それは修正されたかもしれません。 – Snicolas

関連する問題