This is a great question, and certainly something we would like to see happen.

As you mentioned, the key is to convert from Java bytecode to Dalvik bytecode. Easy enough for Jython's own runtime and extlib dependencies, but this also needs to be done for any of the classes we generate.

In general, we would want ahead-of-time compilation. Many Python projects now do this, via setuptools, or they can be compiled by compileall, which setuptools uses.

But Jython does not by default compile the proxies it uses to integrate with Java classes and interfaces. Clamp is a new project we have worked on that supports doing just that (

But Clamp does not work with classes that cannot always be resolved to be the same. Consider the following example:

from better.baz.package import ImprovedBar
class Foo(ImprovedBar):
except ImportError:
from ok.baz.package import ImprovedBar
class Foo(Bar):
In either case the new class Foo is available, but Clamp in general cannot follow both paths (although we could certainly special case in certain cases), or if such classes are not immediately available at import time. Also consider the implications of  using the 3-arg type function to construct such classes - in general we cannot make all such proxies clampable.
It is possible to sidestep some of these issues using the following techniques:

1. For Python classes that extend Java interfaces (but not classes), we can use dynamic proxies. I outlined the basics of this in; everything else would be fairly boilerplate. The interface only limitation is from Java itself.

2. Python modules, including top level ones for say console interaction or equivalent exec/eval, can be compiled to Python bytecode, then run by the Python bytecode VM that I wrote about 5 years ago for this very purpose and continues to be included (accessed by pycimport, all currently experimental only). This also requires a Python bytecode compiler to be written.

Now it turns out that fully supporting step #2 is important for many other purposes, including support for certain types of invokedynamic optimizations. So we will most certainly want to write the Python bytecode compiler for Jython at some point. But let's be clear, this is not so easy. It will require not only writing such a compiler (I would recommend the classic control flow graph approach that is used by CPython; note that Jython has it easy because ASM does this CFG analysis for us), it would also to have pass our unit tests. Shashank Bharadwaj, a student who worked with me on invokedynamic, did some development that could be leveraged however.

Step #1 in contrast is certainly easy.

So that's what we need to do to support Android. Nothing too major, and in fact perhaps if one just used Clamp and dynamic proxies, some interesting Python project could be made to run on Android in a matter of a few days. (Games written using libGDX could be a good target.) That could provide good motivation for getting the more comprehensive work done.

Anyone interested? Talk to me on #jython

- Jim

On Thu, Mar 20, 2014 at 12:10 PM, Matthias F. Brandstetter <> wrote:
I was curious to see whether there is a way to get Jython scripts to run on Android devices. There are (or have been) some projects that try to provide this, but none of them is active any more.

But I would like to understand why this is such an issue. As far as I can see there are tools out there to convert from JVM byte code (.class files) to Davlik .dex files, e.g. included in the Android SDK. So couldn't one just convert the Jython class files into Davlik code using one such tool?

Apparently this is not as easy as I think, I guess otherwise there would be a solution already. But I would be interested in some technical details. Has anybody of you guys tried to run Jython on Android already, and what exactly is the problem here?

Thanks in advance for your clarification!

Matthias F. Brandstetter

Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
Jython-dev mailing list