在Kotlin Multiplatform Moblie中查找应用版本代码
我想在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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
KMM对此类功能没有内置的支持。
一个选项是创建
期望
函数,并在每个平台中应用实际
中的平台特定代码。我曾经将应用程序版本与模块版本同步,但是如果您不能执行此操作,则可以在应用程序模块中创建
Platform
,然后将其传递给您的公共模块:common Main:
Android Main:
然后应用模块您只需创建
platform(buildconfig.version_code.tostring())
,然后根据您的体系结构将其传递到您的共享模块中。另一个选项是使用 buildkonfig plugin:它将像Android插件一样生成配置文件,例如Android插件您在
build.gradle
文件中指定的内容:用法:
KMM doesn't have some built in support for such feature.
One option is to create an
expect
function and apply platform-specific code inactual
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:
Android main:
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:Usage:
如果您想要
共享
模块/框架版本,我只会使用期望/实际
。但是,如果您构建了KMM应用程序,我怀疑您需要的版本是应用程序版本,而不是
shared
模块版本。我会选择一个
appConfig
接口/协议,类似:然后提供
iosappConfig
和androidappConfig
从您的android>代码>和
iosapp
。I would only use
expect/actual
if you want theshared
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:Then provide the
IosAppConfig
andAndroidAppConfig
implementations from yourandroidApp
andiosApp
.