So usually you use the dx tool that is part of the Android SDK to convert from JVM classes to Davlik byte code. A bit of information can be found here (and of course in the Android docs):

http://stackoverflow.com/questions/8348144/how-to-convert-jar-or-class-to-dex-file

However, I must admit that even though I am interested in this topic I have kind of lost hope for full libGDX support via Jython, and that was the very reason for me to start this topic in the first place. I have problems calling a Java class method from a class derived in Jython, as discussed some days ago with Jim. So far I have found no solution to this. But since it is an essential need-to-have to get libGDX working, I cannot follow this Jython path at the moment :(



On 21 March 2014 18:53, Stefan Richthofer <Stefan.Richthofer@gmx.de> wrote:
I have also been wondering, why porting Jython to Android is so hard. (Just like I was wondering why support for native extensions was so hard. Then I decided to find out...)
 
> 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.
 
Why not try it without ahead-of-time compilation first?
Just generate the usual classes in ordinary java bytecode like Jython is already doing.
Then invoke the dx-tool on the fly with these classes as input. Of course this would be
not as performant as ahead-of-time compilation, but the slowdown should be only on startup
as long as the app does not generate Python code at runtime extensively...
This might at least result in a usable but maybe slow solution.
 
Is the problem that the dx-tool is not available on Android? I admit that this might be an issue. One could try to let it convert itself to dex files (the dx-tool is written in Java as far as I know)
and bundle it with Android-Jython.
As a further step, one could try to write an Android Jython that directly generates dalvik code. Maybe one could utilize source code from the dex-tool (is it included in the Android sources
http://code.google.com/p/dexmaker/ to get this on the way.
 
@ Matthias {
>>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
 
Are these available on Android? First step would be to test this. I have been googeling for such stuff but did not find satisfying results. Have
you seen documentation on this etc? However, I must admit that I am not familiar with the Android SDK.
}
 
>>
try:
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
 
This should be no issue, if the dalvik code was generated on runtime after the decision for the import has been made
(Either via dex-tool or somehow directly), should it? If it is an issue, how does Jython currently resolve this?
 
 
I find this topic highly interesting, but unfortunately have no time to work on it. (Already happy to find time for JyNI development beside work).
 
Cheers
 
Stefan
 
 
Gesendet: Freitag, 21. März 2014 um 16:17 Uhr
Von: "Jim Baker" <jbaker@zyasoft.com>
An: "Matthias F. Brandstetter" <haimat@gmail.com>
Cc: "Jython Developers" <jython-dev@lists.sourceforge.net>
Betreff: Re: [Jython-dev] Jython on Android
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 (https://github.com/jythontools/clamp/).
 
But Clamp does not work with classes that cannot always be resolved to be the same. Consider the following example:
 
 
try:
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 https://gist.github.com/jimbaker/6984552; 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 <haimat@gmail.com> 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
haimat@gmail.com

------------------------------------------------------------------------------
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!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Jython-dev mailing list
Jython-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-dev
 
------------------------------------------------------------------------------ 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! http://p.sf.net/sfu/13534_NeoTech_______________________________________________ Jython-dev mailing list Jython-dev@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jython-dev



--
Matthias F. Brandstetter
haimat@gmail.com