Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue Compiling Matplotlib for Android (Buildozer) #3120

Open
clarsendartois opened this issue Mar 6, 2025 · 2 comments
Open

Issue Compiling Matplotlib for Android (Buildozer) #3120

clarsendartois opened this issue Mar 6, 2025 · 2 comments
Labels

Comments

@clarsendartois
Copy link

📝 Subject: Issue Compiling Matplotlib for Android (Buildozer)

Hey everyone, I'm trying to package my Kivy/KivyMD app for Android using Buildozer,
but it's failing when trying to compile Matplotlib from source.


📌 Issue

Buildozer gets stuck at:

[INFO]:    Building matplotlib for arm64-v8a
[INFO]:    matplotlib apparently isn't already in site-packages
[INFO]:    Building compiled components in matplotlib
[INFO]:    -> directory context /mnt/c/Users/darto/Documents/.buildozer/android/platform/build-arm64-v8a_armeabi-v7a/build/other_builds/matplotlib/arm64-v8a__ndk_target_21/matplotlib 
[DEBUG]:   -> running python3 setup.py build_ext -v

🛠 What I’ve Tried

1️⃣ Added matplotlib to requirements in buildozer.spec.
2️⃣ Checked dependencies (e.g., numpy, cycler).
3️⃣ Looked for a precompiled Matplotlib .whl but couldn’t find one.


❓ Questions

1️⃣ Is there a prebuilt Matplotlib wheel for Android that I can use instead of compiling?
2️⃣ If Matplotlib needs to be compiled, what dependencies do I need to include?
3️⃣ Any known workarounds to avoid this issue?


🙏 Any help would be greatly appreciated! Thanks in advance! 😊

@mriscoc
Copy link
Contributor

mriscoc commented Mar 7, 2025

I tried this minimal example:

"""Basic example for kivy_matplotlib_widget

Note:
    MatplotFigure is used when you have only 1 axis with lines only.
    For general purpose, please use MatplotFigureSubplot widget.

"""
from kivy.utils import platform

#avoid conflict between mouse provider and touch (very important with touch device)
#no need for android platform
if platform != 'android':
    from kivy.config import Config
    Config.set('input', 'mouse', 'mouse,disable_on_activity')

from kivy.lang import Builder
from kivy.app import App
import matplotlib.pyplot as plt
import kivy_matplotlib_widget #register all widgets to kivy register
import numpy as np

KV = '''
Screen
    figure_wgt:figure_wgt
    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            size_hint_y:0.1
            Button:
                text:"home"
                on_release:app.home()
            ToggleButton:
                group:'touch_mode'
                state:'down'
                text:"pan" 
                on_release:
                    app.set_touch_mode('pan')
                    self.state='down'
            ToggleButton:
                group:'touch_mode'
                text:"zoom box"  
                on_release:
                    app.set_touch_mode('zoombox')
                    self.state='down'                
        MatplotFigure:
            id:figure_wgt
'''

plt.style.use('dark_background')

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

class Test(App):

    def build(self):  
        self.screen=Builder.load_string(KV)

        # Setup plot
        self.fig, self.ax1 = plt.subplots()
        self.ax1.set(xlabel='time (s)', ylabel='voltage (mV)', title='Test Matplot in Kivy')
        self.ax1.grid(alpha=0.5)

        return self.screen

    def on_start(self, *args):
        self.ax1.plot(t, s)
        self.screen.figure_wgt.figure = self.fig

    def set_touch_mode(self,mode):
        self.screen.figure_wgt.touch_mode=mode

    def home(self):
        self.screen.figure_wgt.home()
        
Test().run()

And compiles without problems with the develop branch of P4A and buildozer 1.5.1.dev0. buildozer.spec:

requirements = python3,kivy,numpy,matplotlib,kivy-matplotlib-widget
android.minapi = 24
android.archs = arm64-v8a
p4a.branch = develop
log_level = 2

Final log:

[INFO]:         matplottest: min API 24, includes recipes (freetype, hostpython3, jpeg, libffi, openssl, png, sdl2_image, sdl2_mixer, sdl2_ttf, sqlite3, python3, sdl2, kiwisolver, numpy, setuptools, pillow, six, matplotlib, pyjnius, android, kivy, cppy, python-dateutil, packaging, filetype, idna, chardet, fonttools, urllib3, requests, pyparsing, certifi, kivy-matplotlib-widget, cycler), built for archs (arm64-v8a)
[INFO]:    matplottest has compatible recipes, using this one
[INFO]:    # Copying android package to current directory
[INFO]:    # Android package filename not found in build output. Guessing...
[INFO]:    # Found android package file: /home/mrisco/kivy/kivy-matplottest/.buildozer/android/platform/build-arm64-v8a/dists/matplottest/build/outputs/apk/debug/matplottest-debug.apk
[INFO]:    # Add version number to android package
[INFO]:    # Android package renamed to matplottest-debug-0.1.apk
[DEBUG]:   -> running cp /home/mrisco/kivy/kivy-matplottest/.buildozer/android/platform/build-arm64-v8a/dists/matplottest/build/outputs/apk/debug/matplottest-debug.apk matplottest-debug-0.1.apk
No setup.py/pyproject.toml used, copying full private data into .apk.
Applying Java source code patches...
Applying patch: src/patches/SDLActivity.java.patch
# Copy /home/mrisco/kivy/kivy-matplottest/.buildozer/android/platform/build-arm64-v8a/dists/matplottest/build/outputs/apk/debug/matplottest-debug.apk to /home/mrisco/kivy/kivy-matplottest/bin/matplottest-0.1-arm64-v8a-debug.apk
# Android packaging done!
# APK matplottest-0.1-arm64-v8a-debug.apk available in the bin directory

@kuzeyron kuzeyron added the recipe label Mar 7, 2025
@clarsendartois
Copy link
Author

Hello, @mriscoc

Thanks for Testing This! 🎉

It looks like your build works fine with the develop branch of P4A and Buildozer 1.5.1.dev0.
I'm currently using:

  • Buildozer version: (1.5.1.dev0)
  • P4A branch: (develop)
  • Android API level: (android.minapi = 24)

I'll try switching to the develop branch and updating Buildozer to 1.5.1.dev0 to see if that resolves the issue.

Question:

Did you need to manually install or modify anything else in your environment, or did it work out of the box after setting p4a.branch = develop?

Thanks again for your help! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants