import java.util.Properties

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")
}

val androidSigningPropertiesFile = rootProject.file("key.properties")
val androidSigningProperties = Properties()
if (!androidSigningPropertiesFile.isFile) {
    throw GradleException("Missing Android signing properties: $androidSigningPropertiesFile")
}
androidSigningPropertiesFile.inputStream().use { androidSigningProperties.load(it) }

fun requireSigningProperty(name: String): String {
    val value = androidSigningProperties.getProperty(name)?.trim()
    if (value.isNullOrBlank()) {
        throw GradleException("Missing Android signing property: $name")
    }
    return value
}

android {
    namespace = "com.jinzhi.butler"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    signingConfigs {
        create("release") {
            keyAlias = requireSigningProperty("keyAlias")
            keyPassword = requireSigningProperty("keyPassword")
            storeFile = file(requireSigningProperty("storeFile"))
            storePassword = requireSigningProperty("storePassword")
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11.toString()
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.jinzhi.butler"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName

        val umengChannel = System.getenv("UMENG_CHANNEL") ?: ""
        manifestPlaceholders.putAll(
            mutableMapOf(
                "UMENG_CHANNEL" to umengChannel,
            )
        )
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.getByName("release")
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro",
            )
        }
    }
}

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