Android Studio 3.1 Canary 8
Build #AI-173.4529993, built on January 6, 2018
JRE: 1.8.0_152-release-1024-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.14.14-300.fc27.x86_64
我正在尝试使用jacoco来生成代码覆盖率。但是,当我运行命令./gradlew tasks
时,我看不到任何名为jacocoTestReport
的任务。
当我尝试运行任务./gradlew jacocoTestReport
时,我收到以下错误:
任务' jacocoTestReport'在根项目' EnumSample'
中找不到
这是我的build.gradlew文件:
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
android {
compileSdkVersion 27
defaultConfig {
applicationId "me.androidbox.enumsample"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
}
jacoco {
toolVersion "0.8.0"
}
task jacocoTestReport(type: JacocoReport) {
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
subprojects.each {
sourceSets it.sourceSets.main
}
reports {
xml.enabled true
html.enabled false
csv.enabled false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
我试图清理并重建项目。但是,报告任务不在那里。
非常感谢任何建议。
答案 0 :(得分:6)
使用jacoco报告时我们需要注意以下几点:
在app / build.gradle中启用测试覆盖率
android {
...
buildTypes {
debug {
testCoverageEnabled true
}
...
}
}
为jacoco报告创建任务
apply plugin: 'jacoco'
task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec")
}
jacoco报告的Gradle命令
./ gradlew clean jacocoTestReport
在此处查找jacoco报告
成功执行jacocoTestReport后生成的jacoco报告路径。
应用程序/建立/报告/覆盖/调试/ index.html中
另外,我已经创建了一个与你相关的android jacoco相关示例存储库。
https://github.com/jiteshmohite/JacocoAndroidSample
另外,请确保您在应用程序目录中运行Gradle命令。
尝试上述示例存储库以供参考。我创造了这个没有复杂性的东西,所以每个人都可以去使用它。
答案 1 :(得分:5)
您正在搜索要执行的错误任务。通过执行./gradlew tasks
,您将能够找到创建 Flavor CoverageReport 任务:
使用您在问题中提到的设置执行./gradlew createDevDebugCoverageReport
后,我能够在/app/build/reports/dev/debug
目录中找到生成的报告。
答案 2 :(得分:2)
有两件事:
您需要enable the code coverage支持您将要测试的构建类型。您的build.gradle
应包含以下内容(您已包含的内容):
android {
...
buildTypes {
debug {
testCoverageEnabled = true
}
...
}
...
}
gradle testBlueDebugUnitTestCoverage
,您将在“build / reports / jacoco / testBlueDebugUnitTestCoverage /” 使用生成JaCoCo报告的Gradle plugin:
将其设置为:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.11.0'
}
}
apply plugin: 'com.vanniktech.android.junit.jacoco'
另一个问题解决方案报告here:
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = false
html.enabled = true
}
classDirectories = fileTree(
dir: './build/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files('build/jacoco/testDebug.exec')
renamedFilesMap = [:]
// Hacky fix for issue: https://code.google.com/p/android/issues/detail?id=69174.
// Rename files with '$$' before generating report, and then rename back after
doFirst {
new File('build/classes/debug').eachFileRecurse { file ->
if (file.name.contains('$$')) {
oldPath = file.path
newPath = oldPath.replace('$$', '$')
file.renameTo(newPath)
renamedFilesMap[newPath] = oldPath
}
}
}
doLast {
renamedFilesMap.each() {
newPath, oldPath ->
new File(newPath).renameTo(oldPath)
}
}
}