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

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

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

    sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDir 'src/main/libs'
        }
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "androidx.core:core:1.0.1"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}

def rustBasePath = "src/main/rust"
def cargoOutputDir = "${rustBasePath}/target"
def archTriplets = [
        'arm64-v8a'  : 'aarch64-linux-android',
        'armeabi-v7a': 'armv7-linux-androideabi',
        'x86'        : 'i686-linux-android',
        'x86_64'     : 'x86_64-linux-android',
]

// https://gendignoux.com/blog/2022/10/24/rust-library-android.html
archTriplets.each { arch, target ->
    // Build with cargo
    tasks.create(name: "cargo-build-${arch}", type: Exec, description: "Building core for ${arch}") {
        workingDir rustBasePath
        commandLine 'cargo', 'build', "--target=${target}", '--release'
    }

    // Copy build libs into this app's libs directory
    tasks.create(name: "rust-sync-${arch}", type: Copy, dependsOn: "cargo-build-${arch}", description: "Copy rust libs for (${arch}) to jniLibs") {
        from "${cargoOutputDir}/${target}/release"
        include "*.so"
        into "src/main/libs/${arch}"
    }

    // Hook up tasks to execute before building java
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn "rust-sync-${arch}"
    }
    preBuild.dependsOn "rust-sync-${arch}"

    // Hook up clean tasks
    tasks.create(name: "clean-${arch}", type: Delete, description: "Deleting built libs for ${arch}") {
        delete fileTree("${cargoOutputDir}/${target}/release") {
            include '*.so'
        }
    }

    clean.dependsOn "clean-${arch}"
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives javadocJar
    archives sourcesJar
}

Properties properties = new Properties()
if (project.rootProject.file("local.properties").exists()) {
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
}
