|
| 1 | +import org.jetbrains.kotlin.config.JvmTarget |
| 2 | +import java.io.ByteArrayOutputStream |
| 3 | + |
| 4 | +plugins { |
| 5 | + id("com.android.application") |
| 6 | + id("org.jetbrains.kotlin.android") |
| 7 | +} |
| 8 | + |
| 9 | +// The last code is 20001, make sure the result is greater than it. |
| 10 | +val vCode = 20000 + getVersionCode() |
| 11 | +val vName = getVersionName() |
| 12 | +logger.lifecycle("App Version: $vName ($vCode)") |
| 13 | + |
| 14 | +android { |
| 15 | + namespace = "cc.chenhe.qqnotifyevo" |
| 16 | + compileSdk = 34 |
| 17 | + defaultConfig { |
| 18 | + applicationId = "cc.chenhe.qqnotifyevo" |
| 19 | + minSdk = 26 |
| 20 | + targetSdk = 33 |
| 21 | + versionCode = vCode |
| 22 | + versionName = vName |
| 23 | + } |
| 24 | + buildFeatures { |
| 25 | + viewBinding = true |
| 26 | + compose = true |
| 27 | + } |
| 28 | + composeOptions { |
| 29 | + // must correspond to kotlin version: https://developer.android.com/jetpack/androidx/releases/compose-kotlin#pre-release_kotlin_compatibility |
| 30 | + kotlinCompilerExtensionVersion = "1.5.3" |
| 31 | + } |
| 32 | + buildTypes { |
| 33 | + release { |
| 34 | + isMinifyEnabled = true |
| 35 | + isShrinkResources = true |
| 36 | + proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro") |
| 37 | + } |
| 38 | + } |
| 39 | + compileOptions { |
| 40 | + sourceCompatibility = JavaVersion.VERSION_1_8 |
| 41 | + targetCompatibility = JavaVersion.VERSION_1_8 |
| 42 | + } |
| 43 | + kotlinOptions { |
| 44 | + jvmTarget = JvmTarget.JVM_1_8.description |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +dependencies { |
| 49 | + val lifecycleVersion = "2.6.2" |
| 50 | + |
| 51 | + // compose |
| 52 | + val composeBom = platform("androidx.compose:compose-bom:2023.10.01") |
| 53 | + implementation(composeBom) |
| 54 | + androidTestImplementation(composeBom) |
| 55 | + implementation("androidx.compose.material3:material3") |
| 56 | + implementation("androidx.compose.material:material-icons-extended") |
| 57 | + implementation("androidx.compose.ui:ui-tooling-preview") |
| 58 | + debugImplementation("androidx.compose.ui:ui-tooling") |
| 59 | + implementation("androidx.activity:activity-compose:1.8.0") |
| 60 | + |
| 61 | + implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion") |
| 62 | + implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycleVersion") |
| 63 | + implementation("androidx.lifecycle:lifecycle-runtime-compose:$lifecycleVersion") |
| 64 | + implementation("androidx.lifecycle:lifecycle-service:$lifecycleVersion") |
| 65 | + |
| 66 | + implementation("androidx.navigation:navigation-compose:2.7.5") |
| 67 | + implementation("androidx.datastore:datastore-preferences:1.0.0") |
| 68 | + implementation("androidx.constraintlayout:constraintlayout:2.1.4") |
| 69 | + implementation("androidx.preference:preference-ktx:1.2.1") |
| 70 | + implementation("androidx.core:core-ktx:1.12.0") |
| 71 | + implementation("androidx.concurrent:concurrent-futures-ktx:1.1.0") // support ListenableFuture |
| 72 | + implementation("com.oasisfeng.nevo:sdk:2.0.0-rc01") |
| 73 | + implementation("com.jakewharton.timber:timber:5.0.1") |
| 74 | + |
| 75 | + testImplementation("junit:junit:4.13.2") |
| 76 | + testImplementation("io.kotest:kotest-assertions-core:5.7.2") |
| 77 | + testImplementation("org.json:json:20231013") // JSONObject |
| 78 | +} |
| 79 | + |
| 80 | +fun String.runCommand(currentWorkingDir: File = file("./")): String { |
| 81 | + val byteOut = ByteArrayOutputStream() |
| 82 | + project.exec { |
| 83 | + workingDir = currentWorkingDir |
| 84 | + commandLine = this@runCommand.split(" ") |
| 85 | + standardOutput = byteOut |
| 86 | + } |
| 87 | + return String(byteOut.toByteArray()).trim() |
| 88 | +} |
| 89 | + |
| 90 | +fun getVersionCode(): Int { |
| 91 | + val cmd = "git rev-list HEAD --first-parent --count" |
| 92 | + return try { |
| 93 | + cmd.runCommand().toInt() |
| 94 | + } catch (e: Exception) { |
| 95 | + logger.error("Failed to get version code with git, return 1 by default.\n${e.message}") |
| 96 | + 1 |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +fun getVersionName(): String { |
| 102 | + val cmd = "git describe --tags --long --first-parent --abbrev=7 --dirty=_dev" |
| 103 | + try { |
| 104 | + val v = cmd.runCommand() |
| 105 | + val pattern = """^v(?<v>[\d|.]+)-\d+-g[A-Za-z0-9]{7}(?<s>_dev)?$""".toRegex() |
| 106 | + val g = pattern.matchEntire(v)?.groups |
| 107 | + if (g == null || g["v"] == null) { |
| 108 | + logger.error( |
| 109 | + "Failed to get version name with git.\n" + |
| 110 | + "Cannot match git tag describe, return <UNKNOWN> by default. raw=$v" |
| 111 | + ) |
| 112 | + return "UNKNOWN" |
| 113 | + } |
| 114 | + return g["v"]!!.value + (g["s"]?.value ?: "") |
| 115 | + } catch (e: Exception) { |
| 116 | + logger.error("Failed to get version name with git, return <UNKNOWN> by default.\n${e.message}") |
| 117 | + return "UNKNOWN" |
| 118 | + } |
| 119 | +} |
0 commit comments