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


static int generateVersionCode(String versionName, int versionCode) {
    def parts = versionName.split("\\.").collect { it.toInteger() }
    return parts[0] * 100000 + parts[1] * 10000 + parts[2] * 1000 + versionCode
}

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

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


android {
    compileSdkVersion 36
    ndkVersion = "27.0.12077973"

    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 "com.foundationdevices.envoy"
        minSdkVersion 29
        targetSdkVersion 35
        versionCode generateVersionCode(flutterVersionName, flutterVersionCode.toInteger())
        versionName flutterVersionName

        ndk {
            abiFilters 'arm64-v8a', 'armeabi-v7a'
        }
    }

    signingConfigs {
        release {
            storeFile file("../key.jks")
            storePassword = System.getenv("KEY_PASSWORD")
            keyAlias = "upload"
            keyPassword = System.getenv("ALIAS_PASSWORD")
        }
    }

    packagingOptions {
        // Fixes duplicate libraries build issue,
        // when your project uses more than one plugin that depend on C++ libs.
        pickFirst 'lib/**/libc++_shared.so'
    }

    buildTypes {
        release {
            if (!project.hasProperty("nosign")) {
                signingConfig signingConfigs.release
            }

            minifyEnabled true
            shrinkResources true

            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    namespace 'com.foundationdevices.envoy'

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_21
        targetCompatibility JavaVersion.VERSION_21
    }

    kotlinOptions {
        jvmTarget = "21"
    }
}

flutter {
    source '../..'
}


dependencies {
    implementation "androidx.core:core-splashscreen:1.0.1"
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5'
}