简体   繁体   中英

Execute Python script from Android app in Java?

I am trying to find a way to execute a Python script from my Java code in Android. I did a research on this matter but the only thing I found is, how I can convert python scripts in APK for android (Kivy etc).

More specifically I have a script which contains a lot of functions.What i want to do is to create an object based on this python script inside my java code and via this object to call my functions.

I can't convert my Python code into Java, because I use various libraries that exists only in python.

Any advice would be helpful and deeply appreciated.


The following a snippet of my Python script. I use the library charm for cryptography.

from charm.core.math.integer import integer,serialize,deserialize
class serializeClass:
    def __init__(self)
        ...

    def serialize(self, charm_object):
        assert type(charm_object) == integer, "required type is integer, not: ", type(charm_object)
        return serialize(charm_object)

    def deserialize(self, object):
        assert type(object) == bytes, "required type is bytes, not: ", type(object)
    return deserialize(object)

You can use SL4A project which would allow you run your Python code on Android.

One option would be to create a Web Service API to allow your python results be served up over HTTP. Then it's just a matter of Android communicating with an HTTP web service which is simple.

You might find some python to java bridge out there but in my experience these things always have limitations in practice and seem great in theory but don't end up working correctly.

Check out Qpython, could be an easy solution. You can run your python script against the Qpython android python core, or deploy yours.

Checkout this repository: https://github.com/qpython-android/app-call-qpython-api

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM