|
| 1 | +import org.gradle.api.tasks.Exec |
| 2 | + |
| 3 | +plugins { |
| 4 | + id("com.android.application") |
| 5 | + id("org.jetbrains.kotlin.android") |
| 6 | + id("org.jetbrains.kotlin.plugin.compose") |
| 7 | +} |
| 8 | + |
| 9 | +android { |
| 10 | + namespace = "com.therealaleph.mhrv" |
| 11 | + compileSdk = 34 |
| 12 | + |
| 13 | + defaultConfig { |
| 14 | + applicationId = "com.therealaleph.mhrv" |
| 15 | + minSdk = 24 // Android 7.0 — covers 99%+ of live devices. |
| 16 | + targetSdk = 34 |
| 17 | + versionCode = 1 |
| 18 | + versionName = "0.1.0" |
| 19 | + |
| 20 | + // Only arm64 for now — we can add armeabi-v7a in a second pass |
| 21 | + // if field reports need it. Android emulators on Apple Silicon |
| 22 | + // only run arm64 natively, so keeping things aarch64-only makes |
| 23 | + // the dev loop fast. |
| 24 | + ndk { |
| 25 | + abiFilters += listOf("arm64-v8a") |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + buildTypes { |
| 30 | + release { |
| 31 | + isMinifyEnabled = false |
| 32 | + proguardFiles( |
| 33 | + getDefaultProguardFile("proguard-android-optimize.txt"), |
| 34 | + "proguard-rules.pro", |
| 35 | + ) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + compileOptions { |
| 40 | + sourceCompatibility = JavaVersion.VERSION_17 |
| 41 | + targetCompatibility = JavaVersion.VERSION_17 |
| 42 | + } |
| 43 | + |
| 44 | + kotlinOptions { |
| 45 | + jvmTarget = "17" |
| 46 | + } |
| 47 | + |
| 48 | + buildFeatures { |
| 49 | + compose = true |
| 50 | + buildConfig = true |
| 51 | + } |
| 52 | + |
| 53 | + // libmhrv_rs.so is produced by `cargo ndk` in the repo root and dropped |
| 54 | + // under app/src/main/jniLibs/<abi>/. The cargoBuild task below runs |
| 55 | + // that before each assembleDebug / assembleRelease. |
| 56 | + sourceSets["main"].jniLibs.srcDirs("src/main/jniLibs") |
| 57 | + |
| 58 | + packaging { |
| 59 | + resources.excludes += setOf( |
| 60 | + "META-INF/AL2.0", |
| 61 | + "META-INF/LGPL2.1", |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +dependencies { |
| 67 | + val composeBom = platform("androidx.compose:compose-bom:2024.06.00") |
| 68 | + implementation(composeBom) |
| 69 | + androidTestImplementation(composeBom) |
| 70 | + |
| 71 | + implementation("androidx.core:core-ktx:1.13.1") |
| 72 | + implementation("androidx.activity:activity-compose:1.9.0") |
| 73 | + implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.2") |
| 74 | + implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.2") |
| 75 | + |
| 76 | + // Compose UI. |
| 77 | + implementation("androidx.compose.ui:ui") |
| 78 | + implementation("androidx.compose.ui:ui-graphics") |
| 79 | + implementation("androidx.compose.ui:ui-tooling-preview") |
| 80 | + implementation("androidx.compose.material3:material3") |
| 81 | + implementation("androidx.compose.material:material-icons-extended") |
| 82 | + |
| 83 | + debugImplementation("androidx.compose.ui:ui-tooling") |
| 84 | + debugImplementation("androidx.compose.ui:ui-test-manifest") |
| 85 | +} |
| 86 | + |
| 87 | +// -------------------------------------------------------------------------- |
| 88 | +// Cross-compile the Rust crate to arm64 Android and drop the .so into the |
| 89 | +// place Android's packager looks. We hand the work off to `cargo ndk` which |
| 90 | +// wraps the right CC / AR / linker env vars for us. |
| 91 | +// |
| 92 | +// This ties to the `assemble*` task so every debug/release build triggers |
| 93 | +// a `cargo ndk` — no manual step. In CI we'd cache the target/ dir to |
| 94 | +// avoid full rebuilds. |
| 95 | +// -------------------------------------------------------------------------- |
| 96 | +val rustCrateDir = rootProject.projectDir.parentFile |
| 97 | +val jniLibsDir = file("src/main/jniLibs") |
| 98 | + |
| 99 | +// After cargo-ndk dumps artifacts into jniLibs/arm64-v8a/, the tun2proxy |
| 100 | +// cdylib lands as `libtun2proxy-<hash>.so` (rustc's deps/ naming convention, |
| 101 | +// because tun2proxy is a transitive dep not a root crate). Android's |
| 102 | +// System.loadLibrary expects a stable name, and the hash changes between |
| 103 | +// builds, so we normalize it to `libtun2proxy.so` here. Also deletes any |
| 104 | +// stale hash-suffixed copies from previous builds. |
| 105 | +fun normalizeTun2proxySo() { |
| 106 | + val abiDir = file("src/main/jniLibs/arm64-v8a") |
| 107 | + if (!abiDir.isDirectory) return |
| 108 | + val hashed = abiDir.listFiles { f -> f.name.matches(Regex("libtun2proxy-[0-9a-f]+\\.so")) } |
| 109 | + ?: emptyArray() |
| 110 | + // Keep only the newest (release build) and rename it. |
| 111 | + val newest = hashed.maxByOrNull { it.lastModified() } |
| 112 | + if (newest != null) { |
| 113 | + val target = abiDir.resolve("libtun2proxy.so") |
| 114 | + if (target.exists()) target.delete() |
| 115 | + newest.copyTo(target, overwrite = true) |
| 116 | + } |
| 117 | + hashed.forEach { it.delete() } |
| 118 | +} |
| 119 | + |
| 120 | +tasks.register<Exec>("cargoBuildDebug") { |
| 121 | + group = "build" |
| 122 | + // Intentionally ALWAYS uses --release. The Rust debug build is 80+MB |
| 123 | + // of unoptimized object code vs 3MB with release; the 20x APK bloat is |
| 124 | + // never worth it just for a Rust stack trace you wouldn't see in |
| 125 | + // logcat anyway. If you need Rust debug symbols, temporarily drop |
| 126 | + // `--release` below and accept the APK size. |
| 127 | + description = "Cross-compile mhrv_rs for arm64-v8a (release — same as cargoBuildRelease)" |
| 128 | + workingDir = rustCrateDir |
| 129 | + commandLine( |
| 130 | + "cargo", "ndk", |
| 131 | + "-t", "arm64-v8a", |
| 132 | + "-o", jniLibsDir.absolutePath, |
| 133 | + "build", "--release", |
| 134 | + ) |
| 135 | + doLast { normalizeTun2proxySo() } |
| 136 | +} |
| 137 | + |
| 138 | +tasks.register<Exec>("cargoBuildRelease") { |
| 139 | + group = "build" |
| 140 | + description = "Cross-compile mhrv_rs for arm64-v8a (release)" |
| 141 | + workingDir = rustCrateDir |
| 142 | + commandLine( |
| 143 | + "cargo", "ndk", |
| 144 | + "-t", "arm64-v8a", |
| 145 | + "-o", jniLibsDir.absolutePath, |
| 146 | + "build", "--release", |
| 147 | + ) |
| 148 | + doLast { normalizeTun2proxySo() } |
| 149 | +} |
| 150 | + |
| 151 | +// Hook the right cargo task in front of each Android build variant. |
| 152 | +tasks.configureEach { |
| 153 | + when (name) { |
| 154 | + "mergeDebugJniLibFolders" -> dependsOn("cargoBuildDebug") |
| 155 | + "mergeReleaseJniLibFolders" -> dependsOn("cargoBuildRelease") |
| 156 | + } |
| 157 | +} |
0 commit comments