Reduce QT application size with the platform interface provided by Ubuntu-app-platform

Source: Internet
Author: User
Tags bind root directory

I don't know if you've tried one of my QT apps before. "How to Package a qmake Ubuntu mobile app as a snap app". In that tutorial, we tried to get all the Ubuntu SDK Library pack ubuntu-sdk-libs into our app, and the result was that the files for our snap package were very large. Almost reached 800m-900m. Obviously such an application is not practical. I also tried to get the QT library we need most in our snap package. You can find my updated code. Even so, the size of the package we eventually formed is about 120M. Obviously, this is not a small file either. So what can we do to make our QT app's package smaller?

The answer is that we use the Ubuntu-app-platform snap app provided by Canonical corporation. This application takes our most needed QT library into our snap application and uses the content sharing method to mount the QT library file in Ubuntu-app-platform into our application. This allows our QT application to use all of the QT libraries provided by Ubuntu-app-platform. This allows all of our snap applications in one Ubuntu core system to share the same QT library, thus reducing the size of our snap application.

Below, let's take a detailed description of how to use the content interface to achieve this goal.


1) Installation
First of all, for the English better developers, we can find the following address we need information:
https://developer.ubuntu.com/en/blog/2016/11/16/snapping-qt-apps/

As described in that article, we have to install Stabe-phone-overlay in our Ubuntu 16.04 desktop to make sure that our QT version is QT 5.6.1. Otherwise we have some QT app packaging that will have files. We can check our QT version in the following ways:

liuxg@liuxg:~$ qmake--version
qmake version 3.0
Using Qt version 5.6.1 In/usr/lib/x86_64-linux-gnu

Why do we need to install our QT version to 5.6.1 version? The careful developer can find the link in Ubuntu-app-platform, which indicates that the snap is based on the QT 5.6.1 version. The QT version that was previously installed on my computer is QT 5.5.1. When I package my QT app with this version, an error occurs at run time.

We follow the steps below to install our QT 5.6.1 Version:

$ 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

After executing the above command, we reconfirm that our QT version information is 5.6.1. The installation of this step is very central, the incorrect QT version may make some QT applications run normally, but some may go wrong.


2) using Ubuntu-app-platform to reconstruct QT applications
The source code of our previous RssReader Ubuntu phone app can be found at the address:
Snapcraft.yaml

Name:rssreader-app version: "1.0" summary:a snap app from the Ubuntu phone app Description:this is a exmaple showing how T o 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-gette Xt-intltool-ubuntu-touch-sounds-suru-icon-theme-qml-module-qttest-qml-module-qtsys
      Teminfo-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-qtdeclarati Ve5-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-t 
      Heme-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: [des KTOP/QT5]

Obviously in the above Snapcraft.yaml, we have put too many QT libraries into our snap package. The result is a huge increase in the size of our snap packet. To be able to take advantage of the ubuntu-app-platform provided by Platforminterface, we have modified the Snapcraft.yaml of our application to: Snapcraft.yaml
Name:rssreader-app
Version: "1.0"
summary:a snap app from Ubuntu phone app
Description:this are an exmaple s Howing How to convert a Ubuntu phone app to a desktop snap app
Confinement:strict # devmode

apps:
  rssreader:< C6/>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 provi Der Too

From the new Snapcraft.yaml file above, we can see that we have removed all the relevant Debian packages from the stage-packages and replaced them with our application. PlatformThis plug. In our application, we also define our platform plug interface. Also, it is worth noting that we created an empty directory under the root of our application Ubuntu-app-platformDirectory This directory is used by platform interface as the mount point to share the QT library to our application. If you do not have this empty file directory, you will get an error while running our QT application. The file results for the entire project are as follows:
liuxg@liuxg:~/snappy/desktop/rssreader_platform$ tree-l 2
.
├──setup
│└──gui
├──snapcraft.yaml
├──src
│├──manifest.json.in
│├──po
│├──rssre Ader
│└──rssreader.pro
└──ubuntu-app-platform

After making sure our project is complete, we enter the following command in the root directory of the project:
$ snapcraft

This will produce the. snap file package We want in the current directory. Let's take a look at the size of the snap package we've produced:


Obviously the file size of our snap package is only 18M, much smaller than our previous 120M. The careful developer may have found all the QT library files. It contains the most basic libraries we run for a QT application:
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

We can install it in the following ways:
liuxg@liuxg:~/snappy/desktop/rssreader_platform$ sudo snap install Rssreader-app_1.0_amd64.snap--dangerous
Rssreader-app 1.0 Installed

When we try to run our app for the first time, we can see the following information:
liuxg@liuxg:~/snappy/desktop/rssreader_platform$ Rssreader-app.rssreader you 
need to connect the Ubuntu-app-platform package with your application 
to reuse GKFX assets, please run:
snap Install Ubuntu-app-pla Tform
snap Connect rssreader-app:platform ubuntu-app-platform:platform

Obviously, it is a hint that we need to install the corresponding Ubuntu-app-platform snap application:
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

At this point, we have manually connected our application's platform Plug and Ubuntu-app-platform platform slots. We can use the following command to view:


In our command line, execute the following command:
liuxg@liuxg:~/snappy/desktop/rssreader_platform$ Rssreader-app.rssreader you 
need to connect the Ubuntu-app-platform package with your application 
to reuse GKFX assets, please run:
snap Install Ubuntu-app-pla Tform
snap Connect rssreader-app:platform ubuntu-app-platform:platform

Obviously, our application encountered a problem when it was executed. It does not launch our application, but rather shows the same information as above. This is due to a bug that we designed in the current SNAPD release. We can now eliminate this problem by the following methods.
$ Sudo/usr/lib/snapd/snap-discard-ns Rssreader-app

We can re-install our app again. Before you have connect, Don't run our app。 If you are already running, then execute the above command to remove name space, then reinstall, Connect, and then finally run our app:
$ rssreader-app.rssreader



The source code for the entire application can be found at address: https://github.com/liu-xiao-guo/rssreader_platform More routines: https://code.launchpad.net/~tpeeters/+ Junk/blog-snapping-qt-apps












Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.