Подтвердить что ты не робот

JaCoCo возвращается 0% Покрытие с Kotlin и Android 3.0

Я пытаюсь проверить покрытие кода для тестового примера, которое я написал в Kotlin. Когда я выполняю ./gradlew createDebugCoverageReport --info, мой файл coverage.ec пуст, и в моих отчетах указывается, что у меня 0% покрытия. Обратите внимание, что тестовые примеры на 100% успешны. Может ли кто-нибудь подумать о каких-либо причинах, по которым мой файл coverage.ec возвращает 0 байт?

Я искал везде без везения.

apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'jacoco'


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        debug {
            testCoverageEnabled = true
        }
        release {
            minifyEnabled false
            testCoverageEnabled = true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


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

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:25.4.0'
    testImplementation 'junit:junit:4.12'
    implementation files('pathtosomejarfile')

}



jacoco {
    toolVersion = "0.7.6.201602180812"
    reportsDir = file("$buildDir/customJacocoReportDir")

}



task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {

    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/androidTest/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec",
            "outputs/code-coverage/connected/*coverage.ec"
    ])
}
4b9b3361

Ответ 1

Вы можете получить линейное покрытие для кода Java и Kotlin, указав два разных каталога для сгенерированных файлов .class:

def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def kotlinDebugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)

Затем просто включите обе файлы в ваши каталоги classDirectories:

classDirectories = files([debugTree], [kotlinDebugTree])

Ответ 2

Через несколько дней я решил решить эту проблему. Для тех из вас, кто сталкивается с подобными проблемами: В вашей папке промежуточных файлов должна быть папка tmp. Эта папка содержит файлы .class для файлов Kotlin. Если вы измените путь fileTree (dir: "$ {buildDir}/intermediates/classes/debug", исключает: fileFilter), где находятся эти файлы классов, Jacoco будет генерировать покрытие кода для вас! Обратите внимание, что вы не сможете увидеть полный, поэтапный обзор вашего покрытия, используя этот метод.