// android/build.gradle

// based on:
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle
//   original location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/build.gradle
//
// * https://github.com/facebook/react-native/blob/0.60-stable/template/android/app/build.gradle
//   original location:
//   - https://github.com/facebook/react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/app/build.gradle

def DEFAULT_COMPILE_SDK_VERSION = 32
def DEFAULT_MIN_SDK_VERSION = 21
def DEFAULT_TARGET_SDK_VERSION = 31
def NATIVE_ANDROID_SDK_VERSION = "16.3.+"

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

def androidExclusion = [
        '**/databinding/**/*.*',
        '**/android/databinding/*Binding.*',
        '**/BR.*',
        '**/R.*',
        '**/R$*.*',
        '**/BuildConfig.*',
        '**/Manifest*.*',
        '**/*_MembersInjector.*',
        '**/Dagger*Component.*',
        '**/Dagger*Component$Builder.*',
        '**/*Module_*Factory.*',
        '**/*Fragment*.*',
        '**/*Activity*.*',
        '**/*Adapter*.*',
        '**/*ViewPager*.*',
        '**/*ViewHolder*.*',
        '**/*Module*.*'
]

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.
    // ref: https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:build_script_external_dependencies
    if (project == rootProject) {
        repositories {
            mavenCentral()
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:7.4.2'
        }
    }
}

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'jacoco'

apply from: 'publish.gradle'

android {
    compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        consumerProguardFiles 'proguard-rules.pro'
    }
    lintOptions {
        abortOnError false
    }

    buildFeatures {
        buildConfig = false
    }
  
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    // ref: https://www.baeldung.com/maven-local-repository
    mavenLocal()
    mavenCentral()
    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()
    jcenter()
}

dependencies {
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules
    implementation "com.onfido.sdk.capture:onfido-capture-sdk:$NATIVE_ANDROID_SDK_VERSION"
    implementation "com.onfido.sdk:onfido-workflow:$NATIVE_ANDROID_SDK_VERSION"
    implementation "com.squareup.okhttp3:logging-interceptor:3.14.9"
    implementation "com.squareup.okhttp3:okhttp:3.14.9"
    // Fix for crash due to 'java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/swiperefreshlayout/widget/SwipeRefreshLayout;''
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.1'
    testCompileOnly 'junit:junit:4.13.2'
    testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.10.1'
    testImplementation 'org.mockito:mockito-core:2.+'
    testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9'
    testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9'
}

afterEvaluate { project ->
    android.libraryVariants.all { variant ->
        def name = variant.name.capitalize()
        def javaCompileTask = variant.javaCompileProvider.get()

        task "jar${name}"(type: Jar, dependsOn: javaCompileTask) {
            from javaCompileTask.destinationDir
        }
    }

    task codeCoverageReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
        group = "Reporting"
        description = "Generate Jacoco coverage reports after running tests."
        reports {
            xml.enabled = true
            html.enabled = true
            csv.enabled = true
        }
        classDirectories.setFrom(fileTree(
                dir: 'build/intermediates/javac/debug/compileDebugJavaWithJavac/classes/com/onfido/reactnative/sdk',
                excludes: androidExclusion
        ))
        sourceDirectories.setFrom(files('src/main/java/com/onfido/reactnative/sdk'))
        executionData.setFrom(files('build/jacoco/testDebugUnitTest.exec'))
    }

    task getCoverage(type: Exec, dependsOn: 'codeCoverageReport') {
        group = "Reporting"
        commandLine "open", "$buildDir/reports/jacoco/codeCoverageReport/html/index.html"
    }
}
