PL13
PL13

Reputation: 413

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 e.t.c.).

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)

Upvotes: 14

Views: 20551

Answers (3)

danielrosero
danielrosero

Reputation: 614

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

Upvotes: 0

Josh Chappelle
Josh Chappelle

Reputation: 1588

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.

Upvotes: 2

masnun
masnun

Reputation: 11906

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

Upvotes: 1

Related Questions