Files
KlipperAnywhere/android/app/build.gradle.kts
T
2026-05-14 02:27:26 +01:00

215 lines
6.3 KiB
Kotlin

import java.io.ByteArrayOutputStream
import java.io.File
import java.util.Properties
import org.gradle.api.GradleException
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
fun Project.readAndroidSdkDir(): File {
val localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
val properties = Properties().apply {
localPropertiesFile.inputStream().use { load(it) }
}
properties.getProperty("sdk.dir")?.let { sdkDir ->
return File(sdkDir)
}
}
val envSdkDir = providers.environmentVariable("ANDROID_SDK_ROOT").orNull
?: providers.environmentVariable("ANDROID_HOME").orNull
if (envSdkDir != null) {
return File(envSdkDir)
}
throw GradleException("Android SDK not found. Set sdk.dir in local.properties or ANDROID_SDK_ROOT.")
}
fun Project.adbExecutable(): File {
val binary = if (System.getProperty("os.name").contains("Windows", ignoreCase = true)) "adb.exe" else "adb"
return File(readAndroidSdkDir(), "platform-tools/$binary")
}
data class AdbTarget(
val serial: String,
val shouldConnect: Boolean,
)
fun Project.resolveWifiAdbTarget(): AdbTarget {
val explicitTarget = providers.gradleProperty("wifiAdbTarget")
.orElse(providers.gradleProperty("wifiAdbSerial"))
.orNull
?.trim()
?.takeIf { it.isNotBlank() }
if (explicitTarget != null) {
return AdbTarget(serial = explicitTarget, shouldConnect = true)
}
val stdout = ByteArrayOutputStream()
project.exec {
commandLine(adbExecutable().absolutePath, "devices")
standardOutput = stdout
isIgnoreExitValue = true
}
val devices = stdout
.toString(Charsets.UTF_8.name())
.lineSequence()
.drop(1)
.mapNotNull { line ->
val trimmed = line.trim()
if (trimmed.isBlank() || trimmed.startsWith("*")) return@mapNotNull null
val serial = trimmed.substringBefore('\t')
val status = trimmed.substringAfter('\t', "").substringBefore(' ')
if (status != "device") return@mapNotNull null
serial
}
.toList()
if (devices.isEmpty()) {
throw GradleException(
"No adb device is connected. Connect one over Wi-Fi or pass -PwifiAdbTarget=host:port.",
)
}
val wifiDevices = devices.filter { ":" in it }
if (wifiDevices.size == 1) {
return AdbTarget(serial = wifiDevices.single(), shouldConnect = false)
}
if (devices.size == 1) {
return AdbTarget(serial = devices.single(), shouldConnect = false)
}
throw GradleException(
buildString {
appendLine("Multiple adb devices are connected:")
devices.forEach { appendLine(" - $it") }
append("Pass -PwifiAdbTarget=host:port or -PwifiAdbSerial=serial to choose one.")
},
)
}
android {
namespace = "com.klipperanywhere.android"
compileSdk = 34
defaultConfig {
applicationId = "com.klipperanywhere.android"
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "0.1.0"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.14"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
implementation(platform("androidx.compose:compose-bom:2024.06.00"))
implementation("androidx.activity:activity-compose:1.9.0")
implementation("androidx.compose.foundation:foundation")
implementation("androidx.compose.material:material")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-text")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
implementation("androidx.security:security-crypto:1.1.0-alpha06")
implementation("com.google.android.material:material:1.12.0")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("com.squareup.okhttp3:okhttp-urlconnection:4.12.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")
implementation("io.coil-kt:coil-compose:2.6.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")
debugImplementation("androidx.compose.ui:ui-tooling")
}
val adbFile = adbExecutable()
val debugApk = layout.buildDirectory.file("outputs/apk/debug/app-debug.apk")
val launchActivity = "com.klipperanywhere.android/.MainActivity"
tasks.register("wifiAdbInstallDebug") {
group = "install"
description = "Installs the debug APK to a Wi-Fi ADB device."
dependsOn("assembleDebug")
doLast {
val target = resolveWifiAdbTarget()
if (target.shouldConnect) {
project.exec {
commandLine(adbFile.absolutePath, "connect", target.serial)
}
}
project.exec {
commandLine(adbFile.absolutePath, "-s", target.serial, "install", "-r", debugApk.get().asFile.absolutePath)
}
}
}
tasks.register("wifiAdbRunDebug") {
group = "run"
description = "Installs the debug APK to a Wi-Fi ADB device and launches the app."
dependsOn("wifiAdbInstallDebug")
doLast {
val target = resolveWifiAdbTarget()
project.exec {
commandLine(
adbFile.absolutePath,
"-s",
target.serial,
"shell",
"am",
"start",
"-n",
launchActivity,
"-a",
"android.intent.action.MAIN",
"-c",
"android.intent.category.LAUNCHER",
)
}
}
}
tasks.register("killAdbServer") {
group = "adb"
description = "Stops the local adb server."
doLast {
project.exec {
commandLine(adbFile.absolutePath, "kill-server")
}
}
}