1

我在 Android 上使用 Parceler 库(https://github.com/johncarl81/parceler

而且我还在使用 Jacoco 进行代码覆盖。

但是,在添加 Parceler 后,使用 Jacoco 运行我的测试代码覆盖率现在失败并出现以下错误:

引起:java.io.FileNotFoundException:/Users/me/android/myapp/blah/app/build/intermediates/classes/demo/com/me/blah/appname/model/User$Parcelable$Creator$0.class(否这样的文件或目录)

用户类如下:

@Parcel
public class User {

    @SerializedName("firstName")
    String firstName;

    @SerializedName("lastName")
    String lastName;

    @SerializedName("profileImage")
    ProfileImage profileImage;

    @SerializedName("username")
    String username;

    // empty constructor needed by the Parceler library
    public User() {
    }

}

我的 Jacoco 文件是这样的:

apply plugin: 'jacoco'

android {
    testOptions {
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
            }
        }
    }
}

project.afterEvaluate {

    android.applicationVariants.all { variant ->
        def name = variant.name
        def testTaskName = "test${name.capitalize()}UnitTest"

        tasks.create(name: "${testTaskName}Coverage", type: JacocoReport, dependsOn: "$testTaskName") {
            group = "Reporting"
            description = "Generate Jacoco coverage reports for the ${name.capitalize()} build."

            classDirectories = fileTree(
                    dir: "${project.buildDir}/intermediates/classes/${name}",
                    excludes: ['**/R.class',
                               '**/R$*.class',
                               '**/*$ViewInjector*.*',
                               '**/*$ViewBinder*.*',
                               '**/BuildConfig.*',
                               '**/Manifest*.*',
                               '**/*$ViewBinder*.*',
                               '**/*$ViewInjector*.*',
                               '**/Lambda$*.class',
                               '**/Lambda.class',
                               '**/*Lambda.class',
                               '**/*Lambda*.class',
                               '**/*InjectAdapter*.*',
                               '**/*StaticInjection*.*',
                               '**/*ModuleAdapter*.*']
            )

            sourceDirectories = files(['src/main/java'].plus(android.sourceSets[name].java.srcDirs))
            executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")

            reports {
                xml.enabled = true
                html.enabled = true
            }
        }
    }
}

我想知道是否需要将生成的 Parceler 文件添加到 Jacoco 文件的排除列表中?如果是这样,我该怎么做?我尝试了各种变体,但似乎都没有奏效。

该错误抱怨缺少以下文件:

用户$Parcelable$Creator$0.class

它是什么,而是按如下方式生成文件:

用户$$Parcelable.class

谁能解释一下?

4

2 回答 2

2

我只是将它排除在 jacocoAndroidCoverage.gradle 中,就像这样,它现在可以工作了

def coverageSourceDirs = [
        '../app/src/main/java'
]

task jacocoStagingDebugCoverageReport(type:JacocoReport, dependsOn: ["connectedStagingDebugAndroidTest"]) {
    group = "Reporting"

    description = "Generate Jacoco coverage reports for staging debug"
    classDirectories = fileTree(
            dir: '../app/build/intermediates/classes/staging/debug/com/',
            excludes: ['**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/*$ViewBinder*.*',
                       '**/*MembersInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*',
                       '**/*$Lambda$*.class',
                       '**/*Factory*.class',
                       '**/*$Builder*', 
                       '**/*$Parcelable*.*',
                       '**/*DaggerApplicationComponent*.class',
                       '**/api']
    )

    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/outputs/code-coverage/connected/flavors/staging/coverage.ec')

    reports {
        xml.enabled = true
        html.enabled = true
    }
}
于 2016-09-06T13:48:25.320 回答
1

尝试这个:

jacocoAndroidUnitTestReport {
    excludes += ['**/*Creator.class']
}

于 2020-03-17T14:33:22.233 回答