Python for Android
github.com
github.com
It's a project that allow you to compile Python and dependencies, and package it along your own application. You could for example compile Numpy, ZMQ, sqlalchemy, zeroconf, etc...
We have a mechanism called bootstrap that allow you to select your primary technology that you're going to use for your application, such as "sdl2" (required for kivy), "webview" (it just start a webpage that connect to your phone port 5000, so you can start an embed webserver such as Flask to serve your app), "service_only" (if you don't need any kind of UI). New bootstrap can be written to support another type of apps.
Also, you could embed your whole UI in pure java if you wanted too, as the directory libs (to include .jar) or src (to include .java) are compiled. So you can imagine having your app in Java, but still call Python for something else if you really wanted too.
That's a pretty interesting possibility, I might tinker with this. I don't mind Java, but vastly prefer Python+numpy+scipy for technical computing and algorithmic experimentation. Java for the GUI/platform specific stuff with the creative stuff in Python works well for some side projects that I've done. In those cases, I interfaced them via REST API to a server, then later translated to pure Java.
I rolled my own solution [0] which uses crystax distribution's of CPython for android (Python 3.5), and I did a very simple JNI bridge between java and cpython using an RPC-like mechanism over json. Basically, on the java side you call a function and pass the arguments [1], and on the python side you "route" it to your own functions [2]. I think all pybridge's code is very very simple for those who understand a bit of Java, a bit of C and a bit of Python, and I explain in the readme how everything works, including which classes/files are relevant.
I have an app where I use this [3], and although it hasn't earn me much money so far (~20 downloads, 110€), it is rated 4.7 in the play store, so the performance of accessing python functions on android is not bad at all..
[0] https://github.com/joaoventura/pybridge
[3] https://play.google.com/store/apps/details?id=com.flatangle....
CrystaX's support is great though, and actually python-for-android uses this for Python 3 builds.
Why use python for this ?
It this is instead a way to package and distribute compiled Python it may be interesting but I'm not sure Android is the best target.
1. If you haven't already discovered Termux, do. It's the one thing on Android that doesn't manifestly suck.
2. I don't mess much with Python, but Termux + python + pip allowed me to install youtube-dl and be able to download and play videos (from numerous platforms) on-device rather than streamed-only.
leouieda.com/blog/scipy-on-android.html
It worked as advertised. Granted, it was a symbolic gesture, since I can't imagine doing any real work on my phone, but it answers the question of whether I could live with an Android tablet as a laptop replacement. My definition du jour of a "real computer" is anything that can run Jupyter. ;-)
Being kind of ignorant of the inner workings of this stuff, I was quite impressed.
I think it just depends on the size of the phone. My current phone has a 5.5" screen and since the dev environment I use was updated to make best use of it, I find it quite usable for programming where my previous 4" device really wasnt, but it's close to the lower bound of what is usable.
It's actually an iPhone 6+ and I use Pythonista, but my point is a large enough phone can reach a level of acceptability and I don't think that's a platform specific point.
The one main problem involves shortcut keys. Chrome OS intercepts certain key presses, such as "CTRL N" or "CTRL T", and routes them to the browser. Makes emacs / vim a little annoying, but a Chrome OS dev assured me this was on the "to fix" list.
[0] https://hn.algolia.com/?query=rubymotion&sort=byPopularity&p...
That's means it's a better product. You can have the best code in the world, but nobody is going to give a damn if they don't know how to use it.
An alternative approach is VOC (http://pybee.org/voc) - this is compiler that takes Python code, and output Java bytecode. This means you can use the Android native UI components, because your Python code becomes indistinguishable from code written in Java. Combine with Briefcase (http://pybee.org/briefcase), and you can get an Android app with one command; combine with Toga (http://pybee.org/toga), and you can get a cross platform app using native system components.
If you want to see this in action, here's a video: https://www.youtube.com/watch?v=RisCgSIWwLA
(Full disclosure - I'm the primary author of these tools)
So I remember hearing that a difficulty with hooking Python to Android is that there's some sort of limit to the amount of Java methods you can access through the JNI, but I could never find a proper explanation of this.
If I just had a version of CPython compiled on Android, given Java's nature of running on a virtual machine, wouldn't it be possible to dynamically load arbitrary classes through the JNI and use the native Android stuff?
What are the primary difficulties to this, if you ignore performance?
An alternative approach is VOC (http://pybee.org/voc) - this is compiler that takes Python code, and output Java bytecode. This means you can use the Android native UI components, because your Python code becomes indistinguishable from code written in Java. Combine with Briefcase (http://pybee.org/briefcase), and you can get an Android app with one command; combine with Toga (http://pybee.org/toga), and you can get a cross platform app using native system components.
If you want to see this in action, here's a video: https://www.youtube.com/watch?v=RisCgSIWwLA
(Full disclosure - I'm the primary author of these tools)
Without it, my feeling is that you lose the benefits of Python's dynamic nature and still suffer the drawbacks - the compiled code is ten times slower than CPython, according to the FAQ. Given those tradeoffs I'd prefer Kotlin, even though Python is my favourite language.
No disrespect ot the work, but I'm always a bit worried about a thing that starts with "we're swapping out the Python interpreter for a compiler". For example, this doesn't include generator support, which is a big part of my Python usage.
"Just" having the CPython bridge is so tempting, because your python code will work (modulo C extensions), plus we get all the performance work too. I believe you tried this, of course.
For VOC, is there a guide to calling Java code from Python code? I see you have Toga, but being able to write my own abstractions without much work would be neat.
As for a guide to calling Java from Python - we don't have any good documentation for this (yet), but it's entirely possible. Here's a sample Android application. If you're familiar with the Android APIs, you should be able to see the syntax conventions in place. https://gist.github.com/freakboy3742/7beb22c587e57240610777a...
"there are some language features (like generators and context managers) that still need to be implemented"
I suppose that this is more "Python (the language) for other systems" more than "Python (the environment) for other systems". Is any of the standard library supported given this import scenario?
Even nowadays with Android N, the NDK is kind of WIP.
My point is that the way Android team deals with NDK users seems like the NDK was imposed on the team, and they don't spend much developer time on it, looking from the set of open issues, outdated documentation where ndk-build is still used instead of cmake, integration with aar is still in progress, and set of available APIs.
https://code.google.com/p/android/issues/list?can=2&q=NDK&so...
I cannot deny they have done lots of progress, but there seems not to be much interest in having a comparable experience to the Java tooling.
And that as I mentioned it still is a work-in-progress, 2nd class developer experience (down to forcing JNI calls to C++ libraries on the device like e.g. Skia), compared with the Java based tooling on the Android SDK.
It doesn't seem to get the love an essential tool is supposed to get.
So I can create apps in pure python without using Kivy or sdl?
It sounds awesome.
http://www.artima.com/weblogs/viewpost.jsp?thread=148064
I never got to use one, but apparently there were APIs for just about every part of the phone that could be scripted with Python.
Unlike pyinstaller, we don't scan import to try reducing the distribution on the phone. Instead we have a blacklist.txt which you can put file pattern to exclude during packaging. I remember for one app in Python 2 to reduce the size around the 5MB.
We won't be able to reduce much more, as Python interpreter is embedded. We also don't want to adopt an approach like Qt where Qt binaries can be shared for all Qt-application, which break the all-in-one APK, open some potential issues, and add more work to manage dependencies.
VOC is compiler that takes Python code, and output Java bytecode. This means you can use the Android native UI components, because your Python code becomes indistinguishable from code written in Java. Combine with Briefcase (http://pybee.org/briefcase), and you can get an Android app with one command; combine with Toga (http://pybee.org/toga), and you can get a cross platform app using native system components.
If you want to see this in action, here's a video: https://www.youtube.com/watch?v=RisCgSIWwLA
(Full disclosure - I'm the primary author of these tools)
http://omz-software.com/pythonista/
its not quite the same, but you can build apps (sorta, i guess?) right on iOS, you can run python, and with a package like stash you can even use pip!
https://github.com/ywangd/stash
this is cool for android though!
Suffice to say it's perfectly possible, most phones are plenty powerful enough.
[EDIT]: if it's not rooted, I know you can still use it with a large portion of mobile traffic by adding the trusted cert it generates and then setting it up as a network proxy through standard settings changes (ie: no iptables transparent proxy necessary). I believe some apps will detect this.
for example Go language, I heard is not a total substitute for Java for Android