|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 KotlinCrypto |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | +import com.android.build.gradle.tasks.MergeSourceSetFolders |
| 17 | + |
| 18 | +plugins { |
| 19 | + id("configuration") |
| 20 | +} |
| 21 | + |
| 22 | +repositories { google() } |
| 23 | + |
| 24 | +kmpConfiguration { |
| 25 | + configure { |
| 26 | + val jniLibsDir = projectDir |
| 27 | + .resolve("src") |
| 28 | + .resolve("androidInstrumentedTest") |
| 29 | + .resolve("jniLibs") |
| 30 | + |
| 31 | + project.tasks.all { |
| 32 | + if (name != "clean") return@all |
| 33 | + doLast { jniLibsDir.deleteRecursively() } |
| 34 | + } |
| 35 | + |
| 36 | + androidLibrary { |
| 37 | + android { |
| 38 | + buildToolsVersion = "34.0.0" |
| 39 | + compileSdk = 34 |
| 40 | + namespace = "org.kotlincrypto.random.test.android" |
| 41 | + |
| 42 | + defaultConfig { |
| 43 | + minSdk = 21 |
| 44 | + |
| 45 | + testInstrumentationRunnerArguments["disableAnalytics"] = true.toString() |
| 46 | + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" |
| 47 | + } |
| 48 | + |
| 49 | + packaging.jniLibs.useLegacyPackaging = true |
| 50 | + |
| 51 | + sourceSets["androidTest"].jniLibs.srcDir(jniLibsDir) |
| 52 | + } |
| 53 | + |
| 54 | + sourceSetTestInstrumented { |
| 55 | + dependencies { |
| 56 | + implementation(libs.androidx.test.core) |
| 57 | + implementation(libs.androidx.test.runner) |
| 58 | + implementation(libs.kmp.process) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + kotlinJvmTarget = JavaVersion.VERSION_1_8 |
| 63 | + compileSourceCompatibility = JavaVersion.VERSION_1_8 |
| 64 | + compileTargetCompatibility = JavaVersion.VERSION_1_8 |
| 65 | + } |
| 66 | + |
| 67 | + common { |
| 68 | + sourceSetTest { |
| 69 | + dependencies { |
| 70 | + implementation(kotlin("test")) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + kotlin { |
| 76 | + if (!project.plugins.hasPlugin("com.android.base")) return@kotlin |
| 77 | + |
| 78 | + try { |
| 79 | + project.evaluationDependsOn(":library:crypto-rand") |
| 80 | + } catch (_: Throwable) {} |
| 81 | + |
| 82 | + val cryptoRandProject = project(":library:crypto-rand") |
| 83 | + |
| 84 | + val cryptoRandBuildDir = cryptoRandProject |
| 85 | + .layout |
| 86 | + .buildDirectory |
| 87 | + .asFile.get() |
| 88 | + |
| 89 | + val nativeTestBinariesTasks = listOf( |
| 90 | + "Arm32" to "armeabi-v7a", |
| 91 | + "Arm64" to "arm64-v8a", |
| 92 | + "X64" to "x86_64", |
| 93 | + "X86" to "x86", |
| 94 | + ).mapNotNull { (arch, abi) -> |
| 95 | + val nativeTestBinariesTask = cryptoRandProject |
| 96 | + .tasks |
| 97 | + .findByName("androidNative${arch}TestBinaries") |
| 98 | + ?: return@mapNotNull null |
| 99 | + |
| 100 | + val abiDir = jniLibsDir.resolve(abi) |
| 101 | + if (!abiDir.exists() && !abiDir.mkdirs()) throw RuntimeException("mkdirs[$abiDir]") |
| 102 | + |
| 103 | + val testExecutable = cryptoRandBuildDir |
| 104 | + .resolve("bin") |
| 105 | + .resolve("androidNative$arch") |
| 106 | + .resolve("debugTest") |
| 107 | + .resolve("test.kexe") |
| 108 | + |
| 109 | + nativeTestBinariesTask.doLast { |
| 110 | + testExecutable.copyTo(abiDir.resolve("libTestExec.so"), overwrite = true) |
| 111 | + } |
| 112 | + |
| 113 | + nativeTestBinariesTask |
| 114 | + } |
| 115 | + |
| 116 | + project.tasks.withType(MergeSourceSetFolders::class.java) { |
| 117 | + if (name != "mergeDebugAndroidTestJniLibFolders") return@withType |
| 118 | + nativeTestBinariesTasks.forEach { task -> this.dependsOn(task) } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments