Android and Python

Is it possible to port existing python scripts to android using SL4A or ASE? What I specifically want to do is create an Android application with the usual interface elements and run python scripts from the application itself, get it and display it. Is it possible?

+4
source share
4 answers

Personnaly I think you implicitly specify three points:

Desktop / Mobile Compatibility

The first is compatibility between the python library on the phone and the python library on your computer. If you are not using a third-party library and remember to include the python extra library provided by sl4a, you should usually be fine.

Packaging

The second point is how to package a Python application for Android. This can easily be done with a wrapper, also described on the sl4a website. In fact, they are distributing a copy of the chapter on the SL4A book that describes how to do this. So this is possible, but keep in mind that the user will need to install python if he has not already done so (this is a kind of warning when starting the application).

User interface

The third thing you should keep in mind is that you are not 100% free regarding the widget / layout that you can use with sl4a. Namely, you cannot do what you could do in Java or Scala. Thus, you may have to consider changing your user interface and instead use a webview (which can interact with python back and forth) with the framework to get a β€œmobile look”

Hope this helps.

+2
source

I think what you want to do is independent of the apk file, which includes python interpretation and your script code.

I do not know if this can be done in Android. The only one I know runs your script from the Android scripting environment.

0
source

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

0
source

I had a similar problem, and finally it was solved 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. Therefore, I don’t use 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-compilation extensions from Mac OS X Snow Leopard. My Python modules only do text input and output, accept socket connections, etc. No interaction with the Android API. All this works great: writing a Java stream to Python input and reading a Java stream to get Python output. Extensions C are built using P4A instructions . (Android could not find .so dynalibs until I added

[build_ext] inplace=1 

to setup.cfg during build. I think this is because the install installation is never called on Android. I just press adb.

All states of the activity lifecycle seem to work, but I cannot yet determine whether the subprocess will automatically pause while the main process is in a stopped state.

I can post the code in a couple of weeks if that is interesting. (Only departure on vacation.)

My packaging plan is to put a ZIP archive of Python code in / assets and have an application for unpacking during the first onCreate. I have not implemented this yet, but I do not expect any problems.

0
source

Source: https://habr.com/ru/post/1379472/


All Articles