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

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

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("keystore.properties")
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists()) {
    keystorePropertiesFile.withReader('UTF-8') { reader ->
        keystoreProperties.load(reader)
    }
}

def _keyAlias = System.getenv("KEY_ALIAS") ?: keystoreProperties.getProperty("keyAlias")
def _keyPassword = System.getenv("KEY_PASSWORD") ?: keystoreProperties.getProperty("keyPassword")
def _storeFile = file(System.getenv("KEYSTORE") ?: keystoreProperties.getProperty("storeFile") ?: "keystore.jks")
def _storePassword = System.getenv("KEYSTORE_PASSWORD") ?: keystoreProperties.getProperty("storePassword")

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 33
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {
        applicationId "com.honjow.eros_n"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        archivesBaseName = "Eros-N_v$versionName+$versionCode"
        splits {
            abi {
                enable true
                reset()
                universalApk true
                include "armeabi-v7a", "arm64-v8a", "x86_64"
            }
        }
    }

    signingConfigs {
        debug {
            storeFile _storeFile
            storePassword _storePassword
            keyAlias _keyAlias
            keyPassword _keyPassword
            v1SigningEnabled true
            v2SigningEnabled true
        }
        release {
            storeFile _storeFile
            storePassword _storePassword
            keyAlias _keyAlias
            keyPassword _keyPassword
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }

    buildTypes {
        release {
            if (signingConfigs.release.storePassword != null){
                signingConfig signingConfigs.release
            }
            minifyEnabled true
            shrinkResources true
        }
        debug {
            if (signingConfigs.debug.storePassword != null){
                signingConfig signingConfigs.release
            }
            minifyEnabled true
            shrinkResources true
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "androidx.startup:startup-runtime:1.1.1"
}
