plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

//签名配置
def keystorePropertiesFile=rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
//https://github.com/flutter/flutter/issues/156304
android {
    namespace "top.coclyun.clipshare"
    compileSdk flutter.compileSdkVersion
    ndkVersion '25.1.8937393'

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

    kotlinOptions {
        jvmTarget = 17
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "top.coclyun.clipshare"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 24
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        // bundle分包时默认会增加versionCode，这里重新复写 https://github.com/flutter/flutter/issues/49544
        project.android.applicationVariants.all { variant ->
            variant.outputs.all { output ->
                output.versionCodeOverride = flutterVersionCode.toInteger()
                def versionName = variant.versionName
                def buildType = variant.buildType.name
                def abi = output.getFilter(com.android.build.OutputFile.ABI) // 获取架构名称

                // 3. 修改 APK 文件名，保留架构名称
                if (abi != null) {
                    // 带架构名称的 APK（如 app_release_v1.0.0_arm64-v8a.apk）
                    output.outputFileName = "app-${abi}-${buildType}-v${versionName}.apk"
                } else {
                    // 通用 APK（无架构名称）
                    output.outputFileName = "app-${buildType}-v${versionName}.apk"
                }
            }
        }
    }
    signingConfigs{
        debug{
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
        release{
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.release
        }
    }
    buildFeatures {
        buildConfig true
    }
}
//https://github.com/flutter/flutter/issues/156111
// -Xlint:-options 如果遇到jdk版本警告问题，配置jdk为jdk17

flutter {
    source '../..'
}

dependencies {
    var acraVersion = "5.12.0"
    implementation "ch.acra:acra-http:$acraVersion"
    implementation "ch.acra:acra-core:$acraVersion"
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.10.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.recyclerview:recyclerview:1.3.0'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5'

}
