plugins {
    id 'com.android.application'
    id 'com.github.willir.rust.cargo-ndk-android'
}

android {
    namespace "io.github.doukutsu_rs"

    /**
     * NOTE: If you want to change the versions of packages required for the build
     * (e.g. build tools, compile SDK, NDK) or add new ones,
     * make the appropriate changes in packages.txt. Otherwise CI could break.
     */
    compileSdkVersion 33
    buildToolsVersion "33.0.0"
    ndkVersion "25.2.9519653"

    defaultConfig {
        applicationId "io.github.doukutsu_rs"
        minSdkVersion 24
        targetSdkVersion 33
        versionCode 3
        versionName "0.102.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        ndk {
            abiFilters 'arm64-v8a'
            stl = "c++_shared"
        }

        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared"
            }
        }

        def documentsAuthorityValue = applicationId + ".documents"
        manifestPlaceholders = [documentsAuthority: documentsAuthorityValue]
        buildConfigField "String",
                "DOCUMENTS_AUTHORITY",
                "\"${documentsAuthorityValue}\""

        resValue "string", "app_name", "doukutsu-rs"
    }

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

            ndk {
                abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
                stl = "c++_shared"
            }

            packagingOptions {
                resources {
                    excludes += "**/DebugProbesKt.bin"
                }
            }
        }
        debug {
            applicationIdSuffix ".debug"
            resValue "string", "app_name", "doukutsu-rs (debug)"

            jniDebuggable true
            renderscriptDebuggable true

            def documentsAuthorityValue =
                    android.defaultConfig.applicationId + applicationIdSuffix + ".documents"
            manifestPlaceholders = [documentsAuthority: documentsAuthorityValue]
            buildConfigField "String",
                    "DOCUMENTS_AUTHORITY",
                    "\"${documentsAuthorityValue}\""
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures {
        viewBinding true
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
    
    packagingOptions {
        jniLibs {
            excludes += "**/dummy.so"
        }

    }
}

dependencies {
    implementation 'androidx.annotation:annotation:1.5.0'
    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'androidx.core:core:1.9.0'
    implementation 'com.google.android.material:material:1.8.0'
}

println("cargo target: ${project.buildDir.getAbsolutePath()}/rust-target")
println("ndk dir: ${android.ndkDirectory}")

cargoNdk {
    librariesNames = ["libdrsandroid.so"]
    module = "../drsandroid/"
    extraCargoEnv = ["ANDROID_NDK_HOME": android.ndkDirectory]
    extraCargoBuildArguments = []
    verbose = true

    buildTypes {
        release {
            buildType = "release"
            extraCargoBuildArguments = ["--locked"]
            targets = [
                "arm64",
                "arm",
                "x86",
                "x86_64"
            ]
        }
        debug {
            buildType = "debug"
            targets = [
                "arm64"
            ]
        }
    }
}

tasks.whenTaskAdded { task ->
    if (task.name == 'javaPreCompileDebug') {
        task.dependsOn 'buildCargoNdkDebug'
    }

    if (task.name == 'javaPreCompileRelease') {
        task.dependsOn 'buildCargoNdkRelease'
    }
}
