| 1 |
import java.io.File |
| 2 |
import java.util.Properties |
| 3 |
|
| 4 |
plugins { |
| 5 |
alias(libs.plugins.android.application) |
| 6 |
alias(libs.plugins.kotlin.compose) |
| 7 |
} |
| 8 |
|
| 9 |
// The Rust core (rust-core/, over oxy-core) is built for the app's ABIs by |
| 10 |
// cargo-ndk and its Kotlin bindings generated by uniffi at build time |
| 11 |
// (oxygenhub-android#1): nothing built is kept in the repository. |
| 12 |
val rustCore: File = rootProject.file("rust-core") |
| 13 |
val rustJniLibs = layout.buildDirectory.dir("rustJniLibs") |
| 14 |
val rustBindings = layout.buildDirectory.dir("generated/uniffi/kotlin") |
| 15 |
|
| 16 |
fun androidNdk(): String { |
| 17 |
System.getenv("ANDROID_NDK_HOME")?.let { return it } |
| 18 |
val props = Properties() |
| 19 |
val local = rootProject.file("local.properties") |
| 20 |
if (local.exists()) local.inputStream().use { input -> props.load(input) } |
| 21 |
val sdk = props.getProperty("sdk.dir") ?: System.getenv("ANDROID_HOME") |
| 22 |
?: error("set sdk.dir in local.properties or ANDROID_HOME") |
| 23 |
val ndks = File(sdk, "ndk").listFiles()?.filter { f -> f.isDirectory }?.sortedBy { f -> f.name }.orEmpty() |
| 24 |
return ndks.lastOrNull()?.path ?: error("no NDK under $sdk/ndk: install one with the SDK manager") |
| 25 |
} |
| 26 |
|
| 27 |
val buildRustCore = tasks.register("buildRustCore", Exec::class) { |
| 28 |
description = "Builds rust-core for arm64-v8a and x86_64 with cargo-ndk." |
| 29 |
workingDir = rustCore |
| 30 |
inputs.dir(rustCore.resolve("src")) |
| 31 |
inputs.files(rustCore.resolve("Cargo.toml"), rustCore.resolve("Cargo.lock")) |
| 32 |
outputs.dir(rustJniLibs) |
| 33 |
environment("ANDROID_NDK_HOME", androidNdk()) |
| 34 |
commandLine("cargo", "ndk", "-t", "arm64-v8a", "-t", "x86_64", "--platform", "31", "-o", rustJniLibs.get().asFile.path, "build", "--release") |
| 35 |
} |
| 36 |
|
| 37 |
val generateRustBindings = tasks.register("generateRustBindings", Exec::class) { |
| 38 |
description = "Generates rust-core's Kotlin bindings with uniffi." |
| 39 |
dependsOn(buildRustCore) |
| 40 |
workingDir = rustCore |
| 41 |
inputs.dir(rustJniLibs) |
| 42 |
outputs.dir(rustBindings) |
| 43 |
commandLine( |
| 44 |
"cargo", "run", "--release", "--bin", "uniffi-bindgen", "generate", |
| 45 |
"--library", rustJniLibs.get().file("arm64-v8a/librust_core.so").asFile.path, |
| 46 |
"--language", "kotlin", "--out-dir", rustBindings.get().asFile.path, |
| 47 |
) |
| 48 |
} |
| 49 |
|
| 50 |
tasks.named("preBuild") { dependsOn(generateRustBindings) } |
| 51 |
|
| 52 |
android { |
| 53 |
namespace = "io.oxygenhub.android" |
| 54 |
compileSdk { |
| 55 |
version = release(37) |
| 56 |
} |
| 57 |
|
| 58 |
defaultConfig { |
| 59 |
applicationId = "io.oxygenhub.android" |
| 60 |
minSdk = 31 |
| 61 |
targetSdk = 37 |
| 62 |
versionCode = 1 |
| 63 |
versionName = "1.0" |
| 64 |
|
| 65 |
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" |
| 66 |
} |
| 67 |
|
| 68 |
buildTypes { |
| 69 |
release { |
| 70 |
optimization { |
| 71 |
enable = false |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
compileOptions { |
| 76 |
sourceCompatibility = JavaVersion.VERSION_11 |
| 77 |
targetCompatibility = JavaVersion.VERSION_11 |
| 78 |
} |
| 79 |
packaging { |
| 80 |
jniLibs { |
| 81 |
useLegacyPackaging = true |
| 82 |
} |
| 83 |
} |
| 84 |
buildFeatures { |
| 85 |
compose = true |
| 86 |
} |
| 87 |
sourceSets["main"].jniLibs.directories.add(rustJniLibs.get().asFile.path) |
| 88 |
sourceSets["main"].kotlin.directories.add(rustBindings.get().asFile.path) |
| 89 |
} |
| 90 |
|
| 91 |
dependencies { |
| 92 |
implementation("net.java.dev.jna:jna:5.17.0@aar") |
| 93 |
implementation(platform(libs.androidx.compose.bom)) |
| 94 |
implementation(libs.androidx.activity.compose) |
| 95 |
implementation(libs.androidx.compose.material3) |
| 96 |
implementation("androidx.compose.material:material-icons-extended") |
| 97 |
implementation(libs.androidx.compose.ui) |
| 98 |
implementation(libs.androidx.compose.ui.graphics) |
| 99 |
implementation(libs.androidx.compose.ui.tooling.preview) |
| 100 |
implementation(libs.androidx.core.ktx) |
| 101 |
implementation(libs.androidx.lifecycle.runtime.ktx) |
| 102 |
testImplementation(libs.junit) |
| 103 |
androidTestImplementation(platform(libs.androidx.compose.bom)) |
| 104 |
androidTestImplementation(libs.androidx.compose.ui.test.junit4) |
| 105 |
androidTestImplementation(libs.androidx.espresso.core) |
| 106 |
androidTestImplementation(libs.androidx.junit) |
| 107 |
debugImplementation(libs.androidx.compose.ui.test.manifest) |
| 108 |
debugImplementation(libs.androidx.compose.ui.tooling) |
| 109 |
} |