修改GJS应用Meson输出文件名后报错,求解决运行需输应用ID问题
资源加载错误修复
你遇到的ImportError是修改src/meson.build后,GResource路径配置与代码中导入的资源路径不匹配导致的,按以下步骤排查:
检查
src/meson.build中的资源编译配置
确保gnome.compile_resources的resource_prefix和代码里的资源前缀完全一致。比如你代码导入的是resource:///org/example/app/js/main.js,配置里的resource_prefix必须是/org/example/app,同时sources要包含对应的.gresource.xml文件,示例配置:gnome.compile_resources( 'exampleapp-resources', 'exampleapp.gresource.xml', output: 'org.example.app.gresource', resource_prefix: '/org/example/app', c_name: 'exampleapp', )验证
.gresource.xml文件路径
打开src/exampleapp.gresource.xml(文件名可能不同),确认<gresource>的prefix和meson配置中的resource_prefix一致,且<file>标签路径正确指向main.js:<gresources> <gresource prefix="/org/example/app"> <file>js/main.js</file> <!-- 其他资源如ui、css等也需正确配置 --> </gresource> </gresources>注意
<file>的路径是相对于.gresource.xml所在目录的相对路径,比如main.js在src/js/下,就写js/main.js。
解决运行需输入应用ID的问题
要不用终端输入ID就能启动,需要配置桌面文件并安装应用:
修改可执行文件配置
在src/meson.build的executable块中添加install: true和gui_app: true,示例:executable( 'exampleapp', 'main.js', # 启动脚本,需正确导入资源中的主逻辑文件 dependencies: dependencies, install: true, gui_app: true, )生成桌面文件
在src/meson.build中添加桌面文件生成配置,这样GNOME菜单会显示你的应用:gnome.make_desktop_entry( 'exampleapp', 'Example App', # 菜单中显示的应用名称 'org.example.app', # 图标名称(需提前放置图标文件并配置) categories: 'Utility;Development;', )
最后步骤
完成配置后,在GNOME Builder中点击Clean清除旧构建文件,再点击Build重新构建,之后就能通过GNOME菜单启动应用,且资源加载错误会被修复。
内容的提问来源于stack exchange,提问作者Behzad

