安卓和Python
我可以使用 SL4A 或 ASE 在 Android 上移植现有的 python 脚本吗?我特别想做的是创建一个具有普通 UI 元素的 Android 应用程序,并从应用程序本身运行 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我个人认为你隐含地要求三点:
桌面/移动兼容性
首先是手机上的python库和计算机上的python库之间的兼容性。如果您不使用第三方库并确保包含 sl4a 提供的额外 python 库,那么通常应该没问题。
打包
第二点是关于如何为android打包Python应用程序。使用包装器可以轻松完成此操作,如 sl4a 网站上所述。事实上,他们分发了一本关于 SL4A 的书的一章的副本,其中描述了如何做到这一点。所以这是可能的,但请记住,如果用户还没有安装 python,则将被要求安装(这是应用程序启动时的一种警报)
UI
您应该记住的第三件事问题是,对于可以与 sl4a 一起使用的小部件/布局,您并不是 100% 免费的。也就是说,您实际上无法做到在 Java 或 Scala 中能够做到的任何事情。因此,您可能需要考虑更改您的 UI,并使用框架创建一个 webview(可以与 python 来回通信)以获得“移动外观和感觉”
希望这会有所帮助。
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.
我认为,你想要做的是制作一个独立的 apk 文件,其中包括 python 解释和你的脚本代码。
不知道安卓能不能实现这个功能。我唯一知道的是从 Android 脚本环境运行你的脚本。
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.
如果网络界面是普通用户界面,请查看此讨论 http://groups.google.com/group/android-scripting/browse_thread/thread/f86812549d2686e2/f828f916411d7a95 。您可以使用 Python、webView、HTML5 和 JavaScript
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
我遇到了类似的问题,最终通过用 Java 编写一个小型单例类来解决它,该类在使用 java.lang.ProcessBuilder 创建的子进程中运行 SL4A 安装中的 Python-4-Android 二进制文件。所以我根本没有使用 SL4A 机制(触发器、上行调用等),只是借用了 Python。
这看起来比尝试通过 SL4A 启动并连接到 Python 进程更干净。
这是 Python 2.7.1,来自 Mac OS X Snow Leopard 的交叉编译扩展。我的 Python 模块仅执行文本输入和输出、接受套接字连接等。不与 Android API 交互。一切正常:将 Java 流写入 Python 输入并读取 Java 流以获取 Python 输出。 C 扩展是使用 P4A 指令 构建的。 (Android 无法找到 .so dynalibs,直到我
在构建期间添加到 setup.cfg 为止。我认为这是因为 Android 上从未调用安装“install”。我只是使用 adb 进行推送。
所有活动生命周期状态似乎正在工作,但我还无法确定当主进程处于停止状态时子进程是否自动暂停,
如果感兴趣的话我可以在几周内发布代码。
我的包装计划是把/assets 中的 Python 代码的 ZIP 存档,并在第一次 onCreate 期间解压应用程序。我还没有实现这一点,但我预计不会出现任何问题。
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
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.