apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'jacoco'

def coverageEnabled = project.hasProperty('coverage')

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.example.test_app"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        testInstrumentationRunnerArguments(clearPackageData: 'true')

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        debug {
            testCoverageEnabled coverageEnabled
        }
    }

    testOptions {
        animationsDisabled = true
        //execution = "ANDROIDX_TEST_ORCHESTRATOR"
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.3.0-alpha01'
    implementation 'androidx.core:core-ktx:1.5.0-alpha01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    // Espresso.
    // https://developer.android.com/jetpack/androidx/releases/test
    androidTestUtil 'androidx.test:orchestrator:1.2.0'
    androidTestImplementation 'pl.pragmatists:JUnitParams:1.1.1'
    androidTestImplementation("androidx.test:runner:1.3.0-rc01")
    androidTestImplementation("androidx.test.ext:junit:1.1.2-rc01")
    androidTestImplementation("androidx.test.ext:junit-ktx:1.1.2-rc01")
    androidTestImplementation("androidx.test.ext:truth:1.3.0-rc01")
    androidTestImplementation("androidx.test.espresso.idling:idling-concurrent:3.3.0-rc01")
    androidTestImplementation("androidx.test.espresso.idling:idling-net:3.3.0-rc01")
    androidTestImplementation("androidx.test.espresso:espresso-accessibility:3.3.0-rc01")
    androidTestImplementation("androidx.test:rules:1.3.0-rc01")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0-rc01")
    androidTestImplementation("androidx.test.espresso:espresso-contrib:3.3.0-rc01")
    androidTestImplementation("androidx.test.espresso:espresso-idling-resource:3.3.0-rc01")
    androidTestImplementation("androidx.test.espresso:espresso-intents:3.3.0-rc01")
    androidTestImplementation("androidx.test.espresso:espresso-web:3.3.0-rc01")
}

if (coverageEnabled) {
    // gradle -Pcoverage firebaseJacoco
    task firebaseJacoco(type: JacocoReport) {
        group = "Reporting"
        description = "Generate Jacoco coverage reports for Firebase test lab."

        def excludes = [
                '**/R.class',
                '**/R$*.class',
                '**/BuildConfig.*',
                "**/androidx"]
        def javaClasses = fileTree(dir: "${project.buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: excludes)
        def kotlinClasses = fileTree(dir: "${project.buildDir}/tmp/kotlin-classes/debug", excludes: excludes)
        getClassDirectories().setFrom(files([javaClasses, kotlinClasses]))

        getSourceDirectories().setFrom(files([
                'src/main/java', 'src/main/kotlin',
                'src/androidTest/java', 'src/androidTest/kotlin']))

        def ecFiles = project.fileTree(dir: '..', include: 'results/coverage_ec/**/sdcard/*.ec')
        ecFiles.forEach { println("Reading in $it") }
        getExecutionData().setFrom(ecFiles)

        reports {
            html { enabled true }
            xml { enabled false }
        }
    }
}
