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 flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
    flutterVersionCode = "1"
}

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

android {
    namespace = "eu.weblibre.gecko"
    compileSdk = 36
    //Temporary fix
    ndkVersion = "27.0.12077973" //flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "eu.weblibre.gecko"
        // https://api.flutter.dev/flutter/widgets/AndroidView-class.html
        minSdkVersion flutter.minSdkVersion
        targetSdk = 36
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
    }

    dependencies {
        // implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        // implementation 'androidx.glance:glance-appwidget:1.0.0'
        implementation 'com.google.android.material:material:1.12.0'
    }

    signingConfigs {
        release {
            keyAlias System.getenv("KEY_ALIAS")
            keyPassword System.getenv("KEY_PASSWORD")
            storeFile System.getenv("KEY_PATH") ? file(System.getenv("KEY_PATH")) : null
            storePassword System.getenv("KEY_PASSWORD")
        }
    }

    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

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            matchingFallbacks = ['release'] // Use on the "release" build type in dependencies (AARs)
        }
    }

    bundle {
        // Profiler issues require us to temporarily package native code compressed to
        // match the previous APK packaging.
        // https://bugzilla.mozilla.org/show_bug.cgi?id=1865634
        packagingOptions {
            jniLibs {
                it.useLegacyPackaging = true
            }
        }
    }

    packagingOptions {
        jniLibs {
            useLegacyPackaging true
        }
    }

    //Exclude dependenciesInfo for f-droid
    dependenciesInfo {
        // Disables dependency metadata when building APKs.
        includeInApk = false
        // Disables dependency metadata when building Android App Bundles.
        includeInBundle = false
    }
}

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

//Override version code for f-droid
ext.abiCodes = ["x86_64": 1, "armeabi-v7a": 2, "arm64-v8a": 3]
import com.android.build.OutputFile
android.applicationVariants.all { variant ->
  variant.outputs.each { output ->
    def abiVersionCode = project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))
    if (abiVersionCode != null) {
      output.versionCodeOverride = variant.versionCode * 10 + abiVersionCode
    }
  }
}
