更新gradle版本3.0后,JacocoTestReport无法正常工作

时间:2017-10-27 09:38:59

标签: java android gradle android-gradle jacoco

我在Android项目中开始从Gradle 2.3迁移到Gradle 3.0。我解决了这个迁移过程中的大多数问题,但我仍然有一个我无法解决它的问题。问题在于jacocoTestReport任务,我不知道为什么会出现此错误。我尝试将jacoco插件更新到最新的avaiable版本但没有成功。下面我打了原始的jacoco gradle文件。任何提示?

  

出了什么问题:任务执行失败':mobile:jacocoTestReport'。   无法读取执行数据文件/ Users / mbratkowski / android_projects / android_app / mobile / build / jacoco / testQaGoogleDebugUnitTest.exec```

apply plugin: 'jacoco'

jacoco {
    version '0.7.5.201505241946'
}

configurations {
    jacocoTestReport
}


task jacocoTestReport(type: JacocoReport) {
    group = 'Reporting'
    description = 'Generate Jacoco coverage reports after running tests.'

    reports {
        xml {
            enabled true // coveralls
            destination "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"
        }

        html {
            enabled true
            destination "${project.buildDir}/reports/jacoco/test/html"
        }
    }

    // fix issue with double $$ at generate report task
    doFirst {
        applyDaggerWorkaround('app/build/intermediates/classes/')
    }
    // avoid some side effects through revert renaming
    doLast {
        revertDaggerWorkaround('app/build/intermediates/classes/')
    }

    // use hidden configuration, for details look into JacocoPlugin.groovy
    jacocoClasspath = project.configurations['androidJacocoAnt']

    // exclude auto-generated classes and tests
    def fileFilter = ['**/R.class',
                      '**/R$*.class',
                      '**/BuildConfig.*',
                      '**/Manifest*.*',
                      '**/*Test*.*',
                      '**/*InjectAdapter*.*',
                      '**/*StaticInjection*.*',
                      '**/*ModuleAdapter*.*',
                      '**/*ViewInjector*.*',
                      '**/*ViewBinder*.*',
                      '**/*$InjectAdapter.class',
                      '**/*$ModuleAdapter.class',
                      '**/*$ViewInjector*.class',
                      '**/*Binding.class',
                      '**/*_MembersInjector.class',
                      '**/*_*Factory.class',
                      '**/AutoParcel_*.class',
                      '**/*_ResourcesFactory.class',
                      '**/Dagger*.class',
                      '**/*ViewModel.class',
                      '**/BR.class',
                      '**/databinding/*.class',
                      '**/android/databinding/**/*.class'
    ]

    def debugTree = fileTree(
            dir: "${project.buildDir}/${project['JACOCO_CLASS_DIR']}",
            excludes: fileFilter
    )

    def coverageSourceDirs = [
            "src/main/java",
            "src/qa/java",
            "src/debug/java",
    ]

    sourceDirectories = files([coverageSourceDirs])
    classDirectories = files([debugTree])

    executionData fileTree(project.buildDir).include('**/jacoco/*.exec')
            .include('**/code-coverage/**/coverage.ec')
}

def applyDaggerWorkaround(String pathWithDaggerClasses) {
    def filePath = new File(pathWithDaggerClasses)
    if (filePath.exists()) {
        filePath.eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}
def revertDaggerWorkaround(String pathWithDaggerClasses) {
    def filePath = new File(pathWithDaggerClasses)
    if (filePath.exists()) {
        filePath.eachFileRecurse { file ->
            if (file.name.contains('$ModuleAdapter')) {
                file.renameTo(file.path.replace('$ModuleAdapter', '$$ModuleAdapter'))
            }
        }
    }
}

0 个答案:

没有答案