OXYGENHUB
# 4c0d52760d2f oxygenhub-android / app / build.gradle.kts
5.5 KiB · last changed CI on OxygenFlow: unit tests with JUnit and JaCoCo coverage, and a debug build, offline (closes #19) 9 days ago source blame history raw
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
    alias(libs.plugins.kotlin.serialization)
8
    alias(libs.plugins.screenshot)
9
}
10
11
// The Rust core (rust-core/, over oxy-core) is built for the app's ABIs by
12
// cargo-ndk and its Kotlin bindings generated by uniffi at build time
13
// (oxygenhub-android#1): nothing built is kept in the repository.
14
val rustCore: File = rootProject.file("rust-core")
15
val rustJniLibs = layout.buildDirectory.dir("rustJniLibs")
16
val rustBindings = layout.buildDirectory.dir("generated/uniffi/kotlin")
17
18
fun androidNdk(): String {
19
    System.getenv("ANDROID_NDK_HOME")?.let { return it }
20
    val props = Properties()
21
    val local = rootProject.file("local.properties")
22
    if (local.exists()) local.inputStream().use { input -> props.load(input) }
23
    val sdk = props.getProperty("sdk.dir") ?: System.getenv("ANDROID_HOME")
24
        ?: error("set sdk.dir in local.properties or ANDROID_HOME")
25
    val ndks = File(sdk, "ndk").listFiles()?.filter { f -> f.isDirectory }?.sortedBy { f -> f.name }.orEmpty()
26
    return ndks.lastOrNull()?.path ?: error("no NDK under $sdk/ndk: install one with the SDK manager")
27
}
28
29
val buildRustCore = tasks.register("buildRustCore", Exec::class) {
30
    description = "Builds rust-core for arm64-v8a and x86_64 with cargo-ndk."
31
    workingDir = rustCore
32
    inputs.dir(rustCore.resolve("src"))
33
    inputs.files(rustCore.resolve("Cargo.toml"), rustCore.resolve("Cargo.lock"))
34
    outputs.dir(rustJniLibs)
35
    environment("ANDROID_NDK_HOME", androidNdk())
36
    commandLine("cargo", "ndk", "-t", "arm64-v8a", "-t", "x86_64", "--platform", "31", "-o", rustJniLibs.get().asFile.path, "build", "--release")
37
}
38
39
val generateRustBindings = tasks.register("generateRustBindings", Exec::class) {
40
    description = "Generates rust-core's Kotlin bindings with uniffi."
41
    dependsOn(buildRustCore)
42
    workingDir = rustCore
43
    inputs.dir(rustJniLibs)
44
    outputs.dir(rustBindings)
45
    commandLine(
46
        "cargo", "run", "--release", "--bin", "uniffi-bindgen", "generate",
47
        "--library", rustJniLibs.get().file("arm64-v8a/librust_core.so").asFile.path,
48
        "--language", "kotlin", "--out-dir", rustBindings.get().asFile.path,
49
    )
50
}
51
52
tasks.named("preBuild") { dependsOn(generateRustBindings) }
53
54
android {
55
    namespace = "io.oxygenhub.android"
56
    compileSdk {
57
        version = release(37)
58
    }
59
60
    defaultConfig {
61
        applicationId = "io.oxygenhub.android"
62
        minSdk = 31
63
        targetSdk = 37
64
        versionCode = 1
65
        versionName = "1.0"
66
67
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
68
    }
69
70
    buildTypes {
71
        // Unit test coverage as JaCoCo XML for the hub's Coverage tab
72
        // (#19): ./gradlew :app:createDebugUnitTestCoverageReport.
73
        debug {
74
            enableUnitTestCoverage = true
75
        }
76
        release {
77
            optimization {
78
                enable = false
79
            }
80
        }
81
    }
82
    compileOptions {
83
        sourceCompatibility = JavaVersion.VERSION_11
84
        targetCompatibility = JavaVersion.VERSION_11
85
    }
86
    packaging {
87
        jniLibs {
88
            useLegacyPackaging = true
89
        }
90
    }
91
    buildFeatures {
92
        compose = true
93
    }
94
    // README screenshots, rendered from previews without an emulator:
95
    // ./gradlew :app:updateDebugScreenshotTest (src/screenshotTest).
96
    experimentalProperties["android.experimental.enableScreenshotTest"] = true
97
    sourceSets["main"].jniLibs.directories.add(rustJniLibs.get().asFile.path)
98
    sourceSets["main"].kotlin.directories.add(rustBindings.get().asFile.path)
99
}
100
101
dependencies {
102
    implementation(libs.jna) { artifact { type = "aar" } }
103
    implementation(platform(libs.androidx.compose.bom))
104
    implementation(libs.androidx.activity.compose)
105
    implementation(libs.androidx.compose.material3)
106
    implementation(libs.androidx.compose.material3.navigation.suite)
107
    implementation(libs.androidx.compose.material3.adaptive)
108
    implementation(libs.androidx.compose.material3.adaptive.layout)
109
    implementation(libs.androidx.compose.material3.adaptive.navigation)
110
    implementation(libs.androidx.compose.material.icons.extended)
111
    implementation(libs.androidx.compose.ui)
112
    implementation(libs.androidx.compose.ui.graphics)
113
    implementation(libs.androidx.compose.ui.tooling.preview)
114
    implementation(libs.androidx.core.ktx)
115
    implementation(libs.androidx.lifecycle.runtime.ktx)
116
    implementation(libs.androidx.lifecycle.runtime.compose)
117
    implementation(libs.androidx.lifecycle.viewmodel.compose)
118
    implementation(libs.androidx.navigation.compose)
119
    implementation(libs.kotlinx.serialization.json)
120
    implementation(libs.kotlinx.coroutines.android)
121
    implementation(libs.okhttp)
122
    implementation(libs.markdown.renderer.m3)
123
    implementation(libs.highlights)
124
    testImplementation(libs.junit)
125
    testImplementation(libs.kotlinx.coroutines.test)
126
    testImplementation(libs.okhttp.mockwebserver)
127
    androidTestImplementation(platform(libs.androidx.compose.bom))
128
    androidTestImplementation(libs.androidx.compose.ui.test.junit4)
129
    androidTestImplementation(libs.androidx.espresso.core)
130
    androidTestImplementation(libs.androidx.junit)
131
    screenshotTestImplementation(libs.screenshot.validation.api)
132
    screenshotTestImplementation(libs.androidx.compose.ui.tooling)
133
    screenshotTestImplementation(libs.kotlinx.coroutines.test)
134
    debugImplementation(libs.androidx.compose.ui.test.manifest)
135
    debugImplementation(libs.androidx.compose.ui.tooling)
136
}