Hacker News new | past | comments | ask | show | jobs | submit login
Python for Android (github.com/kivy)
340 points by gregorymichael on March 27, 2017 | hide | past | favorite | 61 comments



Please note that Python for Android project is not meant to be used with Kivy only anymore.

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.


Just an addition, there is many others alternatives, but it may be hard for others to be able to compile the Python extensions you want (or impossible). That's what we wanted to do here: ability to pick the extension needed, provide a "recipe" that contain specialized tricks or patches to extension for running in Android, etc.


> 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.


Suggestion : could you add a section on the github page explaining what is the use case is for this ?


Amazing.


Some years ago I tried to use python-for-android, but found it too much of a burden to do what I wanted, which was just to simply integrate existing python code in native android apps (with Java). Don't know how much has changed since then, but python-for-android's bootstrap used pygame and pysdl to start an opengl scene, where you would have completely non-native widgets.

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

[1] https://goo.gl/sw4JLt

[2] https://goo.gl/17SlVb

[3] https://play.google.com/store/apps/details?id=com.flatangle....


We've actually completely rewritten python-for-android since then, it now supports multiple bootstraps and is not tied to Kivy at all, and it should be relatively easy to use the python distribution in a native app (although this could be streamlined).

CrystaX's support is great though, and actually python-for-android uses this for Python 3 builds.


I looked at the app, as far as I can tell, it seems fairly basic.

Why use python for this ?


Because I already had a lot of code doing a lot of things in Python (https://github.com/flatangle/flatlib/)..


Can't do a lot of digging on this right now(phone), but for just running Python of various versions take a look at termux, QPython and SL4A. Some may now be defunct, but searches on those should guide you to current info.

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.


Seconding this.

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.


I followed the instructions at this site to install Jupyter/Python including numpy and matplotlib, on my Android phone:

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 can't imagine doing any real work on my phone...

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.


I can tell you from experience Termux works mostly very well on Chromebooks which come with the Play Store. Gives a nice option to putting the device in developer mode and installing Crouton.

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.


No such keyboard issues on my tablet, FWIW.


Tablet and bluetooth keyboard are a step up, admittedly.


OT but this made me think of RubyMotion, which I remember being wildly popular (among Rubyists) back in the day but don't hear much about it now [0]. How many people still use it?

[0] https://hn.algolia.com/?query=rubymotion&sort=byPopularity&p...


RubyMotion was amazing but what it had in ability was superseeded by React Native with everyone and their mother doing a React + Rails combo for websites now. In the end, writing plain Ruby became more cumbersome than writing React so people just abandoned the platform. The price tag of React Native didn't help the issue either (free) along with it just being a npm install away.


I do not thin react native superseeded RM by being a better product. RM is super polished but failed at popularizing itself hence losing a very big chunk of the market. RN on the other hand crushed their competitors by its huge popularity, number of articles, documentation and tutorials available.


> RN on the other hand crushed their competitors by its huge popularity, number of articles, documentation and tutorials available.

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.


You've not heard of "worse is better" [1] then. Many times in the past the worse product has succeeded over the better product.

[1] https://www.dreamsongs.com/WorseIsBetter.html


Because the people who implement the technology aren't those who can admire the "quality". If it's been 6 hours at work and I have 30 minutes to pick a tech to start evaluating, I'm picking the one with better documentation/easier curve. Most people aren't a Facebook and don't need the best greatest thing in the world, just something to fix the current problem. You can kick the can down the road.


From a very cursory look this doesn't seem to hook into the android framework at all including the native UI or other system goodies like notifications and locations, etc. The apps would run in their own little world with their own provided non-native UI.


This seems to be just one of the components of a larger set spread across multiple repos. [1] seems to be the component to access native APIs.

[1]: https://github.com/kivy/plyer


AFAIK, Plyer will give you access to native APIs like accelerometers, but not to the native GUI layer.

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)


Hey Freakboy, always good to see your coding projects. Keep up the good work!


This project(Kivy) doesn't use the native Android UI components, so it's a bit of a negative. You don't get any of the stuff like accessibility.

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?


The primary difficulty is that JNI is really limited on Android. It exists, but it's really slow, and there are some hard limits imposed at the kernel level.

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)


It would be neat if VOC had an interpreter in addition to the compiler, kind of like Gambit Scheme does (and other Lisp implementations). That way you would have the benefits of compiling to Java bytecode (to easily interoperate with the API) while also being able to use the interpreter to modify the app while it's running, for easy development (i.e. live reloading).

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.


Ah, I remember watching the PyCon talk on VOC, a lot of cool work.

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.


Good news on the generator front - VOC fully supports generators, lambdas, passing functions as arguments, and all sorts of things that Python allows but Java (the language) doesn't.

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...


That is good news. Perhaps the website could be updated to reflect that, it might be putting people off.

"there are some language features (like generators and context managers) that still need to be implemented"


Oh, so you simply import java packages in the same way you import python packages? That's a pretty transparent mechanism. Thanks for the clarifications!

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?


The full standard library is a work in progress; at the moment, there's only a couple of key parts of sys, os, and the like. But Ouroboros (https://github.com/pybee/ouroboros) is a project to develop a full standard library that can be integrated into any Python implementation. Effectively, you can use all the bits of the standard library that are written in Python by just compiling them; the rest requires native integration (which is the work in progress).


I always get the feeling that if the Android team got to choose the NDK would never been a thing, rather it was an upper management decision that they had to offer some kind of support for C and C++.

Even nowadays with Android N, the NDK is kind of WIP.


We did Computer Vision work on Android before OpenCV ported to Android and the NDK was the only realistic way to achieve any real-time video/image analysis performance. Using the NDK was tedious and HR-expensive but we had no choice. So in that case, we were glad the NDK was an option.


I am also glad it is there, it is also where I spend most of my Android coding time.

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.


The NDK is essential for a large numbers of apps - including games and real time media applications.


Are you aware that it only got introduced in Android 2.2 right?

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.


This is what I've been looking for!

So I can create apps in pure python without using Kivy or sdl?

It sounds awesome.


Firstly, Looks like it's been around for a couple of years. Is the idea that you could deploy your python code as an android app. I'd like to see some examples apps in the wild, so I could check out performance, etc. But could be cool. I can imagine redeploying my web-app with this, and then doing some funky database syncing with the cloud.


I remember hearing about the Nokia phone with Python:

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.


Yup, you could run it on any S60 phone, and even access stuff like 3D via OpenGL ES.


How big are the generated APKs compared to a normal Android app of similar functionality?


A minimal hello world with Python 3 required 7MB. But it's not optimized.

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.


It looks like a 'Hello World' will result in about a 10 Mib APK which takes up roughly double that space when unpacked on the mobile device. This is not surprising because the APK must contain the Kivy library and a Python interpreter. After that initial hurdle it will scale relatively well, though that can depend on which extensions and other files you include.


If you want a smaller target, VOC (http://pybee.org/voc) can generate a "Hello World" APK in about 1.6MB.

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)


That's not too bad. I see plenty of apps installed on my phone that are 2 to 6x that size, and a few even larger. Appreciate the info.


I'm assuming that 10MB would be less if you didn't want to use Kivy. However, I assume that using Django would probably take even more space that Kivy. So could it get big fast?


Python for Android is an extension for Kivy, so it is not optional. It looks like apps could get quite big since adding functionality involves including Python libraries to be called from Python rather than interacting with the Android API.


Kivy actually is completely optional, python-for-android doesn't require any particular frontend. The main supported options for guis other than Kivy are SDL2 (you can use e.g. PySDL2) or a webview, but it's designed to be easy to add more options.


I know this is about Python for Android. I just wanted to say for all those iOS users who might read this and think "well this is of interest" you might want to check out Pythonista

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!


From the issue tracker, it looks like the project could use some help - anyone want to hop on and triage?


you're someone


I wonder if this allows running things like mitmproxy on your phone. Or maybe someone is aware of a good alternative?


I've actually run mitmproxy on my phone in a chroot before, somehow (several years ago now). However, I can't remember if it was a rooted phone or not, sorry.

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.


Yeah no problem if root is required, I just wanna know what did you use as a layer in between it and android. Maybe there's a relevant tutorial I could follow?


Congrats to the whole team on the release!


can Python now be used instead of Java?

for example Go language, I heard is not a total substitute for Java for Android


Yes, but with the caveat that you'll be running interpreted .py using a cross-compiled Python executable, rather than Python compiled to Android bytecode. Kivy itself (the project that spawned p4a) is a full cross-platform library for UI development; for any heavy computational lifting you'd want to farm it out to a C API library and import it the usual way, modulo some p4a recipe incantations.


I used Qpython for a little while. I think it's based on kivy.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: