plugins {
    id 'com.android.application'
}
import groovy.json.JsonSlurper
def getVersionCode() {
    def versionFile = file('../version.properties')
    if (versionFile.canRead()) {
        def versionProps = new Properties()
        versionProps.load(new FileInputStream(versionFile))
        def versionCode = versionProps['VERSION_CODE'].toInteger()
        def runTasks = gradle.startParameter.taskNames
        for (final def taskItem in runTasks) {
            println(taskItem)
        }
        if (':app:assembleRelease' in runTasks) {
            versionProps['VERSION_CODE'] = (++versionCode).toString()
            versionProps.store(versionFile.newWriter(), null)
        }
        return versionCode
    } else {
        throw new GradleException("Could not find version.properties!")
    }
}

android {
    namespace 'org.b3log.siyuan'
    compileSdk 34
    def jsonFile = file('../../app/package.json')
    def jsonContent = new JsonSlurper().parseText(jsonFile.text)
    def currentVersionCode = getVersionCode()
    defaultConfig {
        applicationId "org.b3log.siyuan"
        minSdk 24
        targetSdk 34
        versionCode currentVersionCode
        versionName jsonContent["version"]
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            resValue "string", "app_name", "SiYuan"
            resValue "string", "app_package_name", "org.b3log.siyuan"
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix ".debug"
            minifyEnabled false
            resValue "string", "app_name", "SiYuan-Debug"
            resValue "string", "app_package_name", "org.b3log.siyuan.debug"
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    applicationVariants.configureEach { variant ->
        variant.outputs.configureEach { output ->
            def currentBuildType = project.android.buildTypes.find { it.name == variant.buildType.name }
            def newApkName = "siyuan-unlock-" + currentBuildType.name + "-v" + versionName + ".apk"
            outputFileName = newApkName
        }
    }

    repositories {
        flatDir {
            dirs 'libs'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        resources {
            excludes += ['META-INF/DEPENDENCIES',
                         'META-INF/LICENSE',
                         'META-INF/LICENSE.txt',
                         'META-INF/license.txt',
                         'META-INF/NOTICE',
                         'META-INF/NOTICE.txt',
                         'META-INF/notice.txt',
                         'plugin.properties']
        }
        jniLibs {
            useLegacyPackaging true
        }
    }

    lint {
        abortOnError false
    }

}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.10.0'
    implementation 'androidx.lifecycle:lifecycle-process:2.6.1'
    implementation 'androidx.work:work-runtime:2.8.1'

    implementation(name: 'kernel', ext: 'aar')
    implementation("commons-io:commons-io:2.5") // 不要升级，否则无法兼容 Android 7
    implementation 'com.gitee.zackratos:UltimateBarX:0.8.0'
    implementation 'com.blankj:utilcodex:1.31.1'
    implementation 'com.koushikdutta.async:androidasync:2.2.1'
}
