android {
    compileSdk 35

    defaultConfig {
        minSdk 21
        targetSdk 35

        vectorDrawables.useSupportLibrary true
        vectorDrawables.generatedDensities = []

        testApplicationId "de.danoeh.antennapod.core.tests"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        resourceConfigurations += ["en", "ar", "br", "ca", "cs", "da", "de", "el", "es", "et", "eu", "fa", "fi", "fr", "gl", "hi", "hu", "in", "it", "iw", "ja", "ko", "lt", "nb", "nl", "pl", "pt", "pt-rBR", "ro", "ru", "sc", "sk", "sl", "sr", "sv", "tr", "uk", "zh-rCN", "zh-rTW"]
    }

    buildTypes {
        release {
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard.cfg"
            resValue "string", "app_name", "AntennaPod"
        }
        debug {
            resValue "string", "app_name", "AntennaPod Debug"
        }
    }

    packagingOptions {
        resources {
            excludes += ["META-INF/LICENSE.txt",
                         "META-INF/NOTICE.txt",
                         "META-INF/CHANGES",
                         "META-INF/README.md"]
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    testOptions {
        animationsDisabled = true
        unitTests {
            includeAndroidResources = true
        }
    }

    lint {
        disable "GradleDependency", "OutdatedLibrary"
        checkDependencies true
        warningsAsErrors true
        abortOnError true
        checkGeneratedSources = true
    }

    buildFeatures {
        viewBinding true
        buildConfig true
    }
}

dependencies {
    // align all versions of kotlin transitive dependencies, see
    // https://youtrack.jetbrains.com/issue/KT-55297/kotlin-stdlib-should-declare-constraints-on-kotlin-stdlib-jdk8-and-kotlin-stdlib-jdk7
    implementation platform("org.jetbrains.kotlin:kotlin-bom:1.9.24")
}

tasks.withType(Test).configureEach {
    testLogging {
        exceptionFormat "full"
        events "skipped", "passed", "failed"
        showStandardStreams true
        displayGranularity 2
    }
}

gradle.projectsEvaluated {
    tasks.withType(JavaCompile).tap {
        configureEach {
            options.compilerArgs << "-Xlint"
        }
    }
}

apply plugin: 'com.github.spotbugs'

spotbugs {
    toolVersion = '4.2.3'
    effort = 'max'
    reportLevel = 'medium'
    excludeFilter = rootProject.file('config/spotbugs/exclude.xml')
    ignoreFailures = true // Handled by printing task
}

gradle.taskGraph.whenReady { taskGraph ->
    taskGraph.allTasks.each { task ->
        if (task.name.toLowerCase().contains('spotbugs')) {
            task.doLast {
                def reportFile = task.project.file("build/reports/spotbugs/debug.xml")
                def reportFilePlay = task.project.file("build/reports/spotbugs/playDebug.xml")

                if (reportFile.exists()) {
                    parseSpotBugsXml(task, reportFile)
                }
                if (reportFilePlay.exists()) {
                    parseSpotBugsXml(task, reportFilePlay)
                }
            }
        }
    }
}

def parseSpotBugsXml(task, xmlFile) {
    def slurped = new groovy.xml.XmlSlurper().parse(xmlFile)

    def foundErrors = false
    slurped['BugInstance'].each { bug ->
        logger.error "[SpotBugs] ${bug['LongMessage']} [${bug.@'type'}]"
        bug['SourceLine'].each { line ->
            logger.error "[SpotBugs]     ${line['Message']}"
            foundErrors = true
        }
    }
    if (foundErrors) {
        throw new TaskExecutionException(task,
                new Exception("SpotBugs violations were found. See output above for details."))
    }
}
