不知道大家是否先前试过我的一个Qt的应用"如何把一个qmake的Ubuntu手机应用打包为一个snap应用".在那个教程中,我们尝试把所有的Ubuntu SDK库包ubuntu-sdk-libs 都打入到我们的应用中,结果是我们的snap包的文件非常大.几乎达到800M-900M.显然这样的应用不具有实用性.我也尝试只把我们最需要的Qt库打入到我们的snap包中.你们可以发现我更新后的代码.即使是这样的,我们最终形成的包的大小也达到120M左右.显然,这也不是一个小的文件.那么我们有什么办法可以使得我们的Qt应用的包变得更小呢?
答案是我们使用canonical公司所提供的ubuntu-app-platform snap应用.这个应用把我们最需要的Qt库打入到我们的snap应用中,并通过content sharing的方法把ubuntu-app-platform中的Qt库文件mount到我们的应用中,从而使得我们的Qt应用能够使用ubuntu-app-platform所提供的所有Qt库.这样我们的所有的在一个Ubuntu Core系统的所有的snap应用都可以共享同一份Qt库,从而达到减少我们snap应用大小的目的.
在下面,我们来详细介绍如何利用content interface来实现这个目的.
就像在那篇文章中介绍的那样,我们在我们的Ubuntu 16.04 Desktop中,必须安装stabe-phone-overlay以确保我们的Qt版本是Qt 5.6.1的.否则我们有些Qt应用的打包会有文件.我们可以通过如下的方式来检查我们的Qt版本:
liuxg@liuxg:~$ qmake --version
QMake version 3.0
Using Qt version 5.6.1 in /usr/lib/x86_64-linux-gnu
为什么需要把我们的Qt版本安装为5.6.1版本呢?细心的开发者,可以发现在ubuntu-app-platform的链接中,它表明该snap是基于Qt 5.6.1的版本的.先前在我的电脑上安装的Qt 版本为Qt 5.5.1.当我用该版本打包我的Qt应用时,在运行时会出现一个错误.
我们按照如下的步骤来安装我们的Qt 5.6.1版本:
$ sudo add-apt-repository ppa:ci-train-ppa-service/stable-phone-overlay
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get dist-upgrade
name: rssreader-app
version: "1.0"
summary: A snap app from Ubuntu phone app
description: This is an exmaple showing how to convert a Ubuntu phone app to a desktop snap app
confinement: strict # devmode
apps:
rssreader:
command: desktop-launch $SNAP/lib/x86_64-linux-gnu/bin/rssreader
plugs: [network,network-bind,network-manager,home,unity7,opengl]
parts:
rssreader:
source: src/
plugin: qmake
qt-version: qt5
build-packages:
- cmake
- gettext
- intltool
- ubuntu-touch-sounds
- suru-icon-theme
- qml-module-qttest
- qml-module-qtsysteminfo
- qml-module-qt-labs-settings
- qtdeclarative5-u1db1.0
- qtdeclarative5-qtmultimedia-plugin
- qtdeclarative5-qtpositioning-plugin
- qtdeclarative5-ubuntu-content1
- qt5-default
- qtbase5-dev
- qtdeclarative5-dev
- qtdeclarative5-dev-tools
- qtdeclarative5-folderlistmodel-plugin
- qtdeclarative5-ubuntu-ui-toolkit-plugin
- xvfb
stage-packages:
- libsdl2-2.0-0
- libqt5gui5
- libqt5qml5
- libqt5quick5
- libqt5widgets5
- libqt5network5
- libqt5multimedia5
- libqt5declarative5
- qml-module-qtquick2
- qml-module-qtquick-window2
- qml-module-qtquick-layouts
- qml-module-qtquick-controls
- qml-module-qt-labs-settings
- qml-module-ubuntu-components
- qml-module-qtquick-xmllistmodel
- ubuntu-ui-toolkit-theme
- qml-module-ubuntu-connectivity
- qml-module-ubuntu-layouts
- qml-module-ubuntu-performancemetrics
- fonts-wqy-zenhei
- fcitx-frontend-qt5
snap:
- -usr/share/doc
- -usr/include
after: [desktop/qt5]
name: rssreader-app
version: "1.0"
summary: A snap app from Ubuntu phone app
description: This is an exmaple showing how to convert a Ubuntu phone app to a desktop snap app
confinement: strict # devmode
apps:
rssreader:
command: desktop-launch $SNAP/lib/x86_64-linux-gnu/bin/rssreader
plugs: [network,network-bind,network-manager,home,unity7,opengl,platform]
parts:
rssreader:
source: src/
plugin: qmake
qt-version: qt5
snap:
- -usr/share/doc
- -usr/include
after: [desktop-ubuntu-app-platform]
plat:
plugin: dump
source: .
snap: [ubuntu-app-platform]
plugs:
platform: # plug name, to be used later
interface: content
content: ubuntu-app-platform1 # content being mounted and the version, currently 1
target: ubuntu-app-platform # the mount directory created
default-provider: ubuntu-app-platform # default content source snap, currently the only provider too
liuxg@liuxg:~/snappy/desktop/rssreader_platform$ tree -L 2
.
├── setup
│ └── gui
├── snapcraft.yaml
├── src
│ ├── manifest.json.in
│ ├── po
│ ├── rssreader
│ └── rssreader.pro
└── ubuntu-app-platform
$ snapcraft
liuxg@liuxg:~/snappy/desktop/rssreader_platform/prime/usr/lib/x86_64-linux-gnu$ l
libdouble-conversion.so.1 libpcre16.so.3 libX11-xcb.so.1 libXdamage.so.1
libdrm.so.2 libproxy.so.1 libXau.so.6 libXdmcp.so.6
libglapi.so.0 libQt5Core.so.5 libxcb-dri2.so.0 libXext.so.6
libgraphite2.so.3 libQt5Gui.so.5 libxcb-dri3.so.0 libXfixes.so.3
libharfbuzz.so.0 libQt5Network.so.5 libxcb-glx.so.0 libxshmfence.so.1
libicudata.so.55 libQt5Qml.so.5 libxcb-present.so.0 libXxf86vm.so.1
libicui18n.so.55 libQt5Quick.so.5 libxcb.so.1 mesa/
libicuuc.so.55 libX11.so.6 libxcb-sync.so.1
liuxg@liuxg:~/snappy/desktop/rssreader_platform$ sudo snap install rssreader-app_1.0_amd64.snap --dangerous
rssreader-app 1.0 installed
liuxg@liuxg:~/snappy/desktop/rssreader_platform$ rssreader-app.rssreader
You need to connect the ubuntu-app-platform package with your application
to reuse shared assets, please run:
snap install ubuntu-app-platform
snap connect rssreader-app:platform ubuntu-app-platform:platform
liuxg@liuxg:~$ sudo snap install ubuntu-app-platform
ubuntu-app-platform (stable) 1 from 'canonical' installed
liuxg@liuxg:~$ sudo snap connect rssreader-app:platform ubuntu-app-platform:platform
liuxg@liuxg:~/snappy/desktop/rssreader_platform$ rssreader-app.rssreader
You need to connect the ubuntu-app-platform package with your application
to reuse shared assets, please run:
snap install ubuntu-app-platform
snap connect rssreader-app:platform ubuntu-app-platform:platform
$ sudo /usr/lib/snapd/snap-discard-ns rssreader-app
$ rssreader-app.rssreader