buildscript {
    repositories {
        google()
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
        classpath "me.champeau.jmh:jmh-gradle-plugin:0.6.7"
    }
}

/**
 * If `android` is true, the project will be configured as an Android rather than a Java library. This isn't used
 * for building archives (such as an APK), but is useful for performing Android checks like lint.
 */
if (!project.hasProperty("android")) {
    apply plugin: 'java-library'
    apply plugin: 'checkstyle'
    apply plugin: 'jacoco'
    apply plugin: 'me.champeau.jmh'
    apply plugin: 'maven-publish'
} else {
    apply plugin: 'com.android.library'
}

repositories {
    google()
    mavenCentral()
}

sourceSets {
    main.java.srcDirs = ['src/main/java']
    main.resources.srcDirs = ['src/main/resources']
    test.java.srcDirs = ['src/test/java']
    test.resources.srcDirs = ['src/test/resources']
}
compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"

compileJava {
    options.compilerArgs << "-Xlint:deprecation"
}

compileTestJava {
    options.compilerArgs << "-Xlint:deprecation"
}

targetCompatibility = '1.8'
sourceCompatibility = '1.8'

if (!project.hasProperty("android")) {
    // TODO: does not build UML diagrams
    javadoc {
        failOnError = false
    }

    jacocoTestReport {
        reports {
            xml {
                enabled false
            }
        }
    }

// Required to use fileExtensions property in checkstyle file
    checkstyle {
        toolVersion = '7.6.1'
    }

    jmhJar.doFirst {
        new File("build/resources/test").mkdirs()
    }

    jmh {
        excludes = ["(BenchmarkTemplate)"]
        threads = 1
        fork = 1
        warmup = '2s'
        warmupIterations = 10
        warmupBatchSize = 1
        warmupForks = 1
        iterations = 20
        timeOnIteration = '2s'
        timeUnit = 's'
        benchmarkMode = ['avgt', 'ss']
        includeTests = true
        resultFormat = 'CSV'
        forceGC = true

        duplicateClassesStrategy = DuplicatesStrategy.WARN
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                groupId = 'org.getodk'
                artifactId = 'javarosa'
                version = 'local'

                from components.java
            }
        }
    }
} else {
    android {
        namespace 'com.example'

        compileSdkVersion 34

        defaultConfig {
            minSdkVersion 21
            targetSdkVersion 33
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }

        compileOptions {
            coreLibraryDesugaringEnabled true
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        buildTypes {
            release {
                minifyEnabled false
            }
        }

        configurations.configureEach {
            resolutionStrategy {
                // Fix mismatch with kotlin-stdlib
                force 'org.jetbrains:annotations:26.0.2'
            }
        }
    }
}

dependencies {
    // Be sure to update dependencies in pom.xml to match
    api 'joda-time:joda-time:2.10.13'
    api 'org.slf4j:slf4j-api:1.7.33'
    api 'com.fasterxml.jackson.core:jackson-databind:2.13.1'
    api 'org.jetbrains.kotlin:kotlin-stdlib:1.6.10'
    api 'org.bouncycastle:bcprov-jdk18on:1.77'
    api 'net.sf.kxml:kxml2:2.3.0'
    compileOnly 'org.jetbrains:annotations:26.0.2'

    // Upgrade to version higher than 1.4 when Collect minSDK >= 26
    api 'org.apache.commons:commons-csv:1.4'

    // Upgrade to version higher than 2.5 when Collect minSDK >= 26
    api 'commons-io:commons-io:2.5'

    testImplementation 'ch.qos.logback:logback-classic:1.2.10'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'net.sf.kxml:kxml2:2.3.0'
    testImplementation 'org.hamcrest:hamcrest-all:1.3'
    testCompileOnly 'org.jetbrains:annotations:26.0.2'
}
