Hacker News new | ask | show | jobs
by freakboy3742 3375 days ago
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)

4 comments

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.