Compose Desktop打包MSI后Gson失效:寻求报错原因分析
Compose Desktop打包后Gson反序列化Settings类失败问题
问题现象
开发Compose Desktop小型项目时,实现了处理Settings的Gson相关函数,在IntelliJ IDEA中运行完全正常,但打包为Windows的MSI或EXE安装包后,出现如下错误:
Unable to create instance of class serializables.Settings. Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.
暂未按照错误提示的方案解决问题,希望先了解导致该报错的根本原因。
相关代码
Gson处理Settings的工具函数
package utilities import com.google.gson.Gson import serializables.Settings import java.io.File fun CheckSettingsFile () { if (!File("settings.json").exists()) { File("settings.json").createNewFile() with(File("settings.json")) { val settings = Settings("192.168.1.254","3344") val gson = Gson() gson.newBuilder().setPrettyPrinting() val toWrite = gson.toJson(settings) this.writeText(toWrite) } } } fun SaveSettings (s : Settings) { val gson = Gson() gson.newBuilder().setPrettyPrinting() val serialized = gson.toJson(s) try { with(File("settings.json")) { this.writeText(serialized) } } catch (err :Exception) { println(err.message) } } fun LoadSettings () : Settings { val gson = Gson() return gson.fromJson(File("settings.json").readText(), Settings::class.java) }
Gradle配置(settings.gradle.kts)
import org.jetbrains.compose.desktop.application.dsl.TargetFormat plugins { kotlin("jvm") id("org.jetbrains.compose") } group = "com.eticket" version = "1.0-SNAPSHOT" repositories { mavenCentral() maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") google() } dependencies { implementation("com.google.code.gson:gson:2.10.1") implementation(compose.desktop.currentOs) } compose.desktop { application { mainClass = "MainKt" nativeDistributions { targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb, TargetFormat.Exe) packageName = "eTicket" packageVersion = "1.0.0" windows { shortcut = true iconFile.set(project.file("stamp-icon.ico")) console = false menu = true } } } }
分析思路请求
恳请提供导致该报错的根本原因分析思路。
内容的提问来源于stack exchange,提问作者NicolaM94
相关产品推荐
相关产品推荐

