Skip to content

Commit

Permalink
refactor: Move versioning logic to root build.gradle.kts
Browse files Browse the repository at this point in the history
Moved the versioning logic and constants for `versionCode` and `versionName` from the app module's `build.gradle.kts` file to the root `build.gradle.kts` file.

This change centralizes version management and ensures consistency across modules. It also simplifies the app module's build file. Additionally, incremented the version build number to 8.
  • Loading branch information
BobbyESP committed Nov 28, 2024
1 parent e33cab2 commit 5a354ef
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 66 deletions.
62 changes: 3 additions & 59 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ plugins {
alias(libs.plugins.compose.compiler)
}

val commitSignature = providers.exec {
commandLine("git", "rev-parse", "--short", "HEAD")
}.standardOutput.asText.get().substringBefore("\n")

val currentVersion: Version = Version.Beta(
versionMajor = 1,
versionMinor = 0,
versionPatch = 0,
versionBuild = 7
)

val localProperties = Properties().apply {
load(project.rootDir.resolve("local.properties").inputStream())
}
Expand All @@ -36,8 +25,8 @@ android {
minSdk = 24
targetSdk = 35

versionCode = currentVersion.toVersionCode()
versionName = currentVersion.toVersionName()
versionCode = rootProject.extra["versionCode"] as Int
versionName = rootProject.extra["versionName"] as String

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -98,7 +87,7 @@ android {
}
kotlinOptions {
jvmTarget = "21"
freeCompilerArgs = listOf("-Xcontext-receivers", "-XXLanguage:+ExplicitBackingFields")
//freeCompilerArgs = listOf("-Xcontext-receivers", "-XXLanguage:+ExplicitBackingFields")
}
buildFeatures {
compose = true
Expand Down Expand Up @@ -220,49 +209,4 @@ class RoomSchemaArgProvider(
}
return listOf("room.schemaLocation=${schemaDir.path}")
}
}

sealed class Version(
open val versionMajor: Int,
val versionMinor: Int,
val versionPatch: Int,
val versionBuild: Int = 0,
val commitId: String = ""
) {
abstract fun toVersionName(): String

fun toVersionCode(): Int {
val minorExtraDigit = if (versionMinor > 9) {
(versionMinor / 10).toString()
} else {
""
}

return "$versionMajor$minorExtraDigit$versionPatch$versionBuild".toInt()
}

class Stable(versionMajor: Int, versionMinor: Int, versionPatch: Int) :
Version(versionMajor, versionMinor, versionPatch) {
override fun toVersionName(): String = "${versionMajor}.${versionMinor}.${versionPatch}"
}

class ReleaseCandidate(
versionMajor: Int, versionMinor: Int, versionPatch: Int, versionBuild: Int
) : Version(versionMajor, versionMinor, versionPatch, versionBuild) {
override fun toVersionName(): String =
"${versionMajor}.${versionMinor}.${versionPatch}-rc.$versionBuild"
}

class Beta(versionMajor: Int, versionMinor: Int, versionPatch: Int, versionBuild: Int) :
Version(versionMajor, versionMinor, versionPatch, versionBuild) {
override fun toVersionName(): String =
"${versionMajor}.${versionMinor}.${versionPatch}-beta.$versionBuild"
}

class Alpha(
versionMajor: Int, versionMinor: Int, versionPatch: Int, commitId: String
) : Version(versionMajor, versionMinor, versionPatch, commitId = commitId) {
override fun toVersionName(): String =
"${versionMajor}.${versionMinor}.${versionPatch}-alpha.$commitId"
}
}
61 changes: 60 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,63 @@ plugins {

val isGoogleMobileServicesBuild by extra {
gradle.startParameter.taskNames.none { task -> task.contains("foss", ignoreCase = true) }
}
}

val commitSignature = providers.exec {
commandLine("git", "rev-parse", "--short", "HEAD")
}.standardOutput.asText.get().substringBefore("\n")

sealed class Version(
open val versionMajor: Int,
val versionMinor: Int,
val versionPatch: Int,
val versionBuild: Int = 0,
val commitId: String = ""
) {
abstract fun toVersionName(): String

fun toVersionCode(): Int {
val minorExtraDigit = if (versionMinor > 9) {
(versionMinor / 10).toString()
} else {
""
}

return "$versionMajor$minorExtraDigit$versionPatch$versionBuild".toInt()
}

class Stable(versionMajor: Int, versionMinor: Int, versionPatch: Int) :
Version(versionMajor, versionMinor, versionPatch) {
override fun toVersionName(): String = "${versionMajor}.${versionMinor}.${versionPatch}"
}

class ReleaseCandidate(
versionMajor: Int, versionMinor: Int, versionPatch: Int, versionBuild: Int
) : Version(versionMajor, versionMinor, versionPatch, versionBuild) {
override fun toVersionName(): String =
"${versionMajor}.${versionMinor}.${versionPatch}-rc.$versionBuild"
}

class Beta(versionMajor: Int, versionMinor: Int, versionPatch: Int, versionBuild: Int) :
Version(versionMajor, versionMinor, versionPatch, versionBuild) {
override fun toVersionName(): String =
"${versionMajor}.${versionMinor}.${versionPatch}-beta.$versionBuild"
}

class Alpha(
versionMajor: Int, versionMinor: Int, versionPatch: Int, commitId: String
) : Version(versionMajor, versionMinor, versionPatch, commitId = commitId) {
override fun toVersionName(): String =
"${versionMajor}.${versionMinor}.${versionPatch}-alpha.$commitId"
}
}

val currentVersion: Version = Version.Beta(
versionMajor = 1,
versionMinor = 0,
versionPatch = 0,
versionBuild = 8
)

val versionCode by extra(currentVersion.toVersionCode())
val versionName by extra(currentVersion.toVersionName())
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object CrashHandler {
reportInfo: ReportInfo = ReportInfo(),
logfilePath: String
) {
startActivity(context, Intent(context, CrashHandlerActivity::class.java).apply {
context.startActivity(Intent(context, CrashHandlerActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
putExtra("version_report", getVersionReport(packageInfo, reportInfo))
putExtra("logfile_path", logfilePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bobbyesp.crashhandler
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -19,11 +20,7 @@ import java.io.File
class CrashHandlerActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { v, insets ->
v.setPadding(0, 0, 0, 0)
insets
}
enableEdgeToEdge()
val versionReport: String = intent.getStringExtra("version_report").toString()
val logfilePath: String = intent.getStringExtra("logfile_path").toString()

Expand Down

0 comments on commit 5a354ef

Please sign in to comment.