Programacion Android Desde Python
Programacion Android Desde Python
Release 0.2.9
Russell Keith-Magee
1 Table of contents 3
1.1 Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 How-to guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Community 5
2.1 Tutorials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 How-to guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3 About Briefcase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
i
ii
Briefcase Documentation, Release 0.2.9
Briefcase is a tool for converting a Python project into a standalone native application. It supports producing binaries
for:
• macOS,
• Windows,
• Linux,
• iOS,
• Android,
• Django, and
• tvOS.
Contents 1
Briefcase Documentation, Release 0.2.9
2 Contents
CHAPTER 1
Table of contents
1.1 Tutorial
Guides and recipes for common problems and tasks, including how to contribute
1.3 Background
1.4 Reference
3
Briefcase Documentation, Release 0.2.9
Community
Briefcase is part of the BeeWare suite. You can talk to the community through:
• @pybeeware on Twitter
• pybee/general on Gitter
2.1 Tutorials
In this tutorial, you’ll take a really simple “Hello, world!” program written in Python, convert it into a working project.
Setup
This tutorial assumes you’ve read and followed the instructions in Getting Started. If you’ve done this, you should
have:
• A tutorial directory,
• An activated Python 3.5+ virtual environment,
• Briefcase installed in that virtual environment,
• Any platform-specific dependencies installed.
5
Briefcase Documentation, Release 0.2.9
This will ask a bunch of questions of you. We’ll use an app_name of “helloworld”, a formal_name of “Hello World”,
using the Toga GUI toolkit. You can use the default values for the other questions (or update them to reflect your own
name if you want).
You’ll now have a few files in this folder, including a helloworld directory.
Check out what the provided helloworld/app.py file contains:
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
class HelloWorld(toga.App):
def startup(self):
# Create a main window with a name matching the app
self.main_window = toga.MainWindow(title=self.name)
def main():
return HelloWorld('Hello World', 'com.example.helloworld')
Put it in a briefcase
Windows
You must use Windows command line (cmd) to run this step. In case you have installed MingW32, Linux compatibility
layer for Windows or similar, make sure it doesn’t interfere with cmd environment.
To create and run the application, run:
6 Chapter 2. Community
Briefcase Documentation, Release 0.2.9
This will produce a windows subdirectory that will contain a HelloWorld-0.0.1.msi installer. If you get an
error stating that Wix Tools cannot be found, and you have already installed them, try restarting your computer.
macOS
This will produce a macOS subdirectory that contains a Hello World.app application bundle. This bundle can be
dragged into your Applications folder, or zipped and distributed to anyone else.
Linux
This will produce a linux subdirectory that contains a Hello World script that will start the application.
iOS
This will start the iOS simulator (you may be asked to select an API and a simulator device on which to run the app)
and run your app.
It will also produce an ios subdirectory that contains an XCode project called Hello World.xcodeproj. You
can open this project in XCode to run your application.
Android
This will produce an android subdirectory that contains a Gradle project. It will also launch the app on the first
Android device or simulator that can be found running on (or attached to) your computer.
When the application runs, you should see a window with a title of “Hello World” appear. The window won’t contain
any content - but it will be a native application, with a native icon in your task bar (or wherever icons appear on your
platform).
You’ve just packaged your first app with Briefcase! Now, let’s make the app actually do something interesting.
2.1. Tutorials 7
Briefcase Documentation, Release 0.2.9
In this step we assume that you followed the previous tutorial. Put the following code into helloworld\app.py,
replacing the old code:
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW, LEFT, RIGHT
class Converter(toga.App):
def calculate(self, widget):
try:
self.c_input.value = (float(self.f_input.value) - 32.0) * 5.0 / 9.0
except Exception:
self.c_input.value = '???'
def startup(self):
# Create a main window with a name matching the app
self.main_window = toga.MainWindow(title=self.name)
self.c_input = toga.TextInput(readonly=True)
self.f_input = toga.TextInput()
f_box.add(self.f_input)
f_box.add(self.f_label)
c_box.add(self.join_label)
c_box.add(self.c_input)
c_box.add(self.c_label)
box.add(f_box)
box.add(c_box)
box.add(button)
box.style.update(direction=COLUMN, padding_top=10)
f_box.style.update(direction=ROW, padding=5)
c_box.style.update(direction=ROW, padding=5)
self.c_input.style.update(flex=1)
self.f_input.style.update(flex=1, padding_left=160)
self.c_label.style.update(width=100, padding_left=10)
(continues on next page)
8 Chapter 2. Community
Briefcase Documentation, Release 0.2.9
button.style.update(padding=15, flex=1)
def main():
return Converter('Converter', 'org.pybee.converter')
2.1. Tutorials 9
Briefcase Documentation, Release 0.2.9
Now, we’re going to deploy the same code, but as a single page web application. Make sure you have the Django
dependencies installed (see Getting Started), and run the following:
$ python setup.py django -s
This will gather all the Javascript dependencies, create an initial database, start a Django runserver, and launch a
browser. You should see the same application running in your browser:
Note: If you get a “Server could not be contacted” error, it’s possible your web browser started faster than the server;
reload the page, and you should see the app.
How-to guides are recipes that take the user through steps in key subjects. They are more advanced than tutorials and
assume a lot more about what the user already knows than tutorials do, and unlike documents in the tutorial they can
stand alone.
If you experience problems with Briefcase, log them on GitHub. If you want to contribute code, please fork the code
and submit a pull request.
10 Chapter 2. Community
Briefcase Documentation, Release 0.2.9
The recommended way of setting up your development envrionment for Briefcase is to install a virtual environment,
install the required dependencies and start coding. Assuming that you are using virtualenvwrapper, you only
have to run:
Briefcase uses unittest (or unittest2 for Python < 2.7) for its own test suite as well as additional helper
modules for testing. To install all the requirements for Briefcase, you have to run the following commands within your
virutal envrionment:
$ pip install -e .
If you have a beeware iOS project that has a crash, it can be difficult to see the stacktrace. Here’s how to do it -
1. Build your iOS project. You don’t have to start it.
2. Open that iOS project in Xcode. Click the Run button (looks like an arrow) and wait for the simulator to open.
Cause the app to crash.
3. Your stack trace ought to appear in the ‘debugger area’ at the bottom of the screen. If you can’t see that area,
you may have to activate it with View > Debug Area > Show Debug Area
Broadly; Python 3. However, the exact versions supported vary depending on the platform being targeted.
The icon attribute specifies the prefix of a path to a set of image files. The name specified will be appended with a
number of suffixes to construct filenames for the various icon sizes needed on each platform. You should provide the
following files:
• On iOS:
– $(icon)-180.png, a 60x60@3x image (iPhone)
– $(icon)-167.png, an 83.5x83.5@2x image (iPad Pro)
– $(icon)-152.png, a 76x76@2x image (iPad)
– $(icon)-120.png, a 40x40@3x/60x60@2x image (iPad, iPhone)
– $(icon)-87.png, a 29x29@3x image (iPad, iPhone)
– $(icon)-80.png, a 40x40@2x image (iPad, iPhone)
– $(icon)-76.png, a 76x76 image (iPad)
– $(icon)-58.png, a 29x29@2x image (iPad)
– $(icon)-40.png, a 40x40 image (iPad)
– $(icon)-29.png, a 29x29 image (iPad)
• On Android:
– $(icon)-192.png, an xxxhdpi image (192x192)
– $(icon)-144.png, an xxhdpi image (144x144)
– $(icon)-96.png, an xhdpi image (96x96)
– $(icon)-72.png, a hdpi image (72x72)
– $(icon)-48.png, an mdpi image (48x48)
– $(icon)-36.png, an ldpi image (36x36)
• On macOS:
– $(icon).icns, a composite ICNS file containing all the required icons.
• On Windows:
– $(icon).ico, a 256x256 ico file.
• On Apple TV:
– $(icon)-400-front.png, a 400x240 image to serve as the front layer of an app icon.
– $(icon)-400-middle.png, a 400x240 image to serve as the middle layer of an app icon.
– $(icon)-400-back.png, a 400x240 image to serve as the back layer of an app icon.
12 Chapter 2. Community
Briefcase Documentation, Release 0.2.9
Briefcase is part of the BeeWare suite. You can talk to the community through:
• @pybeeware on Twitter
• The BeeWare Users Mailing list, for questions about how to use the
• BeeWare suite.
• The BeeWare Developers Mailing list, for discussing the development of
• new features in the BeeWare suite, and ideas for new tools for the suite.
Code of Conduct
The BeeWare community has a strict Code of Conduct. All users and developers are expected to adhere to this code.
If you have any concerns about this code of conduct, or you wish to report a violation of this code, please contact the
project founder Russell Keith- Magee.
Contributing
If you experience problems with Briefcase, log them on GitHub. If you want to contribute code, please fork the code
and submit a pull request.
In this guide we will walk you through setting up your Briefcase environment for development and testing. We will
assume that you have a working Python install, and an existing project. If you don’t have an environment set up yet,
this guide will help you get started.
Note: Briefcase (and the whole BeeWare toolchain) requires Python 3. Support for different Python 3 minor versions
varies depending on the platform you’re targeting; Python 3.5+ will give you the best results.
Install Briefcase
The first step is to install Briefcase. If you’re using a virtual environment for your project, don’t forget to activate it.
$ mkdir tutorial
$ cd tutorial
$ python3 -m venv venv
$ . venv/bin/activate
$ pip install briefcase
Note: On some versions the activate script may be in the venv/Scripts/ folder in which case swap: $ . venv/bin/
activate for $ . venv/Scripts/activate
Next, you’ll need to make sure you’ve got the platform-specific dependencies for the platforms you’re going to target.
14 Chapter 2. Community
Briefcase Documentation, Release 0.2.9
Windows
Mac OSX
Linux
You’ll need install GTK+ 3.10 or later. This is the version that ships starting with Ubuntu 14.04 and Fedora 20. You
also need to install the Python 3 bindings to GTK+. If you want to use the WebView widget, you’ll also need to have
WebKit, plus the GI bindings to WebKit installed. This means you’ll need to install the following:
• Ubuntu 14.04 apt-get install python3-gi gir1.2-webkit2-3.0
• Ubuntu 16.04 apt-get install python3-gi gir1.2-webkit2-4.0 or apt-get install
python3-gi gir1.2-webkit2-3.0
• Fedora 20+ dnf install python3-gobject pywebkitgtk or yum install
python3-gobject pywebkitgtk
• Debian Stretch apt-get install python3-gi gir1.2-webkit2-4.0
iOS
• Install XCode from the App store. Once you’ve installed XCode, you must also install the Xcode Command
Line Tools. This can be done from the Preference panel within XCode itself.
Android
• Install Android Studio. When you start Android Studio for the first time, you’ll be provided a wizard to configure
your installation; select a “standard” installation.
• Put the sdk/tools, sdk/platform-tools and sdk/tools/bin directories in your path. - On macOS: ~/Li-
brary/Android/sdk/tools, ~/Library/Android/sdk/platform-tools and ~/Library/Android/sdk/tools/bin
• Set the ANDROID_SDK_HOME directory - On macOS: ~/Library/Android/sdk
• Update the SDKs:
$ sdkmanager --update
$ emulator @Nexus_5X_API_22
Django
If you are going to create a web app with Django, you need:
• Install an LTS version of Node (6.9.x)
• Install NPM 4.x or higher
Next Steps
You now have a working Briefcase environment, so you can start the first tutorial.
2.3.5 Quickstart
Then, add extra options to your setup.py file to provide the app-specific properties of your app. Settings that are
applicable to any app can be set under the app key; platform specific settings can be specified using a platform key:
setup(
...
options={
'app': {
'formal_name': 'My First App',
'bundle': 'org.example',
},
'macos': {
'app_requires': [
'toga-cocoa'
],
'icon': 'icons/macos',
},
'ios': {
'app_requires': [
'toga-ios'
],
'icon': 'images/ios_icon',
'splash': 'images/ios_splash',
},
'android': {
'app_requires': [
'toga-android'
],
'icon': 'images/android_icon',
'splash': 'images/android_splash',
},
'tvos': {
'app_requires': [
'toga-ios'
]
},
'django': {
'app_requires': [
'toga-django'
(continues on next page)
16 Chapter 2. Community
Briefcase Documentation, Release 0.2.9
At a minimum, you must set a formal_name key (the full, formal name for the app) and a bundle key (the bundle
identifier for the author organization - usually a reverse domain name).
Alternatively, if you’re starting from scratch, you can use cookiecutter to generate a stub project with the required
content:
setup(
...
entry_points={
'gui_scripts': [
'Example = example.gui:main [GUI]',
],
'console_scripts': [
'utility = example.main:main',
]
}
...
0.2.9
0.2.8
• Corrects packaging problem with urllib3, caused by inconsistency between requests and boto3.
• Corrected problems with Start menu targets being created on Windows.
0.2.7
• Added support for launch images for iPhone X, Xs, Xr, Xs Max and Xr Max
• Completed removal of internal pip API dependencies.
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
18 Chapter 2. Community
Briefcase Documentation, Release 0.2.9
0.2.1
0.2.0
0.1.9
0.1.8
0.1.7
• Added check for existing directories, with the option to replace existing content.
• Added a Linux backend.
• Added a Windows backend.
• Added a splash screen for Android
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
• Added support for having multi-target support projects. This clears the way for Briefcase to be used for watchOS
and tvOS projects, and potentially for Python-OSX-support and Python-iOS-support to be merged into a single
Python-Apple-support.
0.1.1
0.1.0
Briefcase is a new project - we have lots of things that we’d like to do. If you’d like to contribute, providing a patch
for one of these features:
• Windows support
• Linux support (for various distros)
• Apple watchOS support
2.4 Reference
20 Chapter 2. Community