ext {
    junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
    androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
    androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
    androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
}

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.13.2'
    }
}

apply plugin: 'com.android.library'
apply plugin: 'jacoco'

android {
    namespace = "app.independo.capacitorvoicerecorder"
    compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
    defaultConfig {
        minSdk = project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
        targetSdk = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled = false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
                excludes = ['jdk.internal.*']
            }
        }
    }
    lint {
        abortOnError = false
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_21
        targetCompatibility JavaVersion.VERSION_21
    }
}

jacoco {
    toolVersion = '0.8.11'
}

repositories {
    google()
    mavenCentral()
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':capacitor-android')
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    testImplementation "junit:junit:$junitVersion"
    testImplementation "org.json:json:20251224"
    testImplementation "org.mockito:mockito-core:5.23.0"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
}

tasks.register('jacocoTestReport', JacocoReport) {
    dependsOn tasks.named('testDebugUnitTest')

    def fileFilter = [
        '**/R.class',
        '**/R$*.class',
        '**/BuildConfig.*',
        '**/Manifest*.*',
        '**/*Test*.*',
        'android/**/*.*',
    ]
    def javaClasses = fileTree(dir: "$buildDir/intermediates/javac/debug", excludes: fileFilter)
    def kotlinClasses = fileTree(dir: "$buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
    classDirectories.setFrom(files([javaClasses, kotlinClasses]))
    sourceDirectories.setFrom(files(['src/main/java', 'src/main/kotlin']))
    executionData.setFrom(
        fileTree(dir: buildDir, includes: [
            'jacoco/testDebugUnitTest.exec',
            'outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec',
        ]),
    )

    reports {
        xml.required = true
        html.required = true
        csv.required = false
    }
}
