def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['Sound_' + name]
}

def reactNativeArchitectures() {
  def value = rootProject.getProperties().get("reactNativeArchitectures")
  return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"

apply from: '../nitrogen/generated/android/NitroSound+autolinking.gradle'

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Sound_" + name]).toInteger()
}

android {
  namespace "com.margelo.nitro.sound"

  compileSdk getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdk getExtOrIntegerDefault("minSdkVersion")
    targetSdk getExtOrIntegerDefault("targetSdkVersion")
    ndkVersion getExtOrDefault("ndkVersion")


    externalNativeBuild {
      cmake {
        cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
        arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
        abiFilters (*reactNativeArchitectures())
      }
    }

    // Ship ProGuard/R8 consumer rules so apps don't need manual keeps
    consumerProguardFiles "consumer-rules.pro"
  }

  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"
    }
  }

  // Prevent bundling RN core JNI libs into this AAR to avoid duplicates
  // in consuming apps (e.g., libjsi.so, libreactnative.so). These are
  // provided by React Native itself and should not be packaged here.
  packagingOptions {
    // New AGP DSL: configure resources + jniLibs explicitly
    resources {
      excludes += [
              "META-INF",
              "META-INF/**"
      ]
    }
    jniLibs {
      excludes += [
              "**/libjsi.so",
              "**/libreactnative.so",
              "**/libfbjni.so",
              "**/libc++_shared.so",
              "**/libNitroModules.so"
      ]
    }
  }

  buildFeatures {
    buildConfig true
    prefab true
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  sourceSets {
    main {
      java.srcDirs += [
        "generated/java",
        "generated/jni"
      ]
    }
  }
}

repositories {
  mavenCentral()
  google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")

dependencies {
  implementation "com.facebook.react:react-android"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
  implementation project(":react-native-nitro-modules")
}
