buildscript {
    def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['lottiereactnative_kotlinVersion']

    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
    }

    dependencies {
        classpath("com.android.tools.build:gradle:7.2.2")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
    }
}

def getExtOrDefault(name, defaultValue) {
    rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
}

def isNewArchitectureEnabled() {
    // To opt-in for the New Architecture, you can either:
    // - Set `newArchEnabled` to true inside the `gradle.properties` file
    // - Invoke gradle with `-newArchEnabled=true`
    // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
    return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

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

if (isNewArchitectureEnabled()) {
    apply plugin: 'com.facebook.react'
}

android {
    // Conditional for compatibility with AGP <4.2.
    if (project.android.hasProperty("namespace")) {
        namespace = "com.airbnb.android.react.lottie"
    } else {
        // print
        println "DEPRECATION WARNING: The `namespace` property is not available in your version of AGP. Please upgrade to AGP 4.2+."
    }

    compileSdk getExtOrDefault('compileSdkVersion', 31)

    // Used to override the NDK path/version on internal CI or by allowing
    // users to customize the NDK path/version from their root project (e.g. for M1 support)
    if (rootProject.hasProperty("ndkPath")) {
        ndkPath rootProject.ext.ndkPath
    }
    if (rootProject.hasProperty("ndkVersion")) {
        ndkVersion rootProject.ext.ndkVersion
    }

    defaultConfig {
        minSdkVersion getExtOrDefault('minSdkVersion', 21)
        targetSdkVersion getExtOrDefault('targetSdkVersion', 31)
        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

        ndk {
            abiFilters(*reactNativeArchitectures())
        }
    }

    packagingOptions {
        exclude "**/libreact_render*.so"
    }

    packagingOptions {
        excludes = [
            "META-INF",
            "META-INF/**",
            "**/libc++_shared.so",
            "**/libfabricjni.so",
            "**/libfbjni.so",
            "**/libjsi.so",
            "**/libfolly_json.so",
            "**/libfolly_runtime.so",
            "**/libglog.so",
            "**/libreact_codegen_rncore.so",
            "**/libreact_debug.so",
            "**/libreact_nativemodule_core.so",
            "**/libreact_render_componentregistry.so",
            "**/libreact_render_core.so",
            "**/libreact_render_debug.so",
            "**/libreact_render_graphics.so",
            "**/librrc_view.so",
            "**/libruntimeexecutor.so",
            "**/libturbomodulejsijni.so",
            "**/libyoga.so",
        ]
    }

    sourceSets {
        main {
            if (isNewArchitectureEnabled()) {
                java.srcDirs += ['src/newarch', "${project.buildDir}/generated/source/codegen/java"]
            } else {
                java.srcDirs += ['src/oldarch']
            }
        }
    }
}

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

repositories {
    google()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$projectDir/../node_modules/react-native/android"
    }
    mavenCentral()
}

def kotlin_version = getExtOrDefault('kotlinVersion', project.properties['lottiereactnative_kotlinVersion'])

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+' // From node_modules

    implementation "com.airbnb.android:lottie:6.5.2"
}

if (isNewArchitectureEnabled()) {
  // rncli & react-native-gradle-plugin use codegenConfig in package.json since react-native@0.70
  // Use react{} for compatibility with react-native@0.68/0.69
  react {
    jsRootDir = file("../src/specs")
    libraryName = 'lottiereactnative'
    codegenJavaPackageName = "com.airbnb.android.react.lottie"
  }
}