plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    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 keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file("key.properties")
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

def versionProperties = new Properties()
def versionPropertiesFile = rootProject.file("version.properties")
if (versionPropertiesFile.exists()) {
    versionProperties.load(new FileInputStream(versionPropertiesFile))
}

def flutterVersionCode = versionProperties.getProperty("flutter.android.versionCode")
if (flutterVersionCode == null) {
    flutterVersionCode = "5"
}

def flutterVersionName = versionProperties.getProperty("flutter.android.versionName")
if (flutterVersionName == null) {
    flutterVersionName = "1.0.4"
}

android { 
    namespace = "com.taihe.vbvs_app"
//    compileSdk = flutter.compileSdkVersion
    compileSdk = localProperties.getProperty('flutter.compileSdkVersion').toInteger()

    ndkVersion = localProperties.getProperty('flutter.ndkVersion')

    // 编译的jvm版本
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    // 指定Kotilin的jvm版本
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.taihe.vbvs_app"
        // 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.
//        minSdk = flutter.minSdkVersion
//        targetSdk = flutter.targetSdkVersion
        minSdk = localProperties.getProperty('flutter.minSdkVersion').toInteger()
        targetSdk = localProperties.getProperty('flutter.targetSdkVersion').toInteger()
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled true//是否启动混淆 ture:打开   false:关闭
            shrinkResources true  //删除无用资源(压缩)
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
//            signingConfig = signingConfigs.debug
            signingConfig signingConfigs.release
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "THYH_v${variant.versionName}-${releaseTime()}.apk"
                }
            }
        }
        debug {
            debuggable true
            // signingConfig signingConfigs.release
        }
    }
}

static def releaseTime() {
    return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}

flutter {
    source = "../.."
}
