PySide for Android guide: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=PySide for Android=


This guide describes:
* how to build Shiboken &amp; PySide for Android using the Necessitas <span class="caps">SDK</span>
* how to use the resulting libraries
* and how to bundle them with your Python program in a standalone <span class="caps">APK</span>
'''<span class="caps">NOTE</span>:''' If you just want to run you Python &amp; PySide programs on Android, you can skip the ''Building PySide'' section and go directly to [[PySide for Android guide#0f9f7f288b4f606f352d2dfede683e15|PySide for Android example application]] ''[qt.io]'' &amp; [[PySide for Android guide#9de8e69b9d5975439d9560475508d28b|Example project for the Necessitas Qt Creator]] ''[qt.io]'' .
See [[PySide for Android guide/#bd908db5ccb07777ced8023dffc802f4|Links]] ''[qt.io]'' for [[PySide for Android guide/#b8e1f77d0be4f2c3175a2050caae5132|source code]] ''[qt.io]'' &amp; [[PySide for Android guide/#9b5d07e7366cd5412ce3607b5d4b2785|pre-built binaries]] ''[qt.io]''.
==Building PySide==
===Prerequisites===
====Necessitas <span class="caps">SDK</span> Make sure to get the latest Necessitas alpha 4 <span class="caps">SDK</span> – older version might not work correctly with the build script (incompatible libraries, different paths, etc.).====
Download &amp; run the Necessitas <span class="caps">SDK</span> installer
'''Once the installer starts -&gt; don’t forget to install Android <span class="caps">API</span> 14 !!'''
* on the ''Select components'' page go to * Miscellaneous -&gt;Android <span class="caps">SDK</span>* and select '''<span class="caps">API</span> 14''' for installation
* to make sure you have the Android <span class="caps">SDK</span> platform/API 14 installed in the Necessitas <span class="caps">SDK</span> (can be done after <span class="caps">SDK</span> installation):<br /> - run <code>SDKMaintenanceTool</code> select ''Package manager'' and in '''Miscantelous-&gt;Android <span class="caps">SDK</span>''' check if you have '''<span class="caps">API</span> 14''' installed<br /> - PySide should probably compile even against a lower <span class="caps">API</span>, but that has not been tested yet
====Other prerequisites====
* system-wide installed Shiboken
* system-wide installed Python 2.7
* Python 2.7 compiled for Android
* cmake
* git
This command should probably fetch most of the prerequisites on Ubuntu:<br />
If you find some are is missing or if you wan’t to provide similar command for other distributions, add them ! <span class="smiley">:)</span>
Then clone the ''Android-pyside-build-scripts'' project and ''cd'' to it’s directory:<br />
Now run the ''prepare.sh'' script:<br /> It clones Android-modified Shiboken &amp; PySide and creates some folders needed for the build.
And that’s it, you are ready to start the build. <span class="smiley">:)</span>
===Build===
To start the build, just run the main build script, called ''build.sh'':<br /> It first builds Shiboken, followed by PySide. The script is fully automatic, but waits for the user to ''press any key'':
* after Shiboken is configured for build
* after PySide is configured for build<br /> Like this you can easily check for any errors during configuration.
Also note, that when running the script, it clears any previous build results before building.
You can also run the ''build_shiboken.sh'' and ''build_pyside.sh'' scripts manually, just always make sure to run the Shiboken one before running the PySide one.
===Build issues===
If you have any issues during the PySide build, the first thing you should do is to check the PySide for Android build scripts repository if it was not updated since you cloned it, as it might already contain a fix for you issue.
====arm-linux-androideabi-g++: Internal error: Killed (program cc1plus)====
This error seems to be caused by low system resources during build and/or running the compilation in multiple threads on some machines. If you are experiencing this error, try to set <code><span class="caps">BUILD</span>_THREAD_COUNT</code> in <code>env.sh</code> to 1.
===Results===
The resulting PySide libraries compiled for Android are located in the ''stage/lib'' folder.
See the ''Example project for the Necessitas Qt Creator'', [[PySide for Android guide#9de8e69b9d5975439d9560475508d28b|Modifying the project]] ''[qt.io]'', section ''Replacing PySide libraries'' for how the PySide libraries can be used as a part of a self-contained Android application.
==PySide for Android example application==
This is an example, that demonstrates a fully functional standalone Android application that uses Python, PySide and Qt Components.
[[Image:example_landscape.png|example application in ladscape]] [[Image:example_portrait_kbtest.png|example application in portrait]] [[Image:example_portrait_tools.png|example application showing the tools menu]]
[http://modrana.org/trac/wiki/ScreenshotsEN#PySideQtComponentsexample more screenshots] ''[modrana.org]''
===Ready-to-install <span class="caps">APK</span>===
A ready-to-install standalone <span class="caps">APK</span> is available here:
http://modrana.org/platforms/android/pyside_example/PySideExample_1.2.apk
Just install it and press the ''PySideExample'' icon.
====First start====
If you haven’t yet installed any Ministro using Qt application on your Android device, you will be redirected to the Play store to install the Ministro application. Ministro is a manager &amp; updater for the Qt libraries for Android. The example application requires to be installed to run.<br /> So just install Ministro and everything else will be handled automatically. You might need to press the ''PySideExample'' icon again once Ministro is installed.
Once Ministro is installed and does it’s work, the example might still take some time to start, as it is unpacking Python, Qt Components, theme for Qt Components and the example program to it’s working directory. Once the unpacking is finished, the example application will be started.
The unpacking is done only once on the first application start, following starts are very fast, at least when tested on my device (HP Touchpad with CM9).
====What the example application demonstrates====
* sending data to Python and back * the content of the entry field is sent to Python, where it is painted on the PySide image and returned to <span class="caps">QML</span> using ImageProvider * the date is retrieved in Python and shown in <span class="caps">QML</span>
* working text entry with the Android virtual keyboard
* working screen rotation
* correct Portrait/Landscape orientation switching * in both normal &amp; inverted orientations
* working notifications (InfoBanner)
* working ToolBar
* working Menu
* “tools” menu with rotation &amp; opacity sliders for the PySide image
====Source code====
Available from Github under <span class="caps">BSD</span> licence:<br />https://github.com/M4rtinK/expyside/tree/android
====Size of the <span class="caps">APK</span>====
The example <span class="caps">APK</span> has about 16 MB. This is because it needs to bundle quite a lot of<br /> libraries and related files. Big part of it is actually not used by the example in any way.
For normal applications it should be possible to make the resulting <span class="caps">APK</span> much smaller by:
* including only the PySide libraries that are actually used
* removing unused Python modules
* cutting down the Qt Components theme from all graphics &amp; icons that are not used
==Example project for the Necessitas Qt Creator==
This project has been used to generate the above mentioned application example <span class="caps">APK</span>.
===Using the project===
Using the project to build your own project is very easy. Just install the [http://necessitas.kde.org/necessitas/necessitas_sdk_installer.php Necessitas <span class="caps">SDK</span>] and clone the example project from git:<br /> Then just open the ''PySideExample.pro'' with the Necessitas Qt Creator.
To generate a new <span class="caps">APK</span>, just click the green '''run''' arrow in the lower left – Qt Creator should rebuild the the <span class="caps">APK</span> and deploy it either to the simulator or to any connected Android device that has debugging enabled.
====Build &amp; deployment issues====
====='''The example fails to start when deployed to device'''=====
First check you have the latest clone of the project.
Then check for which <span class="caps">ARM</span> architecture are you building by clicking on '''Projects'''. The available architecture targets are:
* Necessitas Qt 4.8.2 for Android armv5
* Necessitas Qt 4.8.2 for Android armv7a
The currently used architecture dark, the unused is white-grey. The build architecture needs to correspond to what the <span class="caps">CPU</span> of your device is using. If not sure, just try both and stick with the one that works. <span class="smiley">:)</span>
====='''Cannot find <span class="caps">ELF</span> information'''=====
This error is harmless and can be safely ignored.
===How does it work ?===
The project contains various components, that together enable the creation of fully standalone Python &amp; PySide application <span class="caps">APK</span>, while also allowing to bundle any arbitrary libraries and files needed by the application.
The example project is called <code>PySideExample</code> and uses the <code>org.modrana.PySideExample</code> unique name. This means, that when it’s <span class="caps">APK</span> is installed on and Android device, it gets installed to this directory:<br /> This path is important, as the application needs to set a couple of environmental variables during startup, pointing to libraries and themes that reside in this directory.
====C++ wrapper====
The <code>main.cpp</code> and <code>main.h</code> files are used to build a C++ Python wrapper. This wrapper is build against the the Android-compiled python libraries in <code>build_dependencies/python</code> by Necessitas.
Once the <span class="caps">APK</span> is deployd to the device and started, this wrapper is run. It initializes it’s build-in Python interpreter, which tries to start the <code>/data/data/org.modrana.PySide.Example/files/main.py</code> Python file. This Python code then imports PySide, instantiates a QApplication and starts the main loop.
Behind the scenes, Necessitas handles wrapping the QApplication to an Android activity and showing it on the screen. It also handles other stuff like keyboard input &amp; Qt Mobility.
=====main.h=====
This file contains important paths for the C++ wrapper.
* <code><span class="caps">MAIN</span>_PYTHON_FILE</code> – path to the main Python file to run once the application is started
* <code><span class="caps">PYTHON</span>_HOME</code>
* <code>LD_LIBRARY_PATH</code> – '':'' separated list of paths used to look for libraries when loading them
* <code><span class="caps">PATH</span></code> – search path for executables
* <code><span class="caps">THEME</span>_PATH</code> – path to the main themes folder for Qt Components
* <code><span class="caps">QML</span>_IMPORT_PATH</code> – path to the Qt Components <code>imports</code> folder
=====main.cpp=====
This is the C++ wrapper, it contains the embedded Python interpreter that is used to start the application and also sets some important environmental variables specified through <code>main.h</code> .
====The example Python application====
The example in <code>main.py</code> inside <code>my_python_project.zip</code> is basically a normal PySide application, it imports PySide, instantiates QApplication and starts the main loop. There is only one main difference in this piece of code:
Due to some not yet identified bug, unless <code>libshiboken.so</code> &amp; <code>libpyside.so</code> are manually loaded to memory like this, importing any PySide module fails.
'''So make sure this piece of code is somewhere in your application and is executed before import of any PySide module takes place.'''
Regarding the <code><span class="caps">PYSIDE</span>_APPLICATION_FOLDER</code> environmental variable, it is set by the <code>main.cpp</code> wrapper and can be configured in <code>main.h</code> .
====Bundling====
The example project bundles all files needed by the application inside the standalone <span class="caps">APK</span>. This section describes how the bundling works.
=====Modified QtActivity.java=====
Another mportant file is the main Android ''activity'' located in <code>android/src/org/kde/necessitas/origo/QtActivity.java</code> This is actually the first thing that is started once you press the application icon on android. It basically works as a glue between Android, Qt and in our case, Python &amp; PySide.
The basic activity file that is part of every Necessitas Qt Creator project has been modified by the ''android_python27'' project, to look for two specific zip archives in the installation folder at startup ant to decompress them &amp; then remover the archives.
This way bundling of arbitrary files (libraries, executables, code, themes, etc.) inside the application <span class="caps">APK</span> has been achieved.
'''<span class="caps">NOTE</span>:''' Necessitas Qt Creator might sometimes want to update the pre-generated *.java files with a new version. If this happens, check if the update didn’t break or remove the bundling code in <code>QtActivity.java</code> .
=====GlobalConstants.java=====
The file <code>android/src/org/kde/necessitas/origo/GlobalConstants.java</code> contains variables for the main <code>QtActivity</code> file.
It looks like this:
As you can see, you can use it to set various variables, such as name of the bundling archives or logging prefix (does this actually work ?).
=====my_python_project.zip=====
The first archive, located in <code>android/res/raw/my_python_project.zip</code> in the project contains the Python application code. On first application start, it’s contents are unpacked into:
<code>/data/data/org.modrana.PySide.Example/files/</code>
=====python27.zip=====
This file is located <code>android/res/raw/python_27.zip</code> in the project and it’s content is unpacked to:
<code>/data/data/org.modrana.PySide.Example/files/python</code>
The paths set in <code>main.h</code> expect this and point the corresponding environmental variables to the <code>bin</code>, <code>lib</code>, <code>imports</code> and <code>themes</code> folders in this directory.
===Modifying the project===
When you want to use the example project as basis for your Python application for Android, you just need to rename it and replace the example application.
But just in case I’ve also documented replacing all the other components.
====Renaming====
=====Names=====
There are actually two names – the project/application name and the unique application name.
For the example project, they project name is <code>PySideExample</code> and the unique name is <code>org.modrana.PySideExample</code>. As you can see, the project name is also a suffix for the unique name. (You probably can use a project name that differs from the suffix, but I rather make them the same to avoid needless confusion).
The unique name is very important:
* it has to be unique so it dosn’t clash with other applications * for this reason, it is mostly based on a domain name you control or some other string with low possibility of being used by another developer
* the unique name is used for path to the installation folder * the example project uses the <code>org.modrana.PySideExample</code> and it is installed into <code>/data/data/org.modrana.PySideExample</code> as a result * the path to the installation folder is used when setting important environmental variables, so make sure to change all the corresponding paths when changing the unique name
=====Project rename script=====
You can also run this combined script from the project directory, which should do all the needed renaming:<br /> Just change <code><span class="caps">NEW</span>_NAME</code> and <code><span class="caps">NEW</span>_UNIQUE_NAME</code> to values matching you application and you are set. <span class="smiley">:)</span>
=====What the rename script does=====
Lets say we want to rename the example project from <code>PySideExample</code> to <code>BarApp</code> and from <code>org.modrana.PySideExample</code> to <code>foo.foomatic.BarApp</code>
* rename the project file:<br />
* replace the name inside the project file:<br />
* replace all unique names in <code>main.h</code>:<br />
* replace all unique names in the <code>QtActivity</code>:<br />
* replace all names in the Android manifest file:<br />
* and the last is in the <code>android/res/strings.xml</code> and <code>android/build.xml</code> file:<br />
'''<span class="caps">NOTE</span>:''' Some of these names can be changed from the Necessitas Qt Creator <span class="caps">GUI</span> or of course also by using an editor.
To verify that you have really changed all of the original names or if there are still some left, you can use this command:
====Replacing the application====
The application is located in: <code>android/res/raw/my_python_project.zip</code>
This file is decompressed into the <code>/data/data/org.modrana.PySide.Example/files/</code> folder on first start after installation. Then <code>/data/data/org.modrana.PySide.Example/files/main.py</code> is run by Python.
To replace the example application, just replace the contents of <code>my_python_project.zip</code>, if you want to start other file than <code>main.py</code>, just change the ''<span class="caps">MAIN</span>_PYTHON_FILE'' path in <code>main.h</code> .
====Replacing Python====
The project contains two Python “bundles”, one is used to compile the application wrapper and is located in <code>build_dependencies/python</code>, the other one is in <code>android/res/raw/python_27.zip</code> and is deployed on first start after installation into <code>/data/data/org.modrana.PySide.Example/files/python</code> with all other bundled libraries and files in this archive.
When replacing Python, you should probably replace both bundles with the same Android-compiled Python version, or at least use the same series (2.7 &amp; 2.7 not 2.7 &amp; 2.6).
====Replacing PySide libraries====
The PySide libraries are located in <code>android/res/raw/python_27.zip</code> archive inside the ''lib'' folder. This folder is deployed to <code>/data/data/org.modrana.PySide.Example/files/python/lib</code> on the Android device.
When replacing PySide, you need to replace the libshiboken and libpyside:<br />
====Replacing Qt Components====
The Qt Components are packed in the <code>android/res/raw/python_27.zip</code> in the <code>imports</code> directory, the theme is in <code>themes</code>. These to folders are deployed like this after installation:<br />
So to replace Qt Components and/or their theme, just replace the content of the ''imports'' and/or ''themes'' folders in the <code>python_27.zip</code> archives.
====Adding files, libraries &amp; executables====
=====Files=====
Arbitrary files needed by you application should probably go to the <code>my_python_project.zip</code>, to be deployed together with your application to the main instalation folder.
=====Libraries &amp; executables=====
Libraries should be added to <code>python_27.zip</code> to the <code>lib</code> folder, exectuables to the <code>bin</code> folder. Like this they will be deployed to a folder that is listed in <code>$LD_LIBRARY_PATH</code> and <code>$PATH</code> respectively.
'''<span class="caps">NOTE</span>:''' I haven’t yet tested if running executables through ''subprocess'' actually works.
==Ideas for improvement==
There is definitely still room for improvement, such as:
* customizes Necessitas Qt Creator PySide projects
* building <span class="caps">APK</span>s from command line only without Qt Creator
* show a progress bar when the bundled libs are unpacked on first start
* modified bundling that doesn’t unpack the files during startup bud during installation * this could speed up the first start quite a bit
* compiling the many Qt Components files &amp; images to a single resource file ?
* documenting how to use Android specific <span class="caps">API</span>s from Python
* videos
==Links==
===Source code listing===
A convenient listing of sources for all the components used for the PySide &amp; co port to Android. <span class="smiley">:)</span>
Shiboken for Android<br />https://github.com/M4rtinK/shiboken-android/tree/android
PySide for Android<br />https://github.com/M4rtinK/pyside-android/tree/android
PySide for Android build scripts<br />https://github.com/M4rtinK/android-pyside-build-scripts
Qt Components<br />https://qt.gitorious.org/~martink/qt-components/martinks-ineans-qt-components/commits/android
Example program<br />https://github.com/M4rtinK/expyside/tree/android
Example project for Necessitas QtCreator<br />https://github.com/M4rtinK/android-pyside-example-project
===Binary listing===
List of relevant pre-built binaries
PySide libraries<br />http://modrana.org/platforms/android/pyside/
PyQt libraries<br />http://modrana.org/platforms/android/pyqt4/
Python 2.7 compiled for android – libs, executables &amp; headers<br />http://www.modrana.org/platforms/android/python2.7/python2.7_for_android_v1.zip
Qt Components for Android<br />http://modrana.org/platforms/android/qt_components/qt_components_v1.zip
Cut-down Qt Components theme<br />http://modrana.org/platforms/android/qt_components/qt_components_theme_mini_v1.zip
Example application <span class="caps">APK</span><br />http://modrana.org/platforms/android/pyside_example/PySideExample_1.2.apk
==Acknowledgement==
As usual with open source development, I haven’t done all of this single handedly, but built on work done by others before me. So I’d like to both acknowledge on which work this is build upon and also provided links to the sources I’ve used:
* <span class="caps">THP</span>s [http://thp.io/2011/pyside-android/ PySide for Android] ''[thp.io]'' – showing that this is possible
* Adrià Cereto-Massagué – integrated &amp; improved <span class="caps">THP</span>s patches for [https://github.com/ssorgatem/PySide/tree/android Shiboken] ''[github.com]'' and [https://github.com/ssorgatem/PySide/tree/android PySide] ''[github.com]''
* [http://code.google.com/p/android-python27/ The Android-Python2.7 project] ''[code.google.com]'' – solved the <span class="caps">APK</span> bundling issue, provides Android-buildable Python 2.7
* [http://hg.microcode.ca/blackberry-py/wiki/Building the BlackBerry-Py Building PySide guide] ''[hg.microcode.ca]'' PySide – I’ve used this as a base when making the Android build scripts
* [http://necessitas.kde.org/ the Necessitas project] ''[necessitas.kde.org]'' – made Qt on Android possible * also provides the Necessitas Qt Creator used for by the example project for building standalone <span class="caps">APK</span>s
* [http://qt.io/ Qt-Project] ''[qt.io]'' – provides the <span class="caps">GUI</span> toolkit <span class="smiley">:)</span>
* [[PySide]] ''[qt.io]'' – provides the Python-Qt bindings
* [https://gitorious.org/~inean/qt-components/ineans-qt-components Ineans Qt Components] ''[gitorious.org]'' – with small modifications used in the example application &amp; project
Thanks a lot – without you, this would not be possible! <span class="smiley">:)</span>
===Categories:===
* [[:Category:LanguageBindings|LanguageBindings]]
** [[:Category:LanguageBindings::PySide|PySide]]

Revision as of 16:23, 14 January 2015