apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"
apply plugin: "com.huawei.agconnect"

react {
  autolinkLibrariesWithApp()
}

def enableProguardInReleaseBuilds = false
def jscFlavor = "org.webkit:android-jsc:+"

/**
 * Hermes flag safety:
 * - If you have `hermesEnabled=true/false` in gradle.properties it will use it
 * - Otherwise defaults to true (RN default in many setups)
 */
def isHermesEnabled = (project.hasProperty("hermesEnabled")
        ? project.property("hermesEnabled").toString().toBoolean()
        : true)

android {
  namespace "com.huawei.hms.rn.ads.demo"

  // Make it independent from rootProject.ext (AGP 8 friendly)
  compileSdk 35

  defaultConfig {
    applicationId "<package_name>"
    minSdk 24
    targetSdk 34
    versionCode 1
    versionName "1.0"
  }

  // If you want to keep your NDK pin:
  ndkVersion "27.0.12077973"

  // AGP 8: you generally don't need buildToolsVersion; remove to avoid mismatch.
  // buildToolsVersion "35.0.0"

  signingConfigs {
    debug {
      storeFile file('<keystore_file>')
            storePassword '<keystore_password>'
            keyAlias '<key_alias>'
            keyPassword '<key_password>'
    }
  }

  buildTypes {
    debug {
      signingConfig signingConfigs.debug
    }
    release {
      signingConfig signingConfigs.debug
      minifyEnabled enableProguardInReleaseBuilds
      proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = "17"
  }

  packaging {
    // Keep empty unless you hit duplicate META-INF issues
    resources {
      excludes += ["/META-INF/{AL2.0,LGPL2.1}"]
    }
  }
}

dependencies {
  // RN Gradle plugin sets the correct RN version
  implementation("com.facebook.react:react-android")

  if (isHermesEnabled) {
    implementation("com.facebook.react:hermes-android")
  } else {
    implementation jscFlavor
  }
}