
Python for Android - gregorymichael
https://github.com/kivy/python-for-android
======
txprog
Please 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.

~~~
txprog
Just 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.

------
jventura
Some 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....](https://play.google.com/store/apps/details?id=com.flatangle.charts)

~~~
on_and_off
I looked at the app, as far as I can tell, it seems fairly basic.

Why use python for this ?

~~~
jventura
Because I already had a lot of code doing a lot of things in Python
([https://github.com/flatangle/flatlib/](https://github.com/flatangle/flatlib/))..

------
fencepost
Can'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
Seconding 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
I 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
I 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
No such keyboard issues on my tablet, FWIW.

------
danso
OT 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&p...](https://hn.algolia.com/?query=rubymotion&sort=byPopularity&prefix=false&page=0&dateRange=pastYear&type=story)

~~~
AtheistOfFail
RubyMotion 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
I 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
> 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
You'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)

~~~
AtheistOfFail
Because the people who implement the technology aren't those who can admire
the "quality". If it's been 6 hours at work and I have 30 minutes to pick a
tech to start evaluating, I'm picking the one with better documentation/easier
curve. Most people aren't a Facebook and don't need the best greatest thing in
the world, just something to fix the current problem. You can kick the can
down the road.

------
guelo
From 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
This 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
AFAIK, 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
Hey Freakboy, always good to see your coding projects. Keep up the good work!

------
rtpg
This 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
The 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)

~~~
pjmlp
I always get the feeling that if the Android team got to choose the NDK would
never been a thing, rather it was an upper management decision that they had
to offer some kind of support for C and C++.

Even nowadays with Android N, the NDK is kind of WIP.

~~~
TuringNYC
We did Computer Vision work on Android before OpenCV ported to Android and the
NDK was the only realistic way to achieve any real-time video/image analysis
performance. Using the NDK was tedious and HR-expensive but we had no choice.
So in that case, we were glad the NDK was an option.

~~~
pjmlp
I am also glad it is there, it is also where I spend most of my Android coding
time.

My point is that the way Android team deals with NDK users seems like the NDK
was imposed on the team, and they don't spend much developer time on it,
looking from the set of open issues, outdated documentation where ndk-build is
still used instead of cmake, integration with aar is still in progress, and
set of available APIs.

[https://code.google.com/p/android/issues/list?can=2&q=NDK&so...](https://code.google.com/p/android/issues/list?can=2&q=NDK&sort=-opened&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened)

I cannot deny they have done lots of progress, but there seems not to be much
interest in having a comparable experience to the Java tooling.

------
Humphrey
Firstly, 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.

------
criddell
I 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
Yup, you could run it on any S60 phone, and even access stuff like 3D via
OpenGL ES.

------
tyingq
How big are the generated APKs compared to a normal Android app of similar
functionality?

~~~
Zuider
It 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.

~~~
tyingq
That'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.

------
no_wizard
I 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!

------
voltagex_
From the issue tracker, it looks like the project could use some help - anyone
want to hop on and triage?

~~~
benwilber0
you're someone

------
gear54rus
I wonder if this allows running things like mitmproxy on your phone. Or maybe
someone is aware of a good alternative?

~~~
drvdevd
I'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
Yeah 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?

------
mixedCase
Congrats to the whole team on the release!

------
mrcactu5
can Python now be used instead of Java?

for example Go language, I heard is not a total substitute for Java for
Android

~~~
tfm
Yes, 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.

------
agumonkey
I used Qpython for a little while. I think it's based on kivy.

