QT and Android Whitepaper
QT and Android Whitepaper
ANDROID
Let’s compare by building an embedded system
with a music player app on a NXP i.MX 8 Quad board.
Executive
summary
<2>
This white paper all started with Witekio software engineers wanting to
tackle a recurring question in the engineering community: should we
use Linux and Qt or Android Native for our next embedded product?
Qt and Android are often complementary (automotive, smartphone) and
Qt is platform agnostic*. Witekio teams complete an equivalent number
of Android and Linux/Qt projects. Everyone has their favorite, but no one
had ever achieved an exhaustive and unbiased comparison.
That is what Julien, Erwan and Stephen decided to do.
It was all about comparing Android and Qt, evaluating each solution’s
advantages and drawbacks from the point of view of embedded
software developers.
To achieve this, Witekio developers team decided to build on a real-life
example: develop a Music player app on an NXP i.MX 8 Quad board.
Their work first started with Julien comparing low-level OS choice
between Android and Linux. Then our 2 application developers, Erwan
and Stephen, faced each other in a breath-taking match to develop the
same application with Android and Qt. They compared both solutions
performance, step by step, and had heated discussions.
Their conclusion:
First, from a low-level perspective, it appears that, on the chosen board,
an NXP i.MX 8 Quad, Android’s strengths do not make it when opposed
to Linux performance (RAM, flash memory) and flexibility which allows
drastic boot time optimization.
At the applicative level, however, it is far less obvious to decide between
Android and Qt. Erwan and Stephen developed the same Music player
app, compared more than 15 criteria, and had passionate debates.
Their conclusion: Qt wins by a short head mostly on the data sorting
and filtering criteria.
To know more about the debate details, you can read the full article.
<3>
The same question keeps coming up for developer
teams at Witekio: “Is it better to develop a user
interface with Qt, or with Android?”
In this white paper, we’ll try to answer that
question.
Rather than churning out ready-made facts and
theories, we’ve decided to give you answers
based on an actual example: developing a music
player application on an NXP board that’s geared
up to deal with connected objects, the i.MX8.
We’ll start by talking about the first step in any
embedded application project: choosing your OS.
Android or Linux?
Once this choice has been made, by Julien, you’re
invited to watch the face-off between Erwan and
Stephen:
<4>
PART 1
Choosing
an embedded
operating system
for our Music
Player App on i.MX.8
Most embedded apps used today function systems like FreeRTOS. That tends to blur the
with a microprocessor and an embedded frontier even more between microcontrollers
Linux or Android operating system (OS). and embedded microprocessors. The
Having an operating system gives them main difference is that microcontrollers are
several advantages. Primarily, it shortens better at real-time tasks that necessarily Julien
the development process. Developers can require little computing power,but need embedded
use lots of different libraries, tools, and to have extremely short response times software
frameworks that they’re already familiar with (close to a few microseconds). On the other expert
to build and debug, like Qt, Electron, GDB, hand, microprocessors running Linux or
SSH, and others. Updates are usually easier Android are more practical for feature-rich
and less risky long-term because the device applications, with complex user experience,
has a file system that helps avoid potential business logic, and connectivity.
write errors, particularly in case of a power
outage. Here, we will focus on two classic OS: Linux
and Android.
However, if your application is running on
a microcontroller, it is worth noting that as So our goal today is to choose between
of January 2020 Qt can work in that type of Linux and Android for a system with a
environment. With the recent release of Qt microprocessor.
for MCUs (https://www.qt.io/qt-for-mcus),
Qt is available on an increasing number Is it better to use Linux or Android in terms of
of platforms, including real-time operating criteria like security, flexibility, and usability?
<5>
Choosing your OS: The Android This is especially visible when it comes to
permissions and access controls: Android
vs. Linux Face-Off
itself mostly manages permissions to use
peripherals, not single files (although it is
What are the arguments for using Android
possible to do so if you have root access,
as the OS for our system?
which is quite dangerous). It works well
out of the box, but don’t expect a lot of
As a general rule, makers of evaluation
customizability.
boards have a version of Android all ready
Also, Android has several embedded
for their evaluation kit (EVK) that you can
components that are necessary for mobile
download and install.
phones but completely useless for most
connected objects. Removing these
However, there are several problems. If you
components without breaking Android’s
want to develop your own hardware, porting
overall operation is extremely complicated.
Android can end up being time-consuming
The overabundance of unused components
and expensive.
also increases the attack surface of your
connected object, for no good reason. One
Android needs a large amount of RAM: 512
final issue is that the boot time for Android
MB minimum. It also needs an important
is generally much longer than for a classic
size for the flash memory, with a minimum
Linux system, and difficult to optimize. So
recommended of around 1 GB.
we can say that Android is practical for
developing and deploying a demo, but not
When it comes to updates, Android supports
necessarily ideal for our finished product.
the Google Play Store on verified devices.
However, getting a device approved for the
On the other hand, Android clearly has all
Play Store is a long and costly process and
the right tools for smartphone development.
you probably won’t want to give the user
If your team is used to developing in Java
access to it, but updating a package is as
or Kotlin, using Android in your product will
easy as downloading an APK (Android
let your high-level development team start
Package File) and installing it.
working on the project immediately, with no
ramp-up time! Also, if your product is similar
For OS updates, all the tools are already
to a smartphone, requiring a desktop,
included in the latest versions of Android
several applications, etc., Android might be
by default, including support for dual bank
a good choice.
updates when properly configured.
<6>
Linux is also a flash memory saver. A basic Let’s now wrap up the
Linux system can easily fit into 10 MB and
comparison between Android
a system including Qt can easily fit into 500
MB even with all the modules. and Linux!
The main way to update Linux is to use Android may be easier to implement at
online repositories, much like the Google first, if:
Play Store but for both OS (kernel) and
software packages. You can make your own • Your hardware platform supports Android
easily. But how to do it will depend on what out of the box.
package manager you use: apt, yum, dnf,
pacman… It can get confusing. You may • All your peripherals are natively supported
want to setup a custom solution, too, for by Android.
example to support dual bank updates or to
handle custom encrypted packages. In this situation, you won’t have to recompile
the entire system or install additional
But it’s easy to add additional libraries, libraries, and everything will work right away.
you can customize the libraries you want But if you want to customize your system
to install, and you can even customize the (and I think that’s the best option) or use
kernel! The permissions system is also more features that aren’t available on Android by
flexible and powerful than Android’s, and the default, it ‘s more efficient to use Linux. In
access controls are much more advanced, fact, I’d go so far as to say it’s better to use
which gives you access to top-level security, Linux if you want a faster, more secure and
using users, user groups, and individual file cheaper product.
permissions.
Linux has several advantages over
In short, Linux is very easy to customize. Android:
Maybe even a little too easy. It can be a
little overwhelming, and you might not know • Linux can support systems with limited
where to start! For example, which Linux resources. Using Android is recommended
distribution do you want to use for your for platforms that have at least 512MB of
system: Alpine, Ubuntu, Debian, Gentoo, or available RAM, but you can easily run a
something customized? Linux and Qt with only 128MB of RAM. Linux
with Qt also has lower persistent storage
As a general rule, you should use Yocto requirements. This allows for a lower bill
if you want to create a fully customized of materials, especially when choosing
distribution. Yocto ensures that your Linux Linux+Qt versus Android.
image build process generates something
valid that can be reproduced, analyzed and • It can greatly reduce the attack surface. If
easily adapted for whatever device you you don’t need Bluetooth, you can remove
want to produce. Yocto ensures that library all the Bluetooth drivers and compatibility
and software dependencies are respected, from the kernel, which is much more difficult
and also lets you deploy an SDK so that on Android.
developers can deploy and test their apps
quickly. • It lets you significantly reduce the boot
time, much more than Android could. Times
as low as 2 seconds have been achieved
with basic customizations.
<7>
Comparison of Boot Statistics A 2-second boot, just by doing simple
operations that also drastically reduced
for Linux and Android
image size and the attack surface.
Their process is explained in greater detail
Boot time optimization with Linux in the article, particularly the tools they used
and their references.
It’s fairly easy to find Linux system boot
statistics. For an NXP i.MX 6 SABRE board, I’m convinced that they could have gotten
this article tells you how to go about it. the same kind of results with an i.MX 8.
https://www.witekio.com/blog/challenge-
Boot Time with Android
called-boot-time/
There are several techniques for optimizing
Android. The most common are the same as
i.MX 6-based boards are fairly common
for Linux, particularly the optimization of the
and used in a wide variety of products.
kernel and the boot loader.
This makes them a good reference when it
comes to boot time.
Unfortunately, during boot, Android has
to preload a huge amount of necessary
In the article, they began with a startup boot
information. One technique to avoid this is
time of over 22 seconds (which is honestly
hibernation, described here
still faster than my smartphone). That’s
much too long for a product that frequently
http://jultika.oulu.fi/files/
turns itself off. At that point, the image was
nbnfioulu-201810062898.pdf
just the “standard” Yocto image, without
optimization. The goal was to reduce this to
The environment used is a little different (and
under 2 seconds.
granted, they were using an older version of
Android), but they still managed to go from
The first step was to select a preferred
a 60-second boot to around seven seconds,
graphical interface framework. The 3
which is remarkable. But even though that’s
considered choices were Qt, Cairo and
a huge improvement, they did reach a limit:
Enlightenment. In the end, Cairo was
the technique required suspension of the
chosen as the device didn’t have any GPU
system on the flash memory, so performance
which would make Qt suboptimal and
depended on the read speed.
Enlightenment took too much time to start.
The second step was to tweak the
Also, for the tests, they went into hibernation
initialization process so that their application
mode immediately after boot, which
is started before the rest of the system. This
doesn’t represent reality and leads to an
effectively reduced the boot time to around
underestimation of the boot time, since RAM
8 seconds.
use is much lower right after boot-up than it
is after using an app.
The third step was to optimize the kernel.
By choosing the right kernel configuration
What’s more, this technique requires many
options, and particularly by removing
more changes that are more technical than
whatever wasn’t useful, they reduced boot
for Linux, and none of them allow a reduction
time to around 6 seconds.
of the attack surface.
The fourth step was to optimize the
More detailed information about boot time
configuration of the bootloader to make
optimization in Android 8 is available here
loading the kernel faster, increasing the SD
card access clock frequency and replacing
https://source.android.com/devices/tech/
the reset chips That got it down to a 1.99
perf/boot-times
second boot time.
<8>
Android? Linux? My OS test Qt applications immediately at the start
recommendation for the Music of your product life cycle. That is perfect for
Player App project producing a POC.
Android is a very good choice for starting the Android drivers are updated less frequently
development of a product on an Evaluation than Linux drivers. Android is more difficult to
Kit (EVK), while waiting for a customized customize and extend, and security updates
board. It gives you a usable environment can take some time before being available
very quickly, and lets you deploy and test (if at all) on your device.
Android apps immediately at the beginning
of the product’s life cycle, for example, to And Linux needs far less RAM and
create a proof-of-concept. flash memory which allows reducing the
production costs.
However, it tends to be pretty weak
compared to Linux in terms of long-term So I recommend that, for our test, we use
security, maintainability, performance, and Linux as the OS.
boot time, particularly on customized boards .
Now that the choice is made, I’ll let Erwan
Qt provides all the necessary tools to ease and Stephen compare Android and Qt at the
development with Linux. It may also be used application development level! I’m going to
on nearly every other major embedded and go buy some popcorn, pull up a comfy chair
desktop operating system (QNX Neutrino, and watch.
Green Hills INTEGRITY, VxWorks, and
more). Android has no real clear advantage
there.
Part 1
Recap
Android Linux
Necessary RAM 512 MB 128 MB
Customization YES NO
<9>
PART 2
Comparing
Development
of our Music Player
on Android and Qt
Our test app, Music Player App, is a • Display the song list and provide the usual
simplified music player that will let us filtering options (by artist, album or genre).
compare development methods and tools
for embedded software in C++/Qt and Java/ • Read each song’s metadata to create a
Android. model within the app (both for displaying Stephen
and filtering). will develop in Qt,
Erwan and Stephen will develop the app
simultaneously, and compare Qt and Android • Play tracks, both via a dedicated screen with
in terms of functions, ease of development all the usual controls (pause/play, progress
and development time. bar, volume control, and next/previous track)
and a sidebar on other menus that includes Erwan
Our app has to perform the following minimal controls (pause/play) and the option will develop in Android
functions, which will allow us to compare to return directly to full-screen.
strengths and weaknesses in the two
frameworks: In this paper, we’re also going to talk about Language versions
the “Integrated Development Environment” Qt Application:
• Display a mobile interface, for interacting (IDE) for each of these frameworks, C++11/Qt 5.13 -
with the app. provided by their editors: QtCreator by The Android Application:
Qt Company and Android Studio by Google. Java 8, Android
• Select a file containing an artist’s API 28
discography, so users can download songs
to play.
<10>
ANDROID AND Qt, With Android, we see the utilization of
a dedicated markup language (XML)
LET’S COMPARE.
to define the screens. Naturally, the
app will be composed of a UI module that
1/ Architectural Breakdown encapsulates these definitions and a back-
end module that encapsulates the data
For development, our media player manipulation. Remember that, unlike Qt, the
application separates its functions into XML references are compiled on the go and
two modules, according to the SOLID best made available on the Java end for better
practices for encapsulation: auto-fill.
• One back end module to handle the song As I understand it, since you don’t have
files, compiled as a module exposing an access to that kind of verification, you won’t
API, accessible by QML. catch any little syntactic errors until you
actually run the app. Isn’t that awkward?
• One interface module, acting as the main
application, which presents the various You’re only partly right, since a QML
controls to the user. compiler actually exists. It will catch
errors related to the QML syntax and
This separation of functions is even semantics, but indeed errors with bindings,
more practical with Qt, because the between QML components or even with the
Qt framework uses two languages, C++ back end, and JS errors aren’t verified
C++ and QML, and each has its own separate upstream.
purpose. The back end module is written in
C++ to facilitate interaction with the OS and As the compiler is opt-in, this method has its
the many modules within the Qt framework advantages, like a shorter compilation time
that supports the handling of specialized during development (since files are simply
files (media audio files in our case). The GUI deployed with the rest of the app, then
module is written in QML, to support the uploaded on demand).
organization of components in a declarative,
natural way (a component contained within Architectural Breakdown: Qt 1 // Android 1
another component is automatically a “child”
and will be automatically positioned relative
to its “parent”)
<11>
2/ Mobile Style Interface 2.2/ Internal Navigation
<12>
With Android, obviously, the same 3/ Discography Selection and
layouts are available to use.
Downloading
However, you can’t include scripted
language in the XML files. Nevertheless,
dimensions (margin, padding, height,
3.1/ Searching for files in the device
width, …) can be defined in dedicated files filesystem
organized by target resolution. It is efficient
but if you have too many specific cases, you An existing, system-agnostic, QML
end up with too much boilerplate code. You, component (FileDialog) allows you
on the other hand, only had to throw in a few to integrate a native file explorer into
ternary operators to manage the different the operating system to select songs. It’s
sizes. I guess in the hands of some novices, very, very easy.
it could come out messy.
With Android, one app can delegate
Actually, since you brought up the resolution, an action (in this case, the folder
in Android we indicate sizes in “density- selection) to any other app that can
independent pixels” and “scale-independent process it. That’s how the “share” function in
pixels.” Does Qt have something to offer for your photo software works, for example. It
that? couldn’t be more decoupled.
<13>
4/ Presentation and Filtering of 4.2/ Graphics
the Discography Representation
When it comes to UI graphics, the
Model QML components make decoupling
and reuse easier, allowing list
4.1/ Modeling and Filtering Songs displays to independently define the
container (including its height, position,
To manipulate data models, Qt offers screen fill, etc.), the model (here, our
a generic class, QAbstractListModel, QSortFilterProxyModel defined previously in
that lets developers connect data the backend module, directly accessible by
getters with attributes (like artist, album, and compatible with the QML component)
length, etc.). Besides the QAbstractListModel, and the delegated component that displays
you can also use a QSortFilterProxyModel. each of the elements in the list.
I assigned them particular attributes (one
attribute for each) and a filter condition. This As we already said, in Android,
kind of model is integrated directly into the screens are defined by their structure
lists’ QML components, allowing you to filter and the static data in the XML files.
by masking which has a minimal cost in They’re just as decoupled and reusable as
relation to performance. your QMLs. The dynamic components you
get with JavaScript are managed by the
I saw that in your code, it’s like a element selector, which makes it possible
wordier version of Linq (in C#). But to apply characteristics (border size, colors,
whatever it is, I haven’t seen anything fonts, background, etc.) to the components
like it in Java or Android. I eventually used according to their state, the goal being to
Streams and old fashioned sorting and minimize programmatic manipulation.
filtering.
Display > Qt 1 // Android 1
It’s an approach that’s actually
a lot like SQL views. It improves
performance in terms of access to
filtered models by avoiding costly operations
on the initial list every time the interface
wants to access it. As a bonus, multiple
QSortFilterProxyModel can be chained for
further filtering.
<14>
5/ Analysis of Media Content 6/ Reading Audio Media
Metadata
Reading audio is done by media
The Qt Multimedia module offers classes from the Qt framework,
classes (QMediaPlayer) dedicated so it’s entirely managed by the
to handling media files (in our case, back end module (which interacts with the
audio files tagged with complementary operating system’s audio drivers via the Qt
information/metadata). These classes framework).
permit asynchronous reading and
processing of metadata, which then serve as This creates two important features:
a discography model (as explained above).
• First of all, reading audio doesn’t depend on
The “android.media” package the state of the interface, which means that
provides all you need to read media the music can be playing in the background
files and explore their metadata. But while the user browses the rest of the app.
actually, I see that your app doesn’t display
album covers, even though that’s metadata. • Second, the functions can be decoupled,
so the back end module can define the API
Chalk one up to you. Since the that the interface accesses to display media
metadata that contains the album information (title, album, total length, etc.)
cover directly contains the raw data and the controls for the music that’s playing
for a PNG image, displaying the images is (play/pause, next/last track, volume control,
a little more complicated. Because QML etc.).
Image components only accept resource
URIs, you have to set up an ImageProvider This way of doing is system agnostic, but it
on the back end to be able to turn QML URIs requires the OS to expose audio and video
into QImages from the metadata. That isn’t drivers that are supported by Qt.
as easy as just providing the bitmap of the
image directly to the component. As I said before, I like to use
the “android.media” package,
Analysis of Media Content Metadata specifically with the MediaPlayer
> Qt 0.5 // Android 1 class that lays out everything needed to play
music and control the position and volume.
I did still have to encapsulate it to manage a
song list. That said, there is a more complete
MediaPlayer in JetPack (androidx.media2),
but it’s very new (the first version came out
in September 2019). So it’s a tie?
<15>
7/ Controls and Accessibility 8/ Internationalization
Although I didn’t use them in this For the i18n, everything’s managed
demonstration, the Qt framework in Android Studio without interrupting
does offer options to facilitate the flow of development. You write
app accessibility. For QML components, the translation key, Alt + Enter, you write
accessibility can be configured as a group of the caption in the main language, and
added properties (https://doc.qt.io/qt-5/qml- you’re back in your code. You end up with a
qtquick-accessible.html), which allows you minimal XML that just has to be duplicated
to describe the role, name, purpose of the for the translators. During replacement, the
control component, and other properties that translation closest to the locale is used,
make it easier to fill in fields and navigate a with fallback to the language family, then
page or form. the default language. Obviously, since
Android Studio integrates the XML files, any
Accessibility is completely managed renamed keys are global.
by Android. You just have to fill in a
few extra fields, for example, image The generation process for
descriptions or a contextual help. Android internationalization files is more
Studio gives you a warning if there’s manual with Qt, I have to concede
anything missing, which is really nice to help this point. Generation is done by an external
ensure accessibility. tool that parses the code files, detecting
where to find translations marker based
Controls and Accessibility > Qt 1 // Android 1 on syntactic analysis (both in QML, with a
JavaScript function, and in C++). Under
these conditions, changing a key requires a
new analysis execution on the entire code
base.
<16>
9/ Ease of Development Yeah, it’s a bit more developer-
friendly on Android Studio, but it’s
not like there is a complete lack of
Qt offers Qt Creator, a simple,
tools to work with Qt and C++.
lightweight IDE designed for
developing in C++ that also
On the generic tools’ side, the C/C++
integrates a local help database for the many
development suite is bulletproof on Linux
components in the Qt framework. The IDE
(GCC, G++, GDB and its server variant,
also facilitates the deployment of the app
Valgrind and its variants, or even the
on the target device during development. It
LLVM tool suite – ClangFormat, ClangTidy,
offers deployment and debugging methods
ClangD, etc. –), and those are partially
through a local network (deployment via
integrated in Qt Creator, along with Qt’s own
SSH or SFTP, and remote execution of the
profiler for QML interfaces.
software on the target device, debugging
On the syntactic and semantic side, Qt
using the GNU C++ GDB).
Creator takes the same route as Android
Studio, slowly but surely, via the development
Android Studio is based on IntelliJ
and integration of LSPs (Language Server
IDEA, the older brother of the
Protocols) that let you skip the additional
Jetbrains IDE suite. In my humble
development of analysis tools.
opinion, it’s the most complete IDE family on
the market. It has a lot of advantages over
Additionally, the Qt application can be built
your text editor:
as an Android .apk using Qt Creator. For
that matter, it can target any embedded
• Transparent on-target deployment of the
environment (e.g. iOS, RTOS).
application
Ease of Development > Qt 0 // Android 1
• On-target debugging that’s perfectly
integrated with adb
• Naming suggestions
<17>
SOME SCREENSHOTS OF OUR
MUSIC PLAYER APP ON A NXP
i.MX 8 QUAD BOARD
Qt version
Home page All songs page Playing song page
Android version
Home page All songs page Playing song page
<18>
KEY TAKEAWAYS FROM OUR
ANDROID AND Qt COMPARISON
After feverish debate on each subject, here
are our final conclusions:
<19>
Part 2
Recap
Qt Android
ARCHITECTURAL BREAKDOWN 1 1
MOBILE STYLE INTERFACE
From design to implementation 1 0
Internal navigation 1 1
Component display 1 0
DISCOGRAPHY SELECTION AND DOWNLOADING
File searching 1 1
Importing files 1 1
PRESENTATION AND FILTERING
Modeling and filtering songs 1 0
Display 1 1
ANALYSIS OF MEDIA CONTENT METADATA 0,5 1
READING AUDIO MEDIA 1 1
CONTROLS AND ACCESSIBILITY 1 1
INTERNATIONALIZATION 1 1
EASE OF DEVELOPMENT 0 1
TOTAL 11,5 10
<20>
18+ YEARS
SOFTWARE SYSTEM
EXPERTISE
5 OFFICES IN EUROPE
AND USA
100+ EMBEDDED
AND IOT SOFTWARE
ENGINEERS
200 INNOVATIVE
DEVICES PROJECTS/YEAR