apply plugin: 'com.android.application'

def getVersionName = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags', '--always'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

android {
    compileSdk 34

    defaultConfig {
        applicationId "org.xbmc.kore"
        minSdkVersion 24
        targetSdkVersion 34
        versionCode 33
        versionName = getVersionName()
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

        def supportedLocales = ["en",
                "af-za", "ast", "be-by", "bg", "ca", "cs", "da-dk", "de",
                "es", "es-MX", "eu", "fi", "fr", "hr", "hu", "it", "iw", "ja",
                "ko", "lt", "nl", "pl", "pt", "pt-BR", "ru", "sk", "sl", "zh-CN"]
        buildConfigField "String[]", "SUPPORTED_LOCALES", "new String[]{\""+
                supportedLocales.join("\",\"")+"\"}"
    }

    signingConfigs {
        release {
            if (System.getenv("KODI_ANDROID_STORE_FILE") != null) {
                keyAlias System.getenv("KODI_ANDROID_KEY_ALIAS")
                keyPassword System.getenv("KODI_ANDROID_KEY_PASSWORD")
                storeFile file(System.getenv("KODI_ANDROID_STORE_FILE"))
                storePassword System.getenv("KODI_ANDROID_STORE_PASSWORD")
                enableV1Signing true
                enableV2Signing true
                enableV3Signing true
            }
        }
    }

    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
        unitTests {
            includeAndroidResources = true
        }
    }

    buildTypes {
        release {
            if (System.getenv("KODI_ANDROID_STORE_FILE") != null) {
                signingConfig signingConfigs.release
                zipAlignEnabled true
            }
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        viewBinding true
    }

    packagingOptions {
        resources {
            excludes += ['META-INF/DEPENDENCIES', 'META-INF/NOTICE', 'META-INF/LICENSE', 'META-INF/LICENSE.txt', 'META-INF/NOTICE.txt']
        }
    }

    lint {
        // Too much trouble keeping all translations in sync
        disable 'MissingTranslation'
    }

    bundle {
        language {
            enableSplit = false
        }
    }
    namespace 'org.xbmc.kore'

}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

ext {
    supportLibVersion = '28.0.0'
}

dependencies {
    implementation 'com.google.android.material:material:1.10.0'
    implementation 'androidx.preference:preference:1.2.1'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.recyclerview:recyclerview:1.3.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation('androidx.core:core-google-shortcuts:1.1.0') {
        exclude group:'com.google.android.gms'
    }
    implementation 'androidx.media:media:1.6.0'
    implementation "androidx.viewpager2:viewpager2:1.1.0-beta02"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

    implementation "androidx.fragment:fragment:1.6.2"
    implementation "androidx.fragment:fragment-ktx:1.6.2"

//    Dependency resolution
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
    implementation "androidx.lifecycle:lifecycle-viewmodel:2.6.2"

    // Jackson v2.13 kept. v2.14 and later require minSDK >= 26
    // https://github.com/FasterXML/jackson/wiki/Jackson-Releases
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.5'
    implementation 'com.squareup.okhttp3:okhttp:4.12.0'
    // Ignore the new version warning as it refers to v2.71... which is older on maven
    implementation 'com.squareup.picasso:picasso:2.8'
    implementation 'org.greenrobot:eventbus:3.3.1'
    implementation 'org.jmdns:jmdns:3.5.8'
    implementation 'at.blogc:expandabletextview:1.0.5'
    implementation 'com.simplecityapps:recyclerview-fastscroll:2.0.1'
    implementation 'org.nanohttpd:nanohttpd:2.3.1'

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test:rules:1.5.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'
    androidTestImplementation 'androidx.legacy:legacy-support-v13:1.0.0'
    androidTestImplementation 'org.hamcrest:hamcrest-library:2.2'
    androidTestImplementation 'junit:junit:4.13.2'
    androidTestUtil 'androidx.test:orchestrator:1.4.2'

    testImplementation 'org.robolectric:robolectric:4.8.1'
    testImplementation 'androidx.test:core:1.5.0'
    testImplementation 'androidx.test.ext:junit:1.1.5'
    debugImplementation 'junit:junit:4.13.2'
}

def adb = android.getAdbExecutable().toString()
afterEvaluate {
    tasks.register('grantAnimationPermissionDev', Exec) {
        dependsOn installDebug
        doFirst {
            println("Executing: $adb shell pm grant $android.defaultConfig.applicationId android.permission.SET_ANIMATION_SCALE")
            commandLine "$adb shell pm grant $android.defaultConfig.applicationId android.permission.SET_ANIMATION_SCALE".split(' ')
        }
    }
    tasks.each { task ->
        if (task.name.startsWith('connectedDebugAndroidTest')) {
            task.dependsOn grantAnimationPermissionDev
        }
    }
}

/**
 * Makes sure assets are copied before running the unit tests
 */
tasks.configureEach { task ->
    if (task.name.contains("testDebugUnitTest")) {
        task.dependsOn assembleDebug
    }
}

tasks.withType(JavaCompile).configureEach {
    options.compilerArgs << '-Xlint:unchecked' // << '-Xlint:deprecation'
}
