我在使用Android设置jacoco时遇到了问题,无法进行手动测试。我可以在手动测试应用程序时生成.exec文件。但是,运行带有.exec文件的jacocoTestReport会创建没有覆盖的报告:
我已经遵循了以下教程和主题,但并未解决此问题:
在build.gradle上我有
jacoco{
version "0.7.9"
}
sourceSets {
main {
resources.includes = ['**/jacoco-agent.properties']
}
}
task jacocoTestReport(type:JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports"
// exclude auto-generated classes and tests
def fileFilter = ['**/R.class', '**/R$*.class',
'**/BuildConfig.*', '**/Manifest*.*',
'android/**/*.*']
def debugTree = fileTree(dir:
"${project.buildDir}/intermediates/classes/debug",
excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
additionalSourceDirs = files([
"${buildDir}/generated/source/buildConfig/debug",
"${buildDir}/generated/source/r/debug"
])
executionData = fileTree(dir: project.projectDir, includes:
['**/*.exec', '**/*.ec'])
reports {
xml.enabled = true
xml.destination = "${buildDir}/jacocoTestReport.xml"
csv.enabled = false
html.enabled = true
html.destination = "${buildDir}/reports/jacoco"
}
}
在onStop()方法上,我有:
protected void onStop() {
String TAG = "jacoco";
// use reflection to call emma dump coverage method, to avoid
// always statically compiling against emma jar
String coverageFilePath = "/sdcard/coverage.exec";
java.io.File coverageFile = new java.io.File(coverageFilePath);
try {
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",
coverageFile.getClass(), boolean.class, boolean.class);
dumpCoverageMethod.invoke(null, coverageFile, false, false);
Log.e(TAG, "generateCoverageReport: ok");
} catch (Exception e) {
new Throwable("Is emma jar on classpath?", e);
}
super.onStop();
EventBus.getDefault().unregister(this);
}
在资源文件夹中,我有一个文件“ jacoco-agent.properties”。在此文件中,我有:
destfile=/storage/sdcard/coverage.exec
我正在使用Android Studio 3.3.1在Android 6.0模拟器上进行测试。