// The Android Gradle Plugin builds the native code with the Android NDK.

group = "flutter.soloud.flutter_soloud"
version = "1.0"

buildscript {
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        // The Android Gradle Plugin knows how to build native code with the NDK.
        classpath("com.android.tools.build:gradle:8.7.0")
    }
}

rootProject.allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

apply plugin: "com.android.library"

android {
    namespace = "flutter.soloud.flutter_soloud"

    // Bumping the plugin compileSdk version requires all clients of this plugin
    // to bump the version in their app.
    compileSdk = 35

    // Use the NDK version
    // declared in /android/app/build.gradle file of the Flutter project.
    // Replace it with a version number if this plugin requires a specific NDK version.
    // (e.g. ndkVersion "23.1.7779620")
    ndkVersion = android.ndkVersion

    // Invoke the shared CMake build with the Android Gradle Plugin.
    externalNativeBuild {
        cmake {
            path = "./CMakeLists.txt"

            // The default CMake version for the Android Gradle Plugin is 3.10.2.
            // https://developer.android.com/studio/projects/install-ndk#vanilla_cmake
            //
            // The Flutter tooling requires that developers have CMake 3.10 or later
            // installed. You should not increase this version, as doing so will cause
            // the plugin to fail to compile for some customers of the plugin.
            // version "3.10.2"
        }
    }

    defaultConfig {
        minSdk = 21

        // Add this inside defaultConfig to affect all variants
        externalNativeBuild {
            cmake {
                // Pass NO_OPUS_OGG_LIBS to CMake from multiple sources:
                // 1. gradle project property (preferred)
                // 2. system environment variable (fallback)
                def noOpusOggLibs = project.hasProperty('NO_OPUS_OGG_LIBS') ?
                    project.property('NO_OPUS_OGG_LIBS') :
                    (System.getenv('NO_OPUS_OGG_LIBS') ?: 'false')

                arguments "-DNO_OPUS_OGG_LIBS=${noOpusOggLibs}"

                // Specify the correct target name
                targets "flutter_soloud_plugin"
            }
        }
    }

    // Task to check if NO_OPUS_OGG_LIBS has changed. If yes the cmake task will be re-run
    tasks.whenTaskAdded { task ->
        if (task.name.contains("externalNativeBuild")) {
            // println "=== Flutter SoLoud Plugin ==="
            // println "NO_OPUS_OGG_LIBS=${System.getenv('NO_OPUS_OGG_LIBS') ?: 'false'}"
            
            // Always run CMake configuration
            task.outputs.upToDateWhen { false }
        }
    }

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