15 ms·
Python for Android
- tyingq 10y agoHow big are the generated APKs compared to a normal Android app of similar functionality?
- Zuider 10y agoIt looks like a 'Hello World' will result in about a 10 Mib APK which takes up roughly double that space when unpacked on the mobile device. This is not surprising because the APK must contain the Kivy library and a Python interpreter. After that initial hurdle it will scale relatively well, though that can depend on which extensions and other files you include.
- Humphrey 10y agoI'm assuming that 10MB would be less if you didn't want to use Kivy. However, I assume that using Django would probably take even more space that Kivy. So could it get big fast?
- Zuider 10y agoPython for Android is an extension for Kivy, so it is not optional. It looks like apps could get quite big since adding functionality involves including Python libraries to be called from Python rather than interacting with the Android API.
- inclemnet 10y agoKivy actually is completely optional, python-for-android doesn't require any particular frontend. The main supported options for guis other than Kivy are SDL2 (you can use e.g. PySDL2) or a webview, but it's designed to be easy to add more options.
- tyingq 10y agoThat's not too bad. I see plenty of apps installed on my phone that are 2 to 6x that size, and a few even larger. Appreciate the info.
- deleted 10y ago[deleted]
- freakboy3742 10y agoIf you want a smaller target, VOC (http://pybee.org/voc http://pybee.org/voc) can generate a "Hello World" APK in about 1.6MB. VOC is compiler that takes Python code, and output Java bytecode. This means you can use the Android native UI components, because your Python code becomes indistinguishable from code written in Java. Combine with Briefcase (http://pybee.org/briefcase http://pybee.org/briefcase), and you can get an Android app with one command; combine with Toga (http://pybee.org/toga http://pybee.org/toga), and you can get a cross platform app using native system components. If you want to see this in action, here's a video: https://www.youtube.com/watch?v=RisCgSIWwLA https://www.youtube.com/watch?v=RisCgSIWwLA (Full disclosure - I'm the primary author of these tools)
- txprog 10y agoA minimal hello world with Python 3 required 7MB. But it's not optimized. Unlike pyinstaller, we don't scan import to try reducing the distribution on the phone. Instead we have a blacklist.txt which you can put file pattern to exclude during packaging. I remember for one app in Python 2 to reduce the size around the 5MB. We won't be able to reduce much more, as Python interpreter is embedded. We also don't want to adopt an approach like Qt where Qt binaries can be shared for all Qt-application, which break the all-in-one APK, open some potential issues, and add more work to manage dependencies.
- voltagex_ 10y agoFrom the issue tracker, it looks like the project could use some help - anyone want to hop on and triage?
- benwilber0 10y agoyou're someone
- fencepost 10y agoCan't do a lot of digging on this right now(phone), but for just running Python of various versions take a look at termux, QPython and SL4A. Some may now be defunct, but searches on those should guide you to current info. It this is instead a way to package and distribute compiled Python it may be interesting but I'm not sure Android is the best target.
- dredmorbius 10y agoSeconding this. 1. If you haven't already discovered Termux, do. It's the one thing on Android that doesn't manifestly suck. 2. I don't mess much with Python, but Termux + python + pip allowed me to install youtube-dl and be able to download and play videos (from numerous platforms) on-device rather than streamed-only.
- analog31 10y agoI followed the instructions at this site to install Jupyter/Python including numpy and matplotlib, on my Android phone: leouieda.com/blog/scipy-on-android.html It worked as advertised. Granted, it was a symbolic gesture, since I can't imagine doing any real work on my phone, but it answers the question of whether I could live with an Android tablet as a laptop replacement. My definition du jour of a "real computer" is anything that can run Jupyter. ;-) Being kind of ignorant of the inner workings of this stuff, I was quite impressed.
- AnOscelot 10y agoI can tell you from experience Termux works mostly very well on Chromebooks which come with the Play Store. Gives a nice option to putting the device in developer mode and installing Crouton. The one main problem involves shortcut keys. Chrome OS intercepts certain key presses, such as "CTRL N" or "CTRL T", and routes them to the browser. Makes emacs / vim a little annoying, but a Chrome OS dev assured me this was on the "to fix" list.
- dredmorbius 10y ago
- danso 10y agoOT but this made me think of RubyMotion, which I remember being wildly popular (among Rubyists) back in the day but don't hear much about it now [0]. How many people still use it? [0] https://hn.algolia.com/?query=rubymotion&sort=byPopularity&prefix=false&page=0&dateRange=pastYear&type=story https://hn.algolia.com/?query=rubymotion&sort=byPopularity&p...
- AtheistOfFail 10y agoRubyMotion was amazing but what it had in ability was superseeded by React Native with everyone and their mother doing a React + Rails combo for websites now. In the end, writing plain Ruby became more cumbersome than writing React so people just abandoned the platform. The price tag of React Native didn't help the issue either (free) along with it just being a npm install away.
- holydude 10y agoI do not thin react native superseeded RM by being a better product. RM is super polished but failed at popularizing itself hence losing a very big chunk of the market. RN on the other hand crushed their competitors by its huge popularity, number of articles, documentation and tutorials available.
- natermer 10y ago> RN on the other hand crushed their competitors by its huge popularity, number of articles, documentation and tutorials available. That's means it's a better product. You can have the best code in the world, but nobody is going to give a damn if they don't know how to use it.
- kristianp 10y agoYou've not heard of "worse is better" [1] then. Many times in the past the worse product has succeeded over the better product. [1] https://www.dreamsongs.com/WorseIsBetter.html https://www.dreamsongs.com/WorseIsBetter.html
- Humphrey 10y agoFirstly, Looks like it's been around for a couple of years. Is the idea that you could deploy your python code as an android app. I'd like to see some examples apps in the wild, so I could check out performance, etc. But could be cool. I can imagine redeploying my web-app with this, and then doing some funky database syncing with the cloud.
- guelo 10y agoFrom a very cursory look this doesn't seem to hook into the android framework at all including the native UI or other system goodies like notifications and locations, etc. The apps would run in their own little world with their own provided non-native UI.
- lovelearning 10y agoThis seems to be just one of the components of a larger set spread across multiple repos. [1] seems to be the component to access native APIs. [1]: https://github.com/kivy/plyer https://github.com/kivy/plyer
- freakboy3742 10y agoAFAIK, Plyer will give you access to native APIs like accelerometers, but not to the native GUI layer. An alternative approach is VOC (http://pybee.org/voc http://pybee.org/voc) - this is compiler that takes Python code, and output Java bytecode. This means you can use the Android native UI components, because your Python code becomes indistinguishable from code written in Java. Combine with Briefcase (http://pybee.org/briefcase http://pybee.org/briefcase), and you can get an Android app with one command; combine with Toga (http://pybee.org/toga http://pybee.org/toga), and you can get a cross platform app using native system components. If you want to see this in action, here's a video: https://www.youtube.com/watch?v=RisCgSIWwLA https://www.youtube.com/watch?v=RisCgSIWwLA (Full disclosure - I'm the primary author of these tools)
- raonyguimaraes 10y agoHey Freakboy, always good to see your coding projects. Keep up the good work!
- rtpg 10y agoThis project(Kivy) doesn't use the native Android UI components, so it's a bit of a negative. You don't get any of the stuff like accessibility. So I remember hearing that a difficulty with hooking Python to Android is that there's some sort of limit to the amount of Java methods you can access through the JNI, but I could never find a proper explanation of this. If I just had a version of CPython compiled on Android, given Java's nature of running on a virtual machine, wouldn't it be possible to dynamically load arbitrary classes through the JNI and use the native Android stuff? What are the primary difficulties to this, if you ignore performance?
- freakboy3742 10y agoThe primary difficulty is that JNI is really limited on Android. It exists, but it's really slow, and there are some hard limits imposed at the kernel level. An alternative approach is VOC (http://pybee.org/voc http://pybee.org/voc) - this is compiler that takes Python code, and output Java bytecode. This means you can use the Android native UI components, because your Python code becomes indistinguishable from code written in Java. Combine with Briefcase (http://pybee.org/briefcase http://pybee.org/briefcase), and you can get an Android app with one command; combine with Toga (http://pybee.org/toga http://pybee.org/toga), and you can get a cross platform app using native system components. If you want to see this in action, here's a video: https://www.youtube.com/watch?v=RisCgSIWwLA https://www.youtube.com/watch?v=RisCgSIWwLA (Full disclosure - I'm the primary author of these tools)
- rtpg 10y agoAh, I remember watching the PyCon talk on VOC, a lot of cool work. No disrespect ot the work, but I'm always a bit worried about a thing that starts with "we're swapping out the Python interpreter for a compiler". For example, this doesn't include generator support, which is a big part of my Python usage. "Just" having the CPython bridge is so tempting, because your python code will work (modulo C extensions), plus we get all the performance work too. I believe you tried this, of course. For VOC, is there a guide to calling Java code from Python code? I see you have Toga, but being able to write my own abstractions without much work would be neat.
- agumonkey 10y agoI used Qpython for a little while. I think it's based on kivy.
- txprog 10y agoPlease note that Python for Android project is not meant to be used with Kivy only anymore. It's a project that allow you to compile Python and dependencies, and package it along your own application. You could for example compile Numpy, ZMQ, sqlalchemy, zeroconf, etc... We have a mechanism called bootstrap that allow you to select your primary technology that you're going to use for your application, such as "sdl2" (required for kivy), "webview" (it just start a webpage that connect to your phone port 5000, so you can start an embed webserver such as Flask to serve your app), "service_only" (if you don't need any kind of UI). New bootstrap can be written to support another type of apps. Also, you could embed your whole UI in pure java if you wanted too, as the directory libs (to include .jar) or src (to include .java) are compiled. So you can imagine having your app in Java, but still call Python for something else if you really wanted too.
- JCharante 10y agoAmazing.
- txprog 10y agoJust an addition, there is many others alternatives, but it may be hard for others to be able to compile the Python extensions you want (or impossible). That's what we wanted to do here: ability to pick the extension needed, provide a "recipe" that contain specialized tricks or patches to extension for running in Android, etc.
- on_and_off 10y agoSuggestion : could you add a section on the github page explaining what is the use case is for this ?
- thearn4 10y ago> So you can imagine having your app in Java, but still call Python for something else if you really wanted too. That's a pretty interesting possibility, I might tinker with this. I don't mind Java, but vastly prefer Python+numpy+scipy for technical computing and algorithmic experimentation. Java for the GUI/platform specific stuff with the creative stuff in Python works well for some side projects that I've done. In those cases, I interfaced them via REST API to a server, then later translated to pure Java.
- gear54rus 10y agoI wonder if this allows running things like mitmproxy on your phone. Or maybe someone is aware of a good alternative?
- drvdevd 10y agoI've actually run mitmproxy on my phone in a chroot before, somehow (several years ago now). However, I can't remember if it was a rooted phone or not, sorry. Suffice to say it's perfectly possible, most phones are plenty powerful enough. [EDIT]: if it's not rooted, I know you can still use it with a large portion of mobile traffic by adding the trusted cert it generates and then setting it up as a network proxy through standard settings changes (ie: no iptables transparent proxy necessary). I believe some apps will detect this.
- gear54rus 10y agoYeah no problem if root is required, I just wanna know what did you use as a layer in between it and android. Maybe there's a relevant tutorial I could follow?
- jventura 10y agoSome years ago I tried to use python-for-android, but found it too much of a burden to do what I wanted, which was just to simply integrate existing python code in native android apps (with Java). Don't know how much has changed since then, but python-for-android's bootstrap used pygame and pysdl to start an opengl scene, where you would have completely non-native widgets. I rolled my own solution [0] which uses crystax distribution's of CPython for android (Python 3.5), and I did a very simple JNI bridge between java and cpython using an RPC-like mechanism over json. Basically, on the java side you call a function and pass the arguments [1], and on the python side you "route" it to your own functions [2]. I think all pybridge's code is very very simple for those who understand a bit of Java, a bit of C and a bit of Python, and I explain in the readme how everything works, including which classes/files are relevant. I have an app where I use this [3], and although it hasn't earn me much money so far (~20 downloads, 110€), it is rated 4.7 in the play store, so the performance of accessing python functions on android is not bad at all.. [0] https://github.com/joaoventura/pybridge https://github.com/joaoventura/pybridge [1] https://goo.gl/sw4JLt https://goo.gl/sw4JLt [2] https://goo.gl/17SlVb https://goo.gl/17SlVb [3] https://play.google.com/store/apps/details?id=com.flatangle.charts https://play.google.com/store/apps/details?id=com.flatangle....
- inclemnet 10y agoWe've actually completely rewritten python-for-android since then, it now supports multiple bootstraps and is not tied to Kivy at all, and it should be relatively easy to use the python distribution in a native app (although this could be streamlined). CrystaX's support is great though, and actually python-for-android uses this for Python 3 builds.
- on_and_off 10y agoI looked at the app, as far as I can tell, it seems fairly basic. Why use python for this ?
- jventura 10y agoBecause I already had a lot of code doing a lot of things in Python (https://github.com/flatangle/flatlib/ https://github.com/flatangle/flatlib/)..
- mixedCase 10y agoCongrats to the whole team on the release!
- criddell 10y agoI remember hearing about the Nokia phone with Python: http://www.artima.com/weblogs/viewpost.jsp?thread=148064 http://www.artima.com/weblogs/viewpost.jsp?thread=148064 I never got to use one, but apparently there were APIs for just about every part of the phone that could be scripted with Python.
- stuaxo 10y agoYup, you could run it on any S60 phone, and even access stuff like 3D via OpenGL ES.
- mrcactu5 10y agocan Python now be used instead of Java? for example Go language, I heard is not a total substitute for Java for Android
- tfm 10y agoYes, but with the caveat that you'll be running interpreted .py using a cross-compiled Python executable, rather than Python compiled to Android bytecode. Kivy itself (the project that spawned p4a) is a full cross-platform library for UI development; for any heavy computational lifting you'd want to farm it out to a C API library and import it the usual way, modulo some p4a recipe incantations.
- no_wizard 10y agoI know this is about Python for Android. I just wanted to say for all those iOS users who might read this and think "well this is of interest" you might want to check out Pythonista http://omz-software.com/pythonista/ http://omz-software.com/pythonista/ its not quite the same, but you can build apps (sorta, i guess?) right on iOS, you can run python, and with a package like stash you can even use pip! https://github.com/ywangd/stash https://github.com/ywangd/stash this is cool for android though!