/*
 * This file is part of LSPosed.
 *
 * LSPosed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LSPosed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LSPosed.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2020 EdXposed Contributors
 * Copyright (C) 2021 LSPosed Contributors
 */
import java.nio.file.Paths

apply plugin: 'com.android.application'

android {
    compileSdkVersion androidCompileSdkVersion.toInteger()

    ndkVersion androidCompileNdkVersion
    buildToolsVersion androidBuildToolsVersion

    buildFeatures {
        viewBinding = true
    }

    defaultConfig {
        applicationId "io.github.lsposed.manager"
        minSdkVersion androidMinSdkVersion.toInteger()
        targetSdkVersion androidTargetSdkVersion.toInteger()
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        resConfigs 'en', 'zh-rCN', 'zh-rTW', 'zh-rHK', 'ru', 'uk', 'nl', 'ko'
        resValue "string", "versionName", "${rootProject.ext.versionName}"
    }

    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }

    lintOptions {
        disable 'MissingTranslation'
        disable 'ExtraTranslation'
        abortOnError true
        checkReleaseBuilds true
    }

    packagingOptions {
        exclude 'META-INF/**'
        exclude 'kotlin/**'
        exclude 'org/**'
        exclude '**.properties'
        exclude '**.bin'
    }

    dependenciesInfo.includeInApk false

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            shrinkResources false
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = "LSPosedManager-${defaultConfig.versionName}-${defaultConfig.versionCode}-${buildType.name}.apk"
        }
    }
}

def optimizeReleaseResources = task('optimizeReleaseResources').doLast {
    def aapt2 = Paths.get(project.android.sdkDirectory.path, 'build-tools', project.android.buildToolsVersion, 'aapt2')
    def zip = Paths.get(project.buildDir.path, 'intermediates',
            'processed_res', 'release', 'out', "resources-release.ap_")
    def optimized = new File("${zip}.opt")
    def cmd = exec {
        commandLine aapt2, 'optimize', '--collapse-resource-names',
                '--shorten-resource-paths',
                '-o', optimized, zip
        ignoreExitValue false
    }
    if (cmd.exitValue == 0) {
        delete(zip)
        optimized.renameTo("$zip")
    }
}

tasks.whenTaskAdded { task ->
    if (task.name == 'processReleaseResources') {
        task.finalizedBy optimizeReleaseResources
    }
}

dependencies {
    final def glide_version = '4.12.0'
    final def markwon_version = '4.6.2'
    final def okhttp_version = '4.9.1'
    annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"
    implementation 'androidx.activity:activity:1.2.0'
    implementation 'androidx.browser:browser:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.fragment:fragment:1.3.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.caverock:androidsvg-aar:1.4'
    implementation "com.github.bumptech.glide:glide:$glide_version"
    implementation "com.github.bumptech.glide:okhttp3-integration:$glide_version"
    implementation 'com.github.jinatonic.confetti:confetti:1.1.2'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.takisoft.preferencex:preferencex:1.1.0'
    implementation 'com.takisoft.preferencex:preferencex-colorpicker:1.1.0'
    implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
    implementation "com.squareup.okhttp3:okhttp-dnsoverhttps:$okhttp_version"
    implementation "io.noties.markwon:core:$markwon_version"
    implementation "io.noties.markwon:ext-strikethrough:$markwon_version"
    implementation "io.noties.markwon:ext-tables:$markwon_version"
    implementation "io.noties.markwon:ext-tasklist:$markwon_version"
    implementation "io.noties.markwon:html:$markwon_version"
    implementation "io.noties.markwon:image-glide:$markwon_version"
    implementation "io.noties.markwon:linkify:$markwon_version"
    implementation 'rikka.appcompat:appcompat:1.2.0-rc01'
    implementation 'rikka.core:core:1.3.0'
    implementation 'rikka.insets:insets:1.0.1'
    implementation 'rikka.material:material:1.6.0'
    implementation 'rikka.recyclerview:recyclerview-utils:1.2.0'
    implementation 'rikka.widget:borderview:1.0.0'
    implementation "rikka.widget:switchbar:1.0.2"
    implementation 'rikka.layoutinflater:layoutinflater:1.0.1'
    implementation 'tech.rectifier.preferencex-android:preferencex-simplemenu:88f93154b2'
    implementation 'me.zhanghai.android.appiconloader:appiconloader-glide:1.2.0'
    implementation 'me.zhanghai.android.fastscroll:library:1.1.5'
    implementation files('libs/WeatherView-2.0.3.aar')
    implementation project(':manager-service')
    compileOnly project(":hiddenapi-stubs")
}

configurations {
    cleanedAnnotations
    compile.exclude group: 'org.jetbrains' , module:'annotations'
    compile.exclude group: 'androidx.appcompat', module: 'appcompat'
}
