How to execute Python script from Java code in Android - android

I'm trying to make an standard Android application execute a python script that could return values to Java, but I'm facing a lot of issues.
Jython doesnt support this in the Android environment, SL4A is a dead project, Kivi seems to be an full stack framework that do not use Java at all and QPython is SL4A that works on Android 5+ but almost don't have documentation.
I wanted something like:
// From INSIDE my Java Code
new PythonEngine().execute('a = 1 + 1').getInt('a')
With QPython I found an example that executes an arbitrary code, but I couldn't figure how to get the outcome and how to run the script without opening a new activity that waits for user input to come back.
Is that possible? The example can be found here QPython Java Sample

I don't think any of thoses projects will help. For example, Kivy drive the Python execution, even it it's started from Java.
If you have an application in Java, but want to start a Python Interpreter, i guess solution using https://code.google.com/p/android-python27/ might be a start. Unsure if they have a Java interface in top of the Python C-API.

Related

Run python code right out of github on phone

Is there any mobile python IDE that let you get download code from GitHub directly into the app? In fact, IDE would be an overkill - I don't need to code in the app, just need to run code as a quick demo to discuss ideas with people. My code don't launch GUIs, only console outputs.
My phone is Android, but iPhone answers are also welcome. Thanks!
Update:
There are a few suggestions that this may be a duplicate with Is there a way to run Python on Android?. I did read that one before I asked. It is either not the solution I am looking for or I simply misunderstood it - Kivy seems to be a framework to let you develop mobile apps using python, but what I am looking for is something that I can download my github repository and let me run one python file. Or you can say I want a python interpreter on my phone that can execute code (multiple modules/packages) from a online repository.
Anyway, I tried Kivy, and I got a screen like this (not intend to go off topic like the other question did, just trying to show Kivy did not solve my issue):
PyDroid 3 is half way what I want. It is a good mobile IDE to write some simple code in. But I couldn't find a way to download a repository in there. Copy-pasting works for those all-in-one .py files, but not practical for those code living in multiple modules/packages.
I'd suggest installing Termux and using it to run the code just like a linux terminal

How can a C program be used in Android to serve API from 127.0.0.1?

I know an open source project coded in C, which compiles as system executable for Linux, Windows, OS X, iOS and Android.
On Linux/Windows/OSX it can be just executed with ./example_programm or C:\>exeample_program.exe and it starts serving it's API through http://127.0.0.1:port_number, example: http://127.0.0.1:7778
on Desktop platforms this API can be used by GUI application.
I need to know how this can be done on Android ?
I know on Android adb shell we can do su and then run android compiled program executable, and then probably use open the HTML GUI from android web browser and use it. But, this shouldn't be the case I guess.
The executable doesn't need ANY root or superuser privileges. a limited user account if run the executable makes this executable run and the program starts serving API on 127.0.0.1.
As and alternative I heard Android can either use Cordova wrapper to include this android compiled executable in it and when the Android App starts the App can trigger to execute this android executable in backend. But, I'm not so sure at the moment, if that way will work or not, or what challenges will there be. This probably needs to be tested.
Another alternate way I heard is to have this C code compiled as a library. I have no idea of C coding done in this project, and I don't know what changes I need to make to compile it as Android library. Anyway if with some help I could have this C code compiled for Android as a library, can someone please let me know how this library can be called by the Android App, and it will behave exactly the same ways it behaves on Desktop platform ? like executing the executable and it starts serving API on 127.0.0.1:7778 ?
Thanks for help :)
At a high level, you can use the Android NDK to run C or C++ code in an Android application: https://developer.android.com/ndk/index.html
Whether or not "it will behave exactly the same ways it behaves on Desktop platform" is unknown because I don't know any details about this library. If it's a simple web service, it's likely that it will work.
Regarding making design decisions, I'd leave that alone and just go in with the knowledge that C code can run on Android, then have someone familiar with Android and/or the NDK determine the best way to use the code.

Run Python Code from Android

I'm writing Android app.
The problem is that it should execute some calculations and library for this is written in Python.
What is the best way to invoke Python from Android/Java?
I heard about Kivy and even managed to run application, but python code returns latex formulae, that can't be rendered within Kivy app.
You can probably render LaTeX in Kivy fairly easily using a png exporter (such as presumably exists for web export tools, and modes like emacs' preview mode).
If you need to run python as part of a java app, probably a practical way to do it is to use kivy's python-for-android tools with your own java frontend, invoking the python interpreter via JNI. This would require some thinking and experimentation but should be possible. There are also other projects for building python for android, which might be able to do the same things.

Android - after click on button execute c++ code(openCV)

I wrote 300 lines script (openCV) in c++ and I would like to execute the script after clicking on button in application for android. The script takes a image as an input and the output is edited image. I read about NDK but I am not very skilled in C++/Java, so I do not understand it. I am just learning how to do it. Can you advise me how to do it simple as possible?
Let's say I have simple UI with one button and after click it execute hello world written in c++.
I know how to code android application but i do not know how to put c++ with android together.
Thank you in advance!
Rather than complicating the issue with C++/JNI/NDK you could just rewrite your 300 lines in Java instead. OpenCV has a Java binding that is basically identical to the C++ calls. Java syntax is pretty close to C++ so you already have most of your coding work done. You will have to learn enough Java to handle the basic App framework and button interaction anyway. Go to the OpenCV site, http://OpenCV.org, and get a copy of the Android SDK and plug it into your development environment (Eclipse or Android Studio). I am using OpenCV-2.4.10-android-sdk, but I think 2.4.11 is out. In your IDE you just import as existing Android project. It has sample code built in that you can test right away and then modify to add your functionality.
There are some good tutorials available on setting up an IDE to add native code, NDK, JNI, but it is not really necessary to get OpenCV functionality.

Android and Python

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 ?
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.
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.
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
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.

Categories

Resources