3 ms·
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. A
by freakboy3742 10y 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 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 http://pybee.org/briefcase), and you can get an Android app with one command; combine with Toga (http://pybee.org/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 https://www.youtube.com/watch?v=RisCgSIWwLA
(Full disclosure - I'm the primary author of these tools)
- rtpg 10y agoAh, 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.
- freakboy3742 10y agoGood 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/7beb22c587e57240610777a44af645d8 https://gist.github.com/freakboy3742/7beb22c587e57240610777a...
- rtpg 10y agoOh, 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?
- freakboy3742 10y agoThe 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 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).
- kingosticks 10y agoThat 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"
- pjmlp 10y agoI 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.
- richardwhiuk 10y agoThe NDK is essential for a large numbers of apps - including games and real time media applications.
- pjmlp 10y agoAre 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.
- TuringNYC 10y agoWe 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.
- pjmlp 10y agoI 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&sort=-opened&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened 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.
- ata2001 10y agoThis is what I've been looking for! So I can create apps in pure python without using Kivy or sdl? It sounds awesome.
- ptx 10y agoIt 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.