有没有一种在 Android 上运行 Python 的方法？ [关闭]
We are working on an S60 version and this platform has a nice Python API.
However, there is nothing official about Python on Android, but since Jython exists, is there a way to let the snake and the robot work together?
- 点赞 30 评论 复制链接分享
An example via Matt Cutts via SL4A -- "here’s a barcode scanner written in six lines of Python code:
import android droid = android.Android() code = droid.scanBarcode() isbn = int(code['result']['SCAN_RESULT']) url = "http://books.google.com?q=%d" % isbn droid.startActivity('android.intent.action.VIEW', url)点赞 26 评论 复制链接分享
There is also the new Android Scripting Environment (ASE/SL4A) project. It looks awesome, and it has some integration with native Android components.
Note: no longer under "active development", but some forks may be.点赞 23 评论 复制链接分享
Yet another attempt: https://code.google.com/p/android-python27/
This one embed directly the Python interpretter in your app apk.点赞 10 评论 复制链接分享
I've posted instructions and a patch for cross compiling Python 2.7.2 for Android, you can get it at my blog here: http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/
EDIT: I've open sourced Ignifuga, my 2D Game Engine, it's Python/SDL based and it cross compiles for Android. Even if you don't use it for games, you might get useful ideas from the code and the builder utility (named Schafer, after Tim...you know who).点赞 10 评论 复制链接分享
Using SL4A (which has already been mentioned by itself in other answers) you can run a full-blown web2py instance (other python web frameworks are likely candidates as well). SL4A doesn't allow you to do native UI components (buttons, scroll bars, and the like), but it does support WebViews. A WebView is basically nothing more than a striped down web browser pointed at a fixed address. I believe the native Gmail app uses a WebView instead of going the regular widget route.
This route would have some interesting features:
点赞 10 评论 复制链接分享
- In the case of most python web frameworks, you could actually develop and test without using an android device or android emulator.
- Whatever Python code you end up writing for the phone could also be put on a public webserver with very little (if any) modification.
- You could take advantage of all of the crazy web stuff out there: query, HTML5, CSS3, etc.
You can use QPython:
It has a Python Console, Editor, as well as Package Management / Installers
It's an open source project with both Python 2 and Python 3 implementations. You can download the source and the Android .apk files directly from github.点赞 9 评论 复制链接分享
"The Pygame Subset for Android is a port of a subset of Pygame functionality to the Android platform. The goal of the project is to allow the creation of Android-specific games, and to ease the porting of games from PC-like platforms to Android."
The examples include a complete game packaged in an APK, which is pretty interesting.点赞 9 评论 复制链接分享
From the Python for android site:
Python for android is a project to create your own Python distribution including the modules you want, and create an apk including python, libs, and your application.点赞 9 评论 复制链接分享
I want to post this as an extension of what @JohnMudd has already answered (but please bear with me as English isn't my first language)
It has been years since then, and Kivy has evolved to v1.9-dev. The biggest selling point of Kivy, in my opinion, is its cross-platform compatibility. You can code and test under your local environment (Windows/*nix etc.), you can also build, debug and package your app to run on your Android/iOS/Mac/Windows devices.
With Kivy's own KV language, you can code and build the GUI interface easily (it's just like Java XML, but rather than TextView etc., KV has its own
ui.widgetsfor the similar translation), which is in my opinion quite easy to adopt.
Currently Buildozer and python-for-android are the most recommended tools to build/package your apps. I have tried them both and can firmly say that they make building Android apps with Python a breeze. Users who feel comfortable in their console/terminal/command prompt should have no problems using them, and their guides are well documented, too.
Furthermore, iOS is another big selling point of Kivy, provided that you can use the same code base with little changes required to test-run on your iOS device, via kivy-ios Homebrew tools, although Xcode is required for the build before running on their devices (AFAIK the iOS Simulator in Xcode currently doesn't work for the x86-architecture build). There are also some dependency issues which must be manually compiled and fiddled around with in Xcode to have a successful build, but they wouldn't be too difficult to resolve and people in Kivy Google Group are really helpful too.
With all being said, users with good Python knowledge should have no problem picking up the basics in weeks (if not days) to build some simple apps.
Also worth mentioning is that you can bundle (build recipes) your Python modules with the build so users can really make use of many existing libraries Python bring us, like Requests & PIL etc. through Kivy's extension support.
Sometimes your application requires functionality that is beyond the scope of what Kivy can deliver. In those cases, it is necessary to resort to external software libraries. Given the richness of the Python ecosystem, there is already a lot of software libraries that you can simply import and use right away.
The last but not the least, if you are going to use Kivy for more serious/commercial projects, you may find existing modules not satisfactory. There are some workable solutions though, with the "work in progress" of pyjnius for Android, and pyobjus. Users can now access Java/Objective-C classes through those modules to control some of the native APIs.
My experience in Kivy is that it will find its best fit with seasoned Python programmers and some serious programmers who want rapid development or simple code base maintenance. It runs well on multiple platforms, albeit not really with the native feeling.
I do hope some Python app programmers find this information useful and start taking a look at Kivy. It can only get better (with more support and as libraries/modules get ported) if there is great interest from the community.
P.S. I have no relationship with Kivy whatsoever, I'm merely a programmer who really likes the idea of bringing Python coding fun to mobile/cross-platform development.点赞 8 评论 复制链接分享
There's also python-on-a-chip possibly running mosync: google group点赞 7 评论 复制链接分享
Check out enaml-native which takes the react-native concept and applies it to python.
It lets users build apps with native Android widgets and provides APIs to use android and java libraries from python.
It also integrates with android-studio and shares a few of react's nice dev features like code reloading and remote debugging.点赞 7 评论 复制链接分享
Didn't see this posted here, but you can do it with Pyside and Qt now that Qt works on Android thanks to Necessitas.
It seems like quite a kludge at the moment but could be a viable route eventually...点赞 7 评论 复制链接分享
I use the QPython application. It has an editor, a console, and you can run your Python programs with it. The application is free, and the link is http://qpython.com/.点赞 6 评论 复制链接分享
Take a look at BeeWare. At the moment of answering this question it is still in early development. It's aim is to be able to create native apps with Python for all supported operating systems, including Android.点赞 6 评论 复制链接分享
It can automatically download PyPI packages and build them into an app, including selected native packages such as NumPy.
It enables full access to all Android APIs from Python, including the native user interface toolkit (example pure-Python activity).
This is a commercial product, but it's free for open-source use and will always remain that way.
(I am the creator of this product.)点赞 4 评论 复制链接分享
Not at the moment and you would be lucky to get Jython to work soon. If you're planning to start your development now you would be better off with just sticking to Java for now on.点赞 4 评论 复制链接分享
Another option if you are looking for 3.4.2 or 3.5.1 is this archive on GitHub.
It currently supports Python 3.4.2 or 3.5.1 and the 10d version of the NDK. It can also support 3.3 and 9c, 11c and 12
It's nice in that you simply download it, run make and you get the .so or the .a
I currently use this to run raw Python on android devices. With a couple modifications to the build files you can also make x86 and armeabi 64 bit点赞 2 评论 复制链接分享
One more option seems to be pyqtdeploy which citing the docs is:
a tool that, in conjunction with other tools provided with Qt, enables the deployment of PyQt4 and PyQt5 applications written with Python v2.7 or Python v3.3 or later. It supports deployment to desktop platforms (Linux, Windows and OS X) and to mobile platforms (iOS and Android).
According to Deploying PyQt5 application to Android via pyqtdeploy and Qt5 it is actively developed, although it is difficult to find examples of working Android apps or tutorial on how to cross-compile all the required libraries to Android. It is an interesting project to keep in mind though!点赞 1 评论 复制链接分享
As a Python lover and Android programmer, I am sad to say this is not really a good way to go. There are two problems.
One problem is that there is a lot more than just a programming language to the Android development tools. A lot of the Android graphics involve XML files to configure the display, similar to HTML. The built-in java objects are really integrated with this XML layout, and it's a lot easier than writing your own code to go from logic to bitmap.
The other problem is that the G1 (and probably other Android devices for the near future) are really not that fast. 200 MHz processors, and RAM is very limited. Even in Java you have to do a decent amount of rewriting-to-avoid-more-object-creation if you want to make your app perfectly smooth. Python is going to be too slow for a while still on mobile devices.点赞 1 评论 复制链接分享
Scripting Layer for Android
SL4A does what you want. You can easily install it directly onto your device from their site, and do not need root.
It supports a range of languages. Python is the most mature. By default, it uses Python 2.6, but there is a 3.2 port you can use instead. I have used that port for all kinds of things on a Galaxy S2 and it worked fine.
SL4A provides a port of their
androidlibrary for each supported language. The library provides an interface to the underlying Android API through a single
from android import Android droid = Android() droid.ttsSpeak('hello world') # example using the text to speech facade
let droid = new Android(); droid.ttsSpeak("hello from js");
For user interfaces, you have three options:
- You can easily use the generic, native dialogues and menus through the API. This is good for confirmation dialogues and other basic user inputs.
- You can also open a webview from inside a Python script, then use HTML5 for the user interface. When you use webviews from Python, you can pass messages back and forth, between the webview and the Python process that spawned it. The UI will not be native, but it is still a good option to have.
- There is some support for native Android user interfaces, but I am not sure how well it works; I just haven't ever used it.
You can mix options, so you can have a webview for the main interface, and still use native dialogues.
There is a third party project named QPython. It builds on SL4A, and throws in some other useful stuff.
QPython gives you a nicer UI to manage your installation, and includes a little, touchscreen code editor, a Python shell, and a PIP shell for package management. They also have a Python 3 port. Both versions are available from the Play Store, free of charge. QPython also bundles libraries from a bunch of Python on Android projects, including Kivy, so it is not just SL4A.
Note that QPython still develop their fork of SL4A (though, not much to be honest). The main SL4A project itself is pretty much dead.
点赞 1 评论 复制链接分享
- SL4A Project (now on GitHub): https://github.com/damonkohler/sl4a
- SL4A Python 3 Port: https://code.google.com/p/python-for-android/wiki/Python3
- QPython Project: http://qpython.com
There's also SL4A written in large by Google employees.点赞 1 评论 复制链接分享