feat: 支持动态版本号生成与读取
- build.gradle.kts 根据 baseVersion + git hash + buildDate 生成 versionName - gradle.properties 添加 app.version.base=1.0.0 - 新增 expect/actual getAppVersion(),Android 从 PackageInfo 读取,iOS 从 NSBundle 读取 - AboutScreen 使用 getAppVersion() 替代硬编码 AppInfo.VERSION Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
5bb0cc7071
commit
a0b1b4b569
@ -1,9 +1,25 @@
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.androidApplication)
|
||||
alias(libs.plugins.composeMultiplatform)
|
||||
alias(libs.plugins.composeCompiler)
|
||||
}
|
||||
|
||||
val baseVersion = findProperty("app.version.base") as? String ?: "1.0.0"
|
||||
|
||||
val gitHash = try {
|
||||
providers.exec {
|
||||
commandLine("git", "rev-parse", "--short=5", "HEAD")
|
||||
}.standardOutput.asText.get().trim()
|
||||
} catch (_: Exception) {
|
||||
"unknown"
|
||||
}
|
||||
|
||||
val buildDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMyy"))
|
||||
val appVersionName = "${baseVersion}_${gitHash}_${buildDate}"
|
||||
|
||||
android {
|
||||
namespace = "plus.rua.project"
|
||||
compileSdk = libs.versions.android.compileSdk.get().toInt()
|
||||
@ -13,7 +29,7 @@ android {
|
||||
minSdk = libs.versions.android.minSdk.get().toInt()
|
||||
targetSdk = libs.versions.android.targetSdk.get().toInt()
|
||||
versionCode = 1
|
||||
versionName = "1.0"
|
||||
versionName = appVersionName
|
||||
|
||||
ndk {
|
||||
abiFilters += listOf("arm64-v8a", "armeabi-v7a")
|
||||
|
||||
@ -9,6 +9,9 @@ org.gradle.caching=true
|
||||
org.gradle.parallel=true
|
||||
org.gradle.daemon=true
|
||||
|
||||
#App
|
||||
app.version.base=1.0.0
|
||||
|
||||
#Android
|
||||
android.nonTransitiveRClass=true
|
||||
android.useAndroidX=true
|
||||
|
||||
@ -5,6 +5,7 @@ import androidx.activity.BackEventCompat
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.PredictiveBackHandler
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@ -18,6 +19,25 @@ actual fun getGifUri(gifFile: String): String = "file:///android_asset/gifs/$gif
|
||||
|
||||
actual fun getAppIconUri(): String = "file:///android_asset/app_icon.png?v=2"
|
||||
|
||||
@Composable
|
||||
actual fun getAppVersion(): String {
|
||||
val context = LocalContext.current.applicationContext
|
||||
return try {
|
||||
val packageInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
context.packageManager.getPackageInfo(
|
||||
context.packageName,
|
||||
android.content.pm.PackageManager.PackageInfoFlags.of(0)
|
||||
)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
context.packageManager.getPackageInfo(context.packageName, 0)
|
||||
}
|
||||
packageInfo.versionName ?: "unknown"
|
||||
} catch (_: Exception) {
|
||||
"unknown"
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
actual fun PredictiveBackHandler(
|
||||
enabled: Boolean,
|
||||
|
||||
@ -18,6 +18,9 @@ expect fun getGifUri(gifFile: String): String
|
||||
|
||||
expect fun getAppIconUri(): String
|
||||
|
||||
@Composable
|
||||
expect fun getAppVersion(): String
|
||||
|
||||
/**
|
||||
* 预测性返回手势处理器(Android 13+)。
|
||||
*
|
||||
|
||||
@ -27,6 +27,7 @@ import androidx.compose.ui.unit.dp
|
||||
import com.github.panpf.sketch.AsyncImage
|
||||
import plus.rua.project.AppInfo
|
||||
import plus.rua.project.getAppIconUri
|
||||
import plus.rua.project.getAppVersion
|
||||
|
||||
/**
|
||||
* 关于页面,展示应用图标、名称、版本号及开源许可入口。
|
||||
@ -100,7 +101,7 @@ fun AboutScreen(
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Text(
|
||||
text = "版本:${AppInfo.VERSION}",
|
||||
text = "版本:${getAppVersion()}",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package plus.rua.project
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import platform.Foundation.NSBundle
|
||||
import platform.UIKit.UIDevice
|
||||
|
||||
class IOSPlatform : Platform {
|
||||
@ -14,6 +15,15 @@ actual fun getGifUri(gifFile: String): String = "compose.resource://files/$gifFi
|
||||
|
||||
actual fun getAppIconUri(): String = "compose.resource://files/app_icon.png?v=2"
|
||||
|
||||
@Composable
|
||||
actual fun getAppVersion(): String {
|
||||
return try {
|
||||
NSBundle.mainBundle.objectForInfoDictionaryKey("CFBundleShortVersionString") as? String ?: "unknown"
|
||||
} catch (_: Throwable) {
|
||||
"unknown"
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
actual fun PredictiveBackHandler(
|
||||
enabled: Boolean,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user