66

The other day I came across a Python implementation called Jython.
With Jython you can write Java applications with Python and compile them to pure Java.

I was wondering: Android programming is done with Java.
So, is it possible to make Android apps with Jython?

mid_kid
  • 1,460
  • 2
  • 13
  • 15
  • 1
    Python do not have awesome interoperability with java. If you want native and nice ui, you are forced to use java/scala. If you are ok with non native ui, you can use qt for android + python wrapper + write code in python. – logcat Jun 20 '12 at 13:05
  • I see... So you can't do anything you can do in java with jython? – mid_kid Jun 20 '12 at 13:10
  • 9
    @tabbykitten that interop between Python and Java is exactly what Jython *does* provide. – lvc Jun 20 '12 at 14:45
  • 1
    can you create Jython class which extends android.app.Activity? – logcat Jun 20 '12 at 18:22
  • 1
    @tabbykitten for clarity, its better to ask that as: does Jython allow you to write Python subclasses of Java classes (or, of that specific Java class)? And, yes. [Yes, it does](http://www.jython.org/archive/21/docs/subclassing.html). "Writing a Jython class" is a little ambiguous, since Jython is just a Python implementation (not a dialect as the OP says), so it could mean a class that forms part of *jython itself* (which trivially can subclass Java classes, since Jython is written in Java). – lvc Jun 21 '12 at 01:29
  • are you sure that jython extend Java class? There are post that says another https://groups.google.com/d/msg/android-developers/uFSnwuUJZvY/Nu63jyCEQbgJ . I'am python user, not jython. Perhaps you know more about it. – logcat Jun 21 '12 at 09:45
  • 1
    @tabbykitten that post seems to suggest that Python classes extending Java classes don't quite behave properly when passed to Java APIs - I've never tried that specifically, but that seems to be anomalous to everything else I can find about this - eg, http://stackoverflow.com/questions/11071525/ has a Python class implement Runnable in order to use Java thread management APIs. If it *is* a problem (maybe an `extends` vs `implements` difference?), you can always code some Java glue to make it work - Jython is just Python for the JVM; you can mix other JVM languages without problem. – lvc Jun 21 '12 at 15:22
  • I agree that jython code can be written and used, but you have to write some Java glue. Android Application Framework has some frame and require "extend" too often. – logcat Jun 21 '12 at 16:02
  • 2
    The actual issue, at least with current Jython is totally missed in this whole thread, including the accepted answer: Jython also generates and loads bytecode *at runtime*. Inherently. Even if it could do this as dexed bytecode or somehow dex it at runtime, Android does not permit loading new bytecode at runtime (AFAIK). Without a significant redesign, Jython on Android is currently not feasible, unfortunately. See https://sourceforge.net/p/jython/mailman/message/36752389/ – stewori Feb 19 '20 at 17:39

7 Answers7

45

Jython doesn't compile to "pure java", it compiles to java bytecode - ie, to *.class files. To develop for Android, one further compiles java bytecode to Dalvik bytecode. This means that, yes, Jython can let you use Python for developing Android, subject to you getting it to play nice with the Android SDK (I haven't personally tried this, so I don't know how hard it actually is) - you do need to make sure you don't depend on any Java APIs that Android doesn't provide, and might need to have some of the Android API .class files around when you run jython. Aside from these niggles, your core idea should work - Jython does, indeed, let write code in Python that interacts with anything else that runs on the JVM.

lvc
  • 34,233
  • 10
  • 73
  • 98
  • 2
    according to https://wiki.python.org/jython/JythonFaq/GeneralInfo#Can_I_use_Jython_to_make_apps_for_mobile_phones.3F jython requires JME instead of JSE and that there is no port to the former yet. So is this answer pure theory or has this actually been done by anyone? – Tobias Kienzler Aug 17 '14 at 14:18
  • 3
    @TobiasKienzler the answer is largely theoretical - I do say in the answer that I haven't tried this, and that getting Jython and the Android tools to work together could be a problem. That said, I don't think the JME/JSE difference matters here, since the Python->Java bytecode step would be done *on the development machine*, not on the Android device. Java SE is available on every platform supported by the Android SDK, and the newer Android Studio. – lvc Aug 19 '14 at 12:47
  • thanks for your reply - incidentally I _was_ hoping to use jython for native android development... – Tobias Kienzler Aug 19 '14 at 13:17
  • 1
    Android is not the same as Java Micro Edition. JME was used on the 'pre-smartphone phones', like your average Nokia from the early 2000's. Android is an entirely different thing, the FAQ item on the Jython page is misleading on this subject. – sentiao Nov 03 '14 at 20:48
5

As long as it compiles to pure java (with some constraints, as some APIs are not available), but I doubt that python will be of much use in development of android-specific stuff like activities and UI manipulation code.

You also have to take care of application size - that is serious constraint for mobile developement.

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • Considering that the javac based output of the jython code does not include a python runtime, but only jvm code, how so? Talking in terms of application size. – jheld Feb 20 '16 at 02:22
5

I specially like Kivy

Scripting Layer for Android (SL4A) brings scripting languages to Android by allowing you to edit and execute scripts and interactive interpreters directly on the Android device.

Freeman
  • 5,810
  • 3
  • 47
  • 48
3

Yes and no. With jython you can use java classes to compile for the JVM. But Android use the DVM (Dalvik Virtual Machine) and the compiled code is different. You have to use tools to convert from JVM code to DVM.

1

Yes, you can.

Test your python code on your computer and, when it is ok, copy to your Android device.

Install Pydroid from Google Play Store and compile your code again inside the application and you will get your App ready and running.

Use pip inside Pydroid to install any dependencies.

PS: You will need to configure your Android device to install APKs from outside Play Store.

-3

It's not possible. You can't use jython with android because the DVM doesn't understand it. DVM is not JVM.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
The VOYOU
  • 522
  • 4
  • 14
  • Thanks @Burhan Khalid I'll be careful about these little but important things in my future postings with-in StackOverFlow. – The VOYOU Dec 21 '12 at 07:31
  • I already admitted my English writing mistakes yet somebody down voting me even now. :( To err is human, important is to learn from your mistakes. – The VOYOU May 18 '13 at 07:48
  • Those application are using "jythonroid" i.e. Jython for Android... How come; somebody comes just out of no where and down-votes you for something he knows nothing... hehehe :-p. – The VOYOU Aug 28 '13 at 13:03
  • Bro You really need to work hard on your findings. First `no where is written there that the app is using jython`... second `They are using python`... and You can run `python/jython` in android as I told you but you need to apply some different treatment to android and last `There is a hug difference between python and jython`. – The VOYOU Aug 31 '13 at 08:51
-5

sadly No.

Mobile phones only have Java ME (Micro Edition) but Jython requires Java SE (Standard Edition). There is no Jython port to ME, and there is not enough interest to make it worth the effort.

user3224611
  • 84
  • 1
  • 7
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – A.J. Uppal Apr 27 '15 at 00:22
  • 1
    @A.J. The question is "is it possible to run Jython in Android". So, it IS an answer. With absolutely incorrect explanation, yes, but an answer it is. – Gangnus Apr 13 '16 at 07:54
  • @Gangnus, to be honest, that close vote is quite general, I was more so saying it because there is no evidence or reasoning, it could easily be encapsulated in a comment, which the OP cannot make, hence the close vote. – A.J. Uppal Apr 13 '16 at 16:13
  • Android is *NOT* using Java ME. – jiwopene Sep 28 '18 at 14:08