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 {
|
plugins {
|
||||||
alias(libs.plugins.androidApplication)
|
alias(libs.plugins.androidApplication)
|
||||||
alias(libs.plugins.composeMultiplatform)
|
alias(libs.plugins.composeMultiplatform)
|
||||||
alias(libs.plugins.composeCompiler)
|
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 {
|
android {
|
||||||
namespace = "plus.rua.project"
|
namespace = "plus.rua.project"
|
||||||
compileSdk = libs.versions.android.compileSdk.get().toInt()
|
compileSdk = libs.versions.android.compileSdk.get().toInt()
|
||||||
@ -13,7 +29,7 @@ android {
|
|||||||
minSdk = libs.versions.android.minSdk.get().toInt()
|
minSdk = libs.versions.android.minSdk.get().toInt()
|
||||||
targetSdk = libs.versions.android.targetSdk.get().toInt()
|
targetSdk = libs.versions.android.targetSdk.get().toInt()
|
||||||
versionCode = 1
|
versionCode = 1
|
||||||
versionName = "1.0"
|
versionName = appVersionName
|
||||||
|
|
||||||
ndk {
|
ndk {
|
||||||
abiFilters += listOf("arm64-v8a", "armeabi-v7a")
|
abiFilters += listOf("arm64-v8a", "armeabi-v7a")
|
||||||
|
|||||||
@ -9,6 +9,9 @@ org.gradle.caching=true
|
|||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
org.gradle.daemon=true
|
org.gradle.daemon=true
|
||||||
|
|
||||||
|
#App
|
||||||
|
app.version.base=1.0.0
|
||||||
|
|
||||||
#Android
|
#Android
|
||||||
android.nonTransitiveRClass=true
|
android.nonTransitiveRClass=true
|
||||||
android.useAndroidX=true
|
android.useAndroidX=true
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import androidx.activity.BackEventCompat
|
|||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.activity.compose.PredictiveBackHandler
|
import androidx.activity.compose.PredictiveBackHandler
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.flow.Flow
|
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"
|
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
|
@Composable
|
||||||
actual fun PredictiveBackHandler(
|
actual fun PredictiveBackHandler(
|
||||||
enabled: Boolean,
|
enabled: Boolean,
|
||||||
|
|||||||
@ -18,6 +18,9 @@ expect fun getGifUri(gifFile: String): String
|
|||||||
|
|
||||||
expect fun getAppIconUri(): String
|
expect fun getAppIconUri(): String
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
expect fun getAppVersion(): String
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 预测性返回手势处理器(Android 13+)。
|
* 预测性返回手势处理器(Android 13+)。
|
||||||
*
|
*
|
||||||
|
|||||||
@ -27,6 +27,7 @@ import androidx.compose.ui.unit.dp
|
|||||||
import com.github.panpf.sketch.AsyncImage
|
import com.github.panpf.sketch.AsyncImage
|
||||||
import plus.rua.project.AppInfo
|
import plus.rua.project.AppInfo
|
||||||
import plus.rua.project.getAppIconUri
|
import plus.rua.project.getAppIconUri
|
||||||
|
import plus.rua.project.getAppVersion
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 关于页面,展示应用图标、名称、版本号及开源许可入口。
|
* 关于页面,展示应用图标、名称、版本号及开源许可入口。
|
||||||
@ -100,7 +101,7 @@ fun AboutScreen(
|
|||||||
Spacer(modifier = Modifier.height(8.dp))
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
text = "版本:${AppInfo.VERSION}",
|
text = "版本:${getAppVersion()}",
|
||||||
style = MaterialTheme.typography.bodyMedium,
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package plus.rua.project
|
package plus.rua.project
|
||||||
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import platform.Foundation.NSBundle
|
||||||
import platform.UIKit.UIDevice
|
import platform.UIKit.UIDevice
|
||||||
|
|
||||||
class IOSPlatform : Platform {
|
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"
|
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
|
@Composable
|
||||||
actual fun PredictiveBackHandler(
|
actual fun PredictiveBackHandler(
|
||||||
enabled: Boolean,
|
enabled: Boolean,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user