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

def _ext = rootProject.ext
def _compileSdkVersion = _ext.has('compileSdkVersion') ? _ext.compileSdkVersion : 29
def _targetSdkVersion = _ext.has('targetSdkVersion') ? _ext.targetSdkVersion : 29
def _buildToolsVersion = _ext.has('buildToolsVersion') ? _ext.buildToolsVersion : '29.0.3'
def _minSdkVersion = _ext.has('minSdkVersion') ? _ext.minSdkVersion : 18
def _kotlinVersion = _ext.has('detoxKotlinVersion') ? _ext.detoxKotlinVersion : '1.2.0'
def _kotlinStdlib = _ext.has('detoxKotlinStdlib') ? _ext.detoxKotlinStdlib : 'kotlin-stdlib-jdk8'

android {
    compileSdkVersion _compileSdkVersion
    buildToolsVersion _buildToolsVersion
    defaultConfig {
        minSdkVersion _minSdkVersion
        targetSdkVersion _targetSdkVersion
        versionCode 1
        versionName '1.0'

        consumerProguardFiles 'proguard-rules.pro'
    }

    testOptions {
        unitTests.includeAndroidResources = true
        unitTests.returnDefaultValues = true

        unitTests.all { t ->
            reports {
                html.enabled true
            }
            testLogging {
                events "passed", "skipped", "failed", "standardOut", "standardError"
            }
            afterSuite { desc, result ->
                if (!desc.parent) { // will match the outermost suite
                    def output = "      ${result.resultType} (${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)     "
                    def repeatLength = output.length()
                    println '\n' + ('-' * repeatLength) + '\n' + output + '\n' + ('-' * repeatLength) + '\n'

                    println "see report at file://${t.reports.html.destination}/index.html"
                }
            }
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    lintOptions {
        abortOnError false
    }
}

// In a nutshell:
// "The api configuration should be used to declare dependencies which are exported by the library API, whereas the
//  implementation configuration should be used to declare dependencies which are internal to the component."
// --> https://docs.gradle.org/5.5/userguide/java_library_plugin.html

// Fundamental deps.
dependencies {
    implementation "org.jetbrains.kotlin:$_kotlinStdlib:$_kotlinVersion"

    // noinspection GradleDynamicVersion
    compileOnly 'com.facebook.react:react-native:+'
}

// androidx.test deps.
// All are aligned with this release: https://developer.android.com/jetpack/androidx/releases/test#1.2.0
dependencies {

    // Versions are in-sync with the 'androidx-test-1.2.0' release/tag of the android-test github repo,
    // used by the Detox generator. See https://github.com/android/android-test/releases/tag/androidx-test-1.2.0
    // Important: Should remain so when generator tag is replaced!
    api('androidx.test.espresso:espresso-core:3.2.0') { // Needed all across Detox but also makes Espresso seamlessly provided to Detox users with hybrid apps/E2E-tests.
        exclude group: 'com.google.code.findbugs', module: 'jsr305'
    }
    api 'androidx.test:rules:1.2.0' // Needed because of ActivityTestRule. Needed by users *and* internally used by Detox.
    api 'androidx.test.ext:junit:1.1.1' // Needed so as to seamlessly provide AndroidJUnit4 to Detox users. Depends on junit core.

    // Version is the latest; Cannot sync with the Github repo (e.g. android/android-test) because the androidx
    // packaging version of associated classes is simply not there...
    api 'androidx.test.uiautomator:uiautomator:2.2.0' // Needed by Detox but also makes UIAutomator seamlessly provided to Detox users with hybrid apps/E2E-tests.
}

// Third-party deps.
dependencies {
    implementation 'org.apache.commons:commons-lang3:3.7' // Needed by invoke. Warning: Upgrading to newer versions is not seamless.
    implementation 'com.github.anrwatchdog:anrwatchdog:1.4.0'
}

// Unit-testing deps.
dependencies {
    // noinspection GradleDynamicVersion
    testImplementation 'com.facebook.react:react-native:+'
    testImplementation 'org.json:json:20140107'
    testImplementation 'junit:junit:4.13'
    testImplementation 'org.assertj:assertj-core:3.16.1'
    testImplementation "org.jetbrains.kotlin:kotlin-test:$_kotlinVersion"
    testImplementation 'org.apache.commons:commons-io:1.3.2'
    testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0'
    testImplementation 'org.robolectric:robolectric:4.3.1'
}

// Spek (https://spekframework.org/setup-android)
if (rootProject.hasProperty('isOfficialDetoxLib') ||
    rootProject.hasProperty('isOfficialDetoxApp')) {
    apply plugin: 'de.mannodermaus.android-junit5'

    android {
        testOptions {
            junitPlatform {
                filters {
                    engines {
                        include 'spek2'
                    }
                }
            }
        }
    }

    dependencies {
        testImplementation 'org.spekframework.spek2:spek-dsl-jvm:2.0.12'
        testImplementation 'org.spekframework.spek2:spek-runner-junit5:2.0.12'
        testImplementation "org.jetbrains.kotlin:kotlin-reflect:$_kotlinVersion"
    }
}

// Enable publishing
if (rootProject.hasProperty('isOfficialDetoxLib')) {
    apply from: './detox-publishing.gradle'
}
