You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 02:25:38