3

First, I am sorry if this is a duplicate question but I have spent many hours before writing here looking for an answer and could not find any (that would work at least).

I want to (ideally) compile a python (3.6/7) script into an android app. I need to use external libraries in my code, namely matplotlib and numpy.

I have Kivy Launcher in my android device and can launch normal ("pure") Kivy scripts no problem, and they work fine. My code including matplotlib (see below), however, immediately crashes as I believe the module is not included into the Kivy Launcher.

As for trying to compile an apk, I have downloaded the VM from https://kivy.org/#download with a preinstalled and pre-configured Buildozer + python-for-android. Using it I could create .apk files which would install and run fine as long as they did not include external modules. I tried however to set matplotlib as one of the requirements in the buildozer.spec, and the compilation fails with a:

Command failed: ./distribute.sh -m "kivy matplotlib" -d "myapp"

At this point, I can run my code using matplotlib as a script in the Pydroid 3 app, where I could install my modules normally through pip (kivy, matplotlib, etc.). As for the kivy-matplotlib interface, I am using the matplotlib flower in the kivy-garden module which I just packaged locally with my code by running

garden install --app matplotlib

inside the script directory before exporting my code to the android device.

Is there any way to run this as a native app or at the very least through the Kivy Launcher? I have read that so far buildozer does not support matplotlib, but it was from an un-referenced comment over half a year old. I am also fine if I can get it a .apk running matplotlib by tinkering with python-for-android directly, though I don't know how that would work.

Lastly, I tried to download both numpy and matplotlib source code and add the modules locally to my package so buildozer would not need to download/care about them, but I don't know how that is done for large, complex modules like these (if it is even possible).

This is the sample code that I would like to get a .apk for (copied from How to get started/use matplotlib in kivy):

from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import matplotlib.pyplot as plt

plt.plot([1, 23, 2, 4])
plt.ylabel('some numbers')

class MyApp(App):

    def build(self):
        box = BoxLayout()
        box.add_widget(FigureCanvasKivyAgg(plt.gcf()))
        return box

MyApp().run()
Arce11
  • 51
  • 1
  • 8
  • The vm is very outdated, and we need to remove it from the website, until someone takes the time to produce an updated version, it's based on an ubuntu version you can't update, and that causes a lot of issues. Thankfuly, installing buildozer from scratch on the latest ubuntu is not too hard, you can pip install it, then run it and install the various missing packages as they come. The only trick is for aidl, for which you need to run the android utility from the sdk and let it update everything. The catch though, is that there is no matplotlib recipe for p4a, so it'll probably not work out. – Tshirtman Feb 10 '19 at 10:05
  • @Tshirtman Thank you for your answer! I suppose adding a matplotlib recipe is way more complex than anything I can do right now, so I'll stick to running my programs as scripts for the time being. Do you know if there are any short to mid-term plans on including a matplotlib recipe? – Arce11 Feb 10 '19 at 12:17
  • I don't know of any plan currently, contributions for it are welcome, but i don't think anyone is working on it, if you want to try, you can expect some level of support from the knowledgeable people (i'm less involved in p4a myself), but someone will have to care enough to do the work. – Tshirtman Feb 11 '19 at 11:28
  • For anyone interested in building your own p4a recipes there is some documentation provided by Alexander Taylor in the new p4a documentation from June this year: https://readthedocs.org/projects/python-for-android/downloads/pdf/latest/ – Liam Aug 31 '20 at 12:39

0 Answers0