Okay, this is just too cool not to mention right away!
Jeff Kehres from RIM has been following the progress on and off, and pointed out today the existence of the blackberry-pythonpackager that's already included in the NDK tools.
I therefore herewith present the classic tiny "hello, world" app for the PlayBook, written in Python and with output going only to the appdata/logs/log file (to which stdout is redirected by default).
print "hello, world"
Okay, and let's make a nice icon for it (and by "nice", I mean only "really small file size"):
Now we need a bar-descriptor.xml file. I'll include one stripped of comments and whatever else I can find that's optional. Note the line with entry="true" type="Qnx/Python" as that right there is the magic key to the kingdom. Also you do need the run_native action (including the system="true" part if you'll ever sign the app for distribution):
<?xml version="1.0" encoding="utf-8" standalone="no"?> <qnx xmlns="http://www.qnx.com/schemas/application/1.0"> <id>ca.microcode.bbxpython.m2</id> <name>BBXPyTiny</name> <versionNumber>0.0.1</versionNumber> <description>BBX-Python Tiny App</description> <author>Your Name Here</author> <authorId>gYAUseYour_OwnIdvPY</authorId> <initialWindow> <systemChrome>none</systemChrome> <transparent>false</transparent> </initialWindow> <icon><image>icon.png</image></icon> <asset path="icon.png">icon.png</asset> <asset path="main.py" entry="true" type="Qnx/Python">main.py</asset> <action system="true">run_native</action> </qnx>
We need to build and deploy it:
c:\dev\bbxpy\tiny> bbndk-env.bat ... c:\dev\bbxpy\tiny> blackberry-pythonpackager -devMode test.bar bar-descriptor.xml Info: Package created: test.bar c:\dev\bbxpy\tiny> blackberry-deploy -installApp -package test.bar -device PBIP -password PBPASS Info: Sending request: Install Info: Action: Install Info: File size: 2414 Info: Installing ... Info: Processing 2414 bytes actual_dname::ca.microcode.bbxpython.m2.testDev_bxpython_m2f3b6aa2c actual_id::testDev_bxpython_m2f3b6aa2c actual_version::0.0.1.0 result::success
Note the file size... it's always nice when you don't need a lot of setup and boilerplate code in the way. Of course, it doesn't actually do much, but we can bloat it up a bit later.
That worked because I had a debug token (from which I got my authorId info for the bar-descriptor.xml file above) already installed on my tablet. The alternative would be not to use -devMode during packaging, but use your code signing key instead, with the -sign option and info about your keystore and password.
Here it is installed and ready to run, as the last icon in the list:
Run it, and you briefly get an all-white screen and then it exits.
Let's log in via SSH using PuTTY and see the aftermath:
Using username "devuser". Authenticating with public key ... $ cd /accounts/1000/appdata/ca.microcode.bbxpython.m2.testDev_bxpython_m2f3b6aa2c $ cd logs $ cat devmode_exitcode.txt 0 $ cat log hello, world
And what's the installed app code folder look like?
$ ls -l /apps/ca.microcode.bbxpython.m2.testDev_bxpython_m2f3b6aa2c/ total 16 drwxrwx--- 2 apps dev1 4096 Nov 29 12:28 META-INF drwxrwx--- 2 apps dev1 4096 Nov 29 12:28 python $ ls -l /apps/ca.microcode.bbxpython.m2.testDev_bxpython_m2f3b6aa2c/python total 5 -rw-rw---- 1 apps dev1 710 Nov 29 12:28 bar-descriptor.xml -rw-rw---- 1 apps dev1 796 Nov 29 12:28 icon.png -rw-rw---- 1 apps dev1 22 Nov 29 12:28 main.py
So that pretty much eliminates the need for the native stub launcher code that started this whole thing off for me. Hasta la vista, baby. It's over for us: I've found someone younger... and 25K thinner.
Thanks for sharing that tip, Jeff! I'm happy not to spend time on improving the build tools for now, and this cuts out one more bit of complexity. Python should always look this clean and simple!
One note to any rookies: Python actually compiles to bytecode, which is stored in .pyc files. It's unlikely you'd want to include your entire app in source form (.py files), so for any real app you'll among other things want to compile the .py files to .pyc at packaging time and include those instead of a mass of .py files. The package will be smaller and startup time will be shortened a bit as the runtime won't be compiling your code each time the app launches. I'll go into more detail on this whole area in later posts.
As of June 2012, we've renamed this the BlackBerry-Py Project. You can follow news about it at @BBPyProject and the web site has moved too.