apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'androidx.navigation.safeargs'

def SupportedABIs = ['arm64-v8a']
def WrapScriptABIs = ['arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64']

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.bluehub.mixi"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                if (rootProject.ext.useASAN == true) {
                    arguments "-std=c++14 -DANDROID_ARM_MODE=arm", "-DANDROID_STL=c++_shared"
                } else {
                    cppFlags "-std=c++14 -frtti -fexceptions"
                }
            }
        }
    }

    buildTypes {
        debug {
            manifestPlaceholders = [extractNativeLibs: "true"]

            externalNativeBuild {
                cmake {
                    if (rootProject.ext.useHWASAN == true) {
                        abiFilters.addAll(SupportedABIs)
                        arguments "-DANDROID_STL=c++_shared", "-DUSEHWASAN=ON"
                    } else if (rootProject.ext.useASAN == true) {
                        arguments "-DUSEASAN=ON"
                    }
                }
            }
        }
        release {
            manifestPlaceholders = [extractNativeLibs: "false"]

            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    dataBinding {
        enabled = true
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
}

dependencies {
    implementation(
            fileTree(dir: "libs", include: ["*.jar"]),
            "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version",
            "androidx.core:core-ktx:$core_ktx_version",
            "androidx.appcompat:appcompat:$appcompat_version",
            "androidx.constraintlayout:constraintlayout:$constraint_layout_version",
            "androidx.lifecycle:lifecycle-extensions:$lifecycle_extensions_version",
            "androidx.legacy:legacy-support-v4:$legacy_support_version",
            "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_viewmodel_ktx_version",
            "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version",
            "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_android_version",
            "androidx.navigation:navigation-fragment-ktx:$navigation_fragment_ktx_version",
            "androidx.navigation:navigation-ui-ktx:$navigation_ui_ktx_version",
            "com.google.android.material:material:$android_material_version",
            "com.jakewharton.timber:timber:$timber_version",
            "com.google.dagger:hilt-android:$hilt_version",
            "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_androidx_version",
            "androidx.hilt:hilt-navigation-fragment:$hilt_navigation_version",
            "io.reactivex.rxjava3:rxandroid:$rxjava_version",
            "io.reactivex.rxjava3:rxkotlin:$rxjava_version",
            platform("com.google.firebase:firebase-bom:$firebase_bom_version"),
            "com.google.firebase:firebase-analytics-ktx",
            "com.github.Armen101:AudioRecordView:$audio_record_view_version",
            "com.github.warkiz.widget:indicatorseekbar:$indicator_seekbar_version",
            "com.github.tbruyelle:rxpermissions:$rxpermissions_version"
    )

    testImplementation(
            "junit:junit:$junit_version"
    )

    androidTestImplementation(
            "androidx.test.ext:junit:$ext_version",
            "androidx.test.espresso:espresso-core:$espresso_version"
    )

    kapt(
            "com.google.dagger:hilt-android-compiler:$hilt_version",
            "androidx.hilt:hilt-compiler:$hilt_androidx_version",
    )
}

task deleteWrapDir(type: Delete) {
    delete 'src/main/resources/lib'
}

task copyWrapFile {
    outputs.upToDateWhen { false }

    if (rootProject.ext.useASAN) {
        def folder = new File('src/main/resources/lib')
        def wrapFilePath = file("$rootProject.ext.ndkDir").absolutePath + "/wrap.sh/asan.sh"
        if (!folder.exists()) {
            WrapScriptABIs.each { currentABI ->
                mkdir "src/main/resources/lib/" + currentABI

                copy {
                    from wrapFilePath
                    into "src/main/resources/lib/" + currentABI
                    rename "asan.sh", "wrap.sh"
                }
            }
        }
    }
}

clean.dependsOn(deleteWrapDir)
preBuild.dependsOn(copyWrapFile)
