import me.testcase.ognarviewer.GenerateDdbTask;

plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.playPublisher)
    id "androidx.navigation.safeargs"
    id "checkstyle"
}

android {
    namespace "me.testcase.ognarviewer"
    compileSdk 35

    defaultConfig {
        applicationId "me.testcase.ognarviewer"
        minSdk 26
        targetSdk 35
        versionCode 24
        versionName "1.0.7"

        resConfigs "en"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    // F-Droid wants we strip out the dependency list, because it is not
    // checked as part of the signature verification and one could potentially
    // place any non-free code there.
    dependenciesInfo {
        includeInApk = false
        includeInBundle = false
    }

    signingConfigs {
        release {
            storeFile System.getenv("KEYSTORE_FILE") ? file(System.getenv("KEYSTORE_FILE")) : null
            storePassword System.getenv("KEYSTORE_PASSWORD")
            keyAlias System.getenv("KEY_ALIAS")
            keyPassword System.getenv("KEY_PASSWORD")
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
            if (System.getenv("KEYSTORE_FILE")) {
                signingConfig signingConfigs.release
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures {
        viewBinding true
        buildConfig true
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            all {
                // Otherwise the PrivateDirectoryTest fails.
                // See https://github.com/robolectric/robolectric/issues/7456#issuecomment-2089850157
                jvmArgs "--add-opens=java.base/java.io=ALL-UNNAMED"
            }
        }
    }

    applicationVariants.configureEach { variant ->
        variant.registerGeneratedResFolders(project.files(generateDdbResource.outputDirectory).builtBy(generateDdbResource))
    }
}

dependencies {
    implementation libs.appcompat
    implementation libs.constraintlayout
    implementation libs.material
    implementation libs.navigation.fragment
    implementation libs.navigation.ui
    implementation libs.preference
    implementation libs.recyclerview
    implementation libs.recyclerview.selection

    debugImplementation libs.leakcanary
    debugImplementation libs.fragment.testing.manifest

    testImplementation libs.junit
    testImplementation libs.robolectric
    testImplementation libs.fragment.testing
}

tasks.register("generateDdbResource", GenerateDdbTask)

// The Checkstyle plugin doesn't work well with the Android plugin => register the task manually.
tasks.register("checkstyle", Checkstyle) {
    group "verification"
    source "src/main/java"
    classpath = files()
}

check.dependsOn("checkstyle")
