plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 33
    buildToolsVersion "33.0.1"

    defaultConfig {
        applicationId "com.veilid.veilid_tools_android_tests"
        minSdkVersion 24
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }

        // Required to copy libc++_shared.so
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared"
                targets "cpplink"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    ndkVersion '25.1.8937393'

    // Required to copy libc++_shared.so
    externalNativeBuild {
        cmake {
            version '3.22.1'
            path file('CMakeLists.txt')
        }
    }
    namespace 'com.veilid.veilid_tools_android_tests'
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.security:security-crypto:1.1.0-alpha04'
}

apply plugin: 'org.mozilla.rust-android-gradle.rust-android'

cargo {
    module  = "../../../../../"
    libname = "veilid_tools"
    targets = ["arm", "arm64", "x86", "x86_64"]
    targetDirectory = "../../../../../../target"
    prebuiltToolchains = true
    profile = gradle.startParameter.taskNames.any{it.toLowerCase().contains("debug")} ? "debug" : "release"
    pythonCommand = "python3"
    features {
        defaultAnd("veilid_tools_android_tests", "rt-tokio")
    }
}

afterEvaluate {
    // The `cargoBuild` task isn't available until after evaluation.
    android.applicationVariants.all { variant ->
        def productFlavor = ""
        variant.productFlavors.each {
            productFlavor += "${it.name.capitalize()}"
        }
        def buildType = "${variant.buildType.name.capitalize()}"
        tasks["generate${productFlavor}${buildType}Assets"].dependsOn(tasks["cargoBuild"])
    }
}
