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

buildscript {
    // The Android Gradle plugin is only required when opening the android folder stand-alone.
    // This avoids unnecessary downloads and potential conflicts when the library is included as a
    // module dependency in an application project.
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.5.3'
        }
    }
}

apply plugin: 'com.android.library'

// Whether the app has expo-image (in a node_modules folder above the app's
// android folder, as Node finds it), which ships its own AppGlideModule.
def appHasExpoImage = {
    for (def dir = rootProject.projectDir; dir != null; dir = dir.parentFile) {
        if (new File(dir, "node_modules/expo-image/package.json").isFile()) return true
    }
    return false
}
// An app has one AppGlideModule: with expo-image, FastImage's is left out,
// and FastImage registers its Glide components itself (FastImageGlide).
def excludeAppGlideModule = safeExtGet('excludeAppGlideModule', appHasExpoImage())

// When changing the default, revisit the Glide rules in consumer-rules.pro.
// 4.15+ animates animated WebP (Android 9+) and AVIF (Android 12+), which
// FastImageAnimated builds on. A version that isn't numbered (e.g. "4.+") is
// taken to be recent.
def glideVersion = safeExtGet('glideVersion', '4.16.0')
def glideVersionParts = glideVersion.toString() =~ /^(\d+)\.(\d+)/
if (glideVersionParts.find() &&
        (glideVersionParts.group(1).toInteger() < 4 ||
                (glideVersionParts.group(1).toInteger() == 4 && glideVersionParts.group(2).toInteger() < 15))) {
    throw new GradleException("react-native-fast-image needs Glide 4.15 or later, but glideVersion is ${glideVersion}.")
}

// Newer versions of the Android Gradle plugin (7.3+) take the namespace from
// build.gradle and warn about the package attribute in AndroidManifest.xml;
// older versions need that attribute.
def supportsNamespace = project.android.hasProperty("namespace")

android {
    if (supportsNamespace) {
        namespace "com.dylanvann.fastimage"
    }
    compileSdkVersion safeExtGet('compileSdkVersion', 28)
    buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 21)
        targetSdkVersion safeExtGet('targetSdkVersion', 28)
        versionCode 1
        versionName "1.0"
        // Applied to the app's release build, so apps don't need to add them.
        consumerProguardFiles 'consumer-rules.pro'
    }
    sourceSets {
        main {
            if (supportsNamespace) {
                manifest.srcFile "src/main/AndroidManifestNew.xml"
            }
            java {
                if (excludeAppGlideModule) {
                    exclude "**/FastImageGlideModule.java"
                }
            }
        }
    }
    lintOptions {
        abortOnError false
    }
}

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

dependencies {
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules
    implementation "com.github.bumptech.glide:glide:${glideVersion}"
    // FastImageGlideModule and FastImageOkHttpProgressGlideModule use @GlideModule.
    implementation "com.github.bumptech.glide:annotations:${glideVersion}"
    implementation "com.github.bumptech.glide:okhttp3-integration:${glideVersion}"
    annotationProcessor "com.github.bumptech.glide:compiler:${glideVersion}"
    // SVG images (see FastImageSvg). The package expo-image uses too, so
    // apps with it get one copy. An app with the other package
    // (com.caverock:androidsvg, the same classes) excludes this one (see
    // docs/troubleshooting.md); FastImageSvg works with either.
    implementation 'com.caverock:androidsvg-aar:1.4'
    // Animated PNGs (see FastImageApng).
    implementation 'com.github.penfeizhou.android.animation:apng:3.0.5'
}

// Unit tests of the downloads (src/test, with Robolectric, which gives them
// Android's classes), run from an example app's android folder:
// `./gradlew :react-native-fast-image:testDebugUnitTest`. The npm package
// doesn't include src/test, so apps don't get these.
if (file('src/test').exists()) {
    android.testOptions.unitTests.includeAndroidResources = true
    dependencies {
        testImplementation 'junit:junit:4.13.2'
        testImplementation 'org.robolectric:robolectric:4.15.1'
    }
}
