在Kotlin Multiplatform Moblie中查找应用版本代码

发布于 2025-01-21 06:38:17 字数 1341 浏览 3 评论 0 原文

我想在KMM中找到应用程序版本代码。在特定平台IE

Android

var versionCode = BuildConfig.VERSION_CODE

它将返回 1.1

ios

let appVersion = "iOS " + (Bundle.main.versionNumber ?? "")

它将返回 1.1

我如何在特定的KMM中进行平台?

更新

我尝试了期望/实际,但我会遇到一些错误。

Commanmain

expect class Platform() {
    val versionCode: String
}

iosmain

actual class Platform actual constructor() {
    actual val versionCode =
        platform.Foundation.NSBundle.mainBundle.infoDictionary?.get("CFBundleVersion")
}

iOS侧上的错误

”在此处输入图像说明”

androidmain

actual class Platform actual constructor() {
    actual val versionCode = BuildConfig.VERSION_CODE
}

Android side上的错误/strong>

alt

I want to find application version code in Kmm. In specific platform i.e.

Android

var versionCode = BuildConfig.VERSION_CODE

it will return 1.1

iOS

let appVersion = "iOS " + (Bundle.main.versionNumber ?? "")

it will return 1.1

How can I do in Kmm in specific platform?

UPDATE

I tried expect/actual but I am getting some error.

CommonMain

expect class Platform() {
    val versionCode: String
}

iosMain

actual class Platform actual constructor() {
    actual val versionCode =
        platform.Foundation.NSBundle.mainBundle.infoDictionary?.get("CFBundleVersion")
}

Error on iOS side

enter image description here

androidMain

actual class Platform actual constructor() {
    actual val versionCode = BuildConfig.VERSION_CODE
}

Error on android side

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如此安好 2025-01-28 06:38:17

KMM对此类功能没有内置的支持。

一个选项是创建期望函数,并在每个平台中应用实际中的平台特定代码。

我曾经将应用程序版本与模块版本同步,但是如果您不能执行此操作,则可以在应用程序模块中创建 Platform ,然后将其传递给您的公共模块:

common Main:

expect class Platform { // remove constructor here
    val versionCode: String
}

Android Main:

actual class Platform(actual val versionCode: String)

然后应用模块您只需创建 platform(buildconfig.version_code.tostring()),然后根据您的体系结构将其传递到您的共享模块中。


另一个选项是使用 buildkonfig plugin:它将像Android插件一样生成配置文件,例如Android插件您在 build.gradle 文件中指定的内容:

buildkonfig {
    packageName = "com.example.app"

    defaultConfigs {
        buildConfigField(STRING, "VERSION_CODE", "1.0")
    }
}

用法:

com.example.app.BuildKonfig.VERSION_CODE

KMM doesn't have some built in support for such feature.

One option is to create an expect function and apply platform-specific code in actual for each platform.

I used to sync app version with module version, but if you can't do that, you can create Platform in your app module and pass it to your common module:

Common main:

expect class Platform { // remove constructor here
    val versionCode: String
}

Android main:

actual class Platform(actual val versionCode: String)

Then in app module you just create it Platform(BuildConfig.VERSION_CODE.toString()) and pass it into your shared module depending on your architecture.


Another option is to use BuildKonfig plugin: it will generate a configuration file like the Android plugin does, depending on what you specify in the build.gradle file:

buildkonfig {
    packageName = "com.example.app"

    defaultConfigs {
        buildConfigField(STRING, "VERSION_CODE", "1.0")
    }
}

Usage:

com.example.app.BuildKonfig.VERSION_CODE
┊风居住的梦幻卍 2025-01-28 06:38:17

如果您想要共享模块/框架版本,我只会使用期望/实际

但是,如果您构建了KMM应用程序,我怀疑您需要的版本是应用程序版本,而不是 shared 模块版本。

我会选择一个 appConfig 接口/协议,类似:

interface AppConfig {
    val version: String
}

然后提供 iosappConfig androidappConfig 从您的 android>代码>和 iosapp

I would only use expect/actual if you want the shared module/framework version.

If you're building a KMM app, however, I'd suspect that the version you need is the app version instead, not the shared module's version.

I'd go with an AppConfig interface/protocol, something like:

interface AppConfig {
    val version: String
}

Then provide the IosAppConfig and AndroidAppConfig implementations from your androidApp and iosApp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文