import org.gradle.api.tasks.Copy import java.time.Instant plugins { id("com.android.application") id("org.jetbrains.kotlin.android") id("org.jetbrains.kotlin.plugin.compose") } fun buildSetting(propertyName: String, environmentName: String) = providers.gradleProperty(propertyName).orElse(providers.environmentVariable(environmentName)) val buildInstant = Instant.now() val gitRevision = runCatching { val process = ProcessBuilder("git", "rev-parse", "--short=12", "HEAD") .directory(rootProject.projectDir.parentFile) .redirectErrorStream(true) .start() val output = process.inputStream.bufferedReader().use { it.readText().trim() } if (process.waitFor() == 0 && output.matches(Regex("[0-9a-fA-F]{7,40}"))) output else null }.getOrNull() val isGitDirty = runCatching { val process = ProcessBuilder("git", "status", "--porcelain") .directory(rootProject.projectDir.parentFile) .redirectErrorStream(true) .start() val output = process.inputStream.bufferedReader().use { it.readText() } process.waitFor() == 0 && output.isNotBlank() }.getOrDefault(false) val defaultBuildId = "${gitRevision ?: "local"}${if (isGitDirty) "-dirty" else ""}-${buildInstant.toEpochMilli()}" val appVersion = buildSetting("appVersion", "APP_VERSION").orElse("0.1.0").get() val appBuildId = buildSetting("appBuildId", "APP_BUILD_ID") .orElse(defaultBuildId) .get() val appVersionCode = buildSetting("appVersionCode", "APP_VERSION_CODE") .orElse(buildInstant.epochSecond.toString()) .get() .toIntOrNull() ?: error("appVersionCode/APP_VERSION_CODE must be a positive 32-bit integer") require(appVersionCode in 1..2_100_000_000) { "appVersionCode/APP_VERSION_CODE must be between 1 and 2,100,000,000" } require(appVersion.isNotBlank() && appBuildId.isNotBlank()) { "appVersion/APP_VERSION and appBuildId/APP_BUILD_ID must not be blank" } require(appVersion.matches(Regex("[0-9]+\\.[0-9]+\\.[0-9]+(?:-[A-Za-z0-9.-]+)?"))) { "appVersion/APP_VERSION must be a semantic version such as 0.1.0 or 0.1.0-rc.1" } require(appBuildId.matches(Regex("[A-Za-z0-9._-]+"))) { "appBuildId/APP_BUILD_ID may contain only letters, numbers, dot, underscore, and hyphen" } val safeBuildLabel = "${appVersion}-$appBuildId-$appVersionCode" .replace(Regex("[^A-Za-z0-9._-]"), "_") android { namespace = "com.ee2e.app" compileSdk = 35 defaultConfig { applicationId = "com.ee2e.app" minSdk = 29 targetSdk = 35 versionCode = appVersionCode versionName = "$appVersion+$appBuildId" } compileOptions { sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 } kotlin { compilerOptions { jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_21) } } buildFeatures { compose = true } } tasks.register("archiveDebugApk") { group = "build" description = "Copies the debug APK to a versioned filename for build tracking." dependsOn("assembleDebug") from(layout.buildDirectory.file("outputs/apk/debug/app-debug.apk")) into(layout.buildDirectory.dir("outputs/versioned-apk")) rename { "ee2e-phone-calls-$safeBuildLabel-debug.apk" } } dependencies { implementation(platform("androidx.compose:compose-bom:2024.12.01")) implementation("androidx.activity:activity-compose:1.10.0") implementation("androidx.compose.material3:material3") implementation("androidx.compose.ui:ui") implementation("androidx.compose.ui:ui-tooling-preview") debugImplementation("androidx.compose.ui:ui-tooling") }