plugins {
  id 'org.jetbrains.kotlin.multiplatform' version '1.3.71'
}

apply plugin: 'com.android.library'

android {
  compileSdkVersion 29

  defaultConfig {
    minSdkVersion 16
    targetSdkVersion 29
  }
}

dependencies {
  // Specify Kotlin/JVM stdlib dependency.
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

kotlin {
  jvm()
  js {
    browser {}
  }
  android()
  androidNativeArm32()
  androidNativeArm64()

  targets {
    fromPreset(presets.androidNativeArm32, 'androidNativeArm32')
    fromPreset(presets.androidNativeArm64, 'androidNativeArm64')

    configure([androidNativeArm32, androidNativeArm64]) {
      binaries {
        sharedLib {
          baseName = "ktnes"
        }
      }
    }
  }

  sourceSets {
    armMain {
    }
    androidNativeArm32().compilations.main.defaultSourceSet {
      dependsOn armMain
    }
    androidNativeArm64().compilations.main.defaultSourceSet {
      dependsOn armMain
    }
    commonMain {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
      }
    }
    jvmMain {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
      }
    }
    jsMain {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
      }
    }
    jvmTest {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
        implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        implementation 'com.google.truth:truth:1.0.1'
        implementation 'org.mockito:mockito-core:1.10.19'
        implementation "org.robolectric:robolectric:3.8"
      }
    }
  }
}

def platforms = [
    "androidNativeArm32": "armeabi-v7a",
    "androidNativeArm64": "arm64-v8a"
]

afterEvaluate {
  task copyLibs(type: Copy) {
    def buildType = project.findProperty("kotlin.build.type") ?: "DEBUG"
    destinationDir project(":android").file("src/main/jniLibs")
    platforms.each { name, platform ->
      into(platform) {
        from kotlin.targets."$name".binaries.getSharedLib(buildType).outputFile
      }
    }
  }

  assemble {
    finalizedBy copyLibs
  }
}