buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
    mavenCentral()
    google()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:7.2.2'
  }
}

def resolveBuildType() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()

    return tskReqStr.contains('Release') ? 'release' : 'debug'
}

apply plugin: 'com.android.library'

def safeExtGet(prop, fallback) {
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

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

def USE_HERMES = rootProject.ext.hermesEnabled

repositories {
  mavenCentral()
}

android {
  compileSdkVersion safeExtGet("compileSdkVersion", 31)

  def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
    if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
        namespace "com.ammarahmed.mmkv"
    }

  if (rootProject.hasProperty("ndkPath")) {
        ndkPath rootProject.ext.ndkPath
    }
    if (rootProject.hasProperty("ndkVersion")) {
        ndkVersion rootProject.ext.ndkVersion
    }

  buildFeatures {
    prefab true
  }

  

  defaultConfig {
    minSdkVersion safeExtGet('minSdkVersion', 19)
    targetSdkVersion safeExtGet('targetSdkVersion', 31)
    var appProject = rootProject.allprojects.find {it.plugins.hasPlugin('com.android.application')}
    externalNativeBuild {
      cmake {
        cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
        arguments "-DANDROID_STL=c++_shared", "-DUSE_HERMES=${USE_HERMES}"
        abiFilters (*reactNativeArchitectures())
      }
    }
    packagingOptions {
    doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : ''
    excludes = [
            "META-INF",
            "META-INF/**",
            "**/libfbjni.so",
            "**/libjsi.so",
            "**/libreact_nativemodule_core.so",
            "**/libturbomodulejsijni.so",
            "**/libc++_shared.so"
      ]
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  externalNativeBuild {
     cmake {
          path "./src/main/rnmmkv/CMakeLists.txt"
      }
  }
}

dependencies {
  //noinspection GradleDynamicVersion
  implementation 'com.facebook.react:react-android:+'
  implementation "androidx.security:security-crypto:1.1.0-alpha03"
  implementation 'com.google.code.gson:gson:2.8.6'
}

