4

Can I port existing python scripts on android using SL4A or ASE ? What I specifically want to do is to create an android application with normal UI elements and run the python scripts from the application itself, get the output and display it. Is this possible ?

Amritha
  • 795
  • 3
  • 9
  • 26
  • What kind of script do you want to port? – lc2817 Nov 03 '11 at 13:12
  • Its actually a file parser script.I have a folder full of scripts which I want to port. So in the application the user should be able to feed in an input file (through Java UI) and the output is the parsed file structure. The command to run the script and the file should be passed from java to python i guess.. – Amritha Nov 03 '11 at 13:44
  • It should work, I think. – lc2817 Nov 03 '11 at 14:31
  • The framework currently runs on linux. That should not be a problem right ? It should still run when loaded on the ASE ? – Amritha Nov 03 '11 at 15:07
  • I think so, tell us if you encounter any kind of problem. – lc2817 Nov 03 '11 at 15:11
  • How do I download zipped files containing python source code into the android SL4A ? I ve tried going to the website and downloading it but the download always fails. – Amritha Nov 06 '11 at 08:13
  • You don't get the python source code but the implementation of python. It is an APK that you can download from the sl4a website. – lc2817 Nov 06 '11 at 20:13

4 Answers4

2

Personnaly I think that you ask implicitly for three points:

Compatibility Desktop/Mobile

The first is the compatibility between the python library on the phone and the python library in your computer. If you don't use third party library and be sure to include the extra python library provided by sl4a, you should generally be ok.

Packaging

The second point is about how to package a Python app for android. It can easily be done with a wrapper as well described on the sl4a website. In fact they distribute a copy of a chapter of a book about SL4A that describes how to do that. So that is possible, but bear in mind that the user will be required to install python if he hasn't done so already (this is a sort of alert at the launch of the app)

UI

The third thing that you should have in mind is that you are not a 100% free concerning the widget/layout you can use with sl4a. Namely you can't really do whatever you would be able to do in Java or Scala. So you might have to consider altering your UI and do a webview instead (that can communicate back and forth with the python) with a framework to obtain a "mobile look and feel"

Hope this helps.

lc2817
  • 3,722
  • 16
  • 40
0

I had a similar problem and finally solved it by writing a small singleton class in Java that runs the Python-4-Android binary from the SL4A installation in a subprocess created using java.lang.ProcessBuilder. So I'm not using the SL4A mechanisms at all (triggers, upcalls, etc), just borrowing Python.

This seems cleaner than trying to start and connect to a Python process through SL4A.

This is Python 2.7.1, cross-compiling extensions from Mac OS X Snow Leopard. My Python modules are doing only text input and output, accepting socket connections, etc. No interaction with the Android API. It all works fine: writing a Java stream to Python input and reading a Java stream to get Python output. C extensions are build using the P4A instructions . (Android could not find .so dynalibs until I added

[build_ext]
inplace=1

to setup.cfg during the build. I think this is because setup 'install' is never invoked on the Android. I'm just pushing with adb.

All the activity lifecycle states seem to be working, but I can't yet determine whether the sub-process is automatically suspended while the main process is in the stopped state.

I can post code in a couple of weeks if this is of interest. (Just departing on vacation.)

My plan for packaging is to put a ZIP archive of the Python code in /assets and have the app unzip during the first onCreate. I haven't implemented this yet, but I don't expect any problems.

Gene
  • 46,253
  • 4
  • 58
  • 96
0

I think, what you want to do is make an independently apk file, that includes the python interpret and your script code.

I don't know if this can be done in Android. The only whay I know is running your script from Android Scripting Environment.

Manu
  • 1,130
  • 3
  • 10
  • 26
  • Can I download existing python files into the scripting environment and call them from my java application ? – Amritha Nov 03 '11 at 13:03
  • Manu you could have used google before saying that. It is of course possible to do so. Amritha, yes you can certainly do that I think(I am not sure) provided you can load the interpreter with Java. – lc2817 Nov 03 '11 at 13:09
  • When I answer he didn't say any time that was developing in Java. Anyway, I've said "I think" do I deserve some reputation be removed by this? I don't think so. – Manu Nov 03 '11 at 15:12
0

If web interface is normal UI then look at this discussion http://groups.google.com/group/android-scripting/browse_thread/thread/f86812549d2686e2/f828f916411d7a95 . You can use Python, webView, HTML5 and JavaScript

Andrey Sboev
  • 7,454
  • 1
  • 20
  • 37