在 Java 中从 Android 应用程序执行 Python 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34235763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute Python script from Android app in Java?
提问by PL13
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.).
我试图找到一种方法从我的 Java 代码在 Android 中执行 Python 脚本。我对此事进行了研究,但我发现的唯一一件事是,如何将 APK 中的 Python 脚本转换为 android(Kivy 等)。
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.
更具体地说,我有一个包含很多函数的脚本。我想要做的是在我的 java 代码中基于这个 python 脚本创建一个对象,并通过这个对象来调用我的函数。
I can't convert my Python code into Java, because I use various libraries that exists only in python.
我无法将 Python 代码转换为 Java,因为我使用了仅存在于 Python 中的各种库。
Any advice would be helpful and deeply appreciated.
任何建议都会有所帮助并深表感谢。
The following a snippet of my Python script. I use the library charm for cryptography.
以下是我的 Python 脚本的片段。我使用库的魅力进行密码学。
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)
回答by masnun
You can use SL4A project which would allow you run your Python code on Android.
您可以使用 SL4A 项目,它允许您在 Android 上运行您的 Python 代码。
回答by Josh Chappelle
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.
一种选择是创建一个 Web 服务 API,以允许通过 HTTP 提供您的 Python 结果。那么这只是 Android 与 HTTP Web 服务通信的问题,这很简单。
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.
你可能会发现一些 python 到 java 的桥接器,但根据我的经验,这些东西在实践中总是有局限性,理论上看起来很棒,但最终不能正常工作。
回答by danielrosero
Check out Qpython, could be an easy solution. You can run your python script against the Qpython android python core, or deploy yours.
查看 Qpython,可能是一个简单的解决方案。您可以针对 Qpython android python 内核运行您的 python 脚本,或者部署您的脚本。
Checkout this repository: https://github.com/qpython-android/app-call-qpython-api
签出这个存储库:https: //github.com/qpython-android/app-call-qpython-api