plugins {
    id "com.diffplug.spotless" version "5.12.5"
}
apply from: '../dependencies.gradle'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
/* TODO disabling Jacoco due to incompatibility with AGP >= 8.0
 * https://github.com/arturdm/jacoco-android-gradle-plugin/issues/102
apply plugin: 'com.dicedmelon.gradle.jacoco-android'

jacoco {
    toolVersion = "0.8.7"
}

jacocoAndroidUnitTestReport {
    csv.enabled false
    html.enabled true
    xml.enabled true
}
*/

repositories {
    google()
    maven { url "https://jitpack.io" }
}

spotless {
    kotlin {
        target '**/*.kt'
        ktlint()userData(['max_line_length': '100'])
    }
}

android {
    compileSdk COMPILE_SDK_VERSION
    namespace "com.farmerbb.taskbar"

    defaultConfig {
        applicationId "com.farmerbb.taskbar"

        minSdkVersion MIN_SDK_VERSION
        targetSdkVersion TARGET_SDK_VERSION
        versionCode VERSION_CODE
        versionName VERSION_NAME

        resConfigs "en", "es", "ja", "ru", "de", "zh-rCN", "tr", "nl", "pl"
        vectorDrawables.generatedDensities = []

        buildConfigField "String", "BASE_APPLICATION_ID", "\"com.farmerbb.taskbar\""
        buildConfigField "String", "PAID_APPLICATION_ID", "\"com.farmerbb.taskbar.paid\""
        buildConfigField "String", "SUPPORT_APPLICATION_ID", "\"com.farmerbb.taskbar.support\""
        buildConfigField "String", "ANDROIDX86_APPLICATION_ID", "\"com.farmerbb.taskbar.androidx86\""

        buildConfigField "long", "TIMESTAMP", "${System.currentTimeMillis()}L"
    }

    buildFeatures {
        buildConfig true
    }

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

    kotlinOptions {
        jvmTarget = '21'
    }

    signingConfigs {
        release {
            if(System.getenv("KSTOREFILE") != null)
                storeFile file(System.getenv("KSTOREFILE"))

            storePassword System.getenv("KSTOREPWD")
            keyAlias System.getenv("KEYALIAS")
            keyPassword System.getenv("KEYPWD")
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            manifestPlaceholders = [appName: "@string/tb_app_name_debug"]
        }

        release {
            minifyEnabled true
            shrinkResources true
            // NOTE: Do not use 'proguard-android-optimize.txt' as it breaks the mapping overrides
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            manifestPlaceholders = [appName: "@string/tb_app_name"]

            applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "${project.parent.name}-${defaultConfig.versionName}.apk"
                }
            }
        }
    }

    flavorDimensions "default"

    productFlavors {
        paid { applicationId "com.farmerbb.taskbar.paid" }
        free { applicationId "com.farmerbb.taskbar" }
    }

    sourceSets {
        main {
            java { srcDirs('src/main/java', 'src/playstore/java', 'src/nonlib/java', "src/compat-$COMPILE_SDK_VERSION/java") }
            res { srcDirs('src/main/res', 'src/playstore/res', 'src/nonlib/res') }
            manifest.srcFile 'src/playstore/AndroidManifest.xml'
        }
    }
    
    lintOptions {
        abortOnError false
    }

    testOptions {
        unitTests {
            all {
                testLogging {
                    events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'

                    outputs.upToDateWhen {
                        false
                    }

                    showStandardStreams = false
                    exceptionFormat = 'full'
                    /* TODO disabling Jacoco due to incompatibility with AGP >= 8.0
                     * https://github.com/arturdm/jacoco-android-gradle-plugin/issues/102
                    jacoco {
                        includeNoLocationClasses = true
                        excludes = ['jdk.internal.*']
                    }
                    */
                }
            }
            
            includeAndroidResources = true
        }
    }
}

dependencies {
    implementation "androidx.legacy:legacy-support-v4:$SUPPORT_V4_VERSION"
    implementation "androidx.appcompat:appcompat:$APPCOMPAT_VERSION"
    //noinspection GradleDependency
    implementation "androidx.browser:browser:$BROWSER_VERSION"
    implementation "com.google.android.material:material:$MATERIAL_VERSION"
    //noinspection GradleDependency
    implementation 'com.mikepenz:iconics-core:3.2.5'
    implementation 'com.mikepenz:foundation-icons-typeface:3.0.0.5'
    implementation "dev.rikka.shizuku:api:$SHIZUKU_VERSION"
    implementation "dev.rikka.shizuku:provider:$SHIZUKU_VERSION"
    implementation 'moe.banana:toast-compat:1.0.5'
    implementation group:'com.twofortyfouram', name:'android-plugin-api-for-locale', version:'[1.0.2,2.0['
    implementation "org.lsposed.hiddenapibypass:hiddenapibypass:$HIDDEN_API_BYPASS_VERSION"

    testImplementation "com.google.truth:truth:1.1.3"
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'androidx.test:core:1.4.0'
    testImplementation "org.robolectric:robolectric:4.7.3"
    testImplementation "org.powermock:powermock-module-junit4:$POWERMOCK_VERSION"
    testImplementation "org.powermock:powermock-module-junit4-rule:$POWERMOCK_VERSION"
    testImplementation "org.powermock:powermock-api-mockito2:$POWERMOCK_VERSION"
    testImplementation "org.powermock:powermock-classloading-xstream:$POWERMOCK_VERSION"
    testImplementation 'org.apache.commons:commons-lang3:3.12.0'

    coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:2.1.2"
}
