Issues with building Kivy APK with buildozer - android

I wrote a simple Kivy app on Mac OsX that replicates buttons:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class ButtonApp(App):
def add_button(self, none):
button = Button(text='Press Me too!')
button.bind(on_press=self.add_button)
self.b.add_widget(button)
def build(self):
self.b = BoxLayout()
button = Button(text="Press me!")
button.bind(on_press=self.add_button)
self.b.add_widget(button)
return self.b
if __name__ == "__main__":
ButtonApp().run()
However, when I initiate the buildozer file with buildozer init, and run it with buildozer android debug I get the following error
self.target.build_package()
File "/usr/local/lib/python2.7/site-packages/buildozer/targets/android.py", line 517, in build_package
version = self.buildozer.get_version()
File "/usr/local/lib/python2.7/site-packages/buildozer/__init__.py", line 675, in get_version
' (looking for `{1}`)'.format(fn, regex))
Exception: Unable to find capture version in ./main.py
(looking for `__version__ = ['"](.*)['"]`)
I've tried many solutions including switching the locations of the file, changing the version, adding __version__ = "1.2.0" to main.py file, and all with no success. What am I doing wrong?

Buildozer works only on linux for the android package, this error is because you were on OSX

Related

Python Kivy Android

Recently I have created a dummy android app using Python2 and Kivy. Binded it using buildozer but when I installed it on my android device everything going good so far but app is not loading the screen. It opens and instantly closed it, and no error(s) displayed :(
I am using kivy 1.11.0 and buildozer version is 0.34. Application is working before binding it to .apk
Python Code
import kivy
kivy.require('1.11.0')
#from kivy.core.window._window_sdl2 import _WindowSDL2Storage
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
class screen1(GridLayout):
def __init__(self, **kwargs):
super(screen1, self).__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text="hello world!"))
self.add_widget(Label(text="bye world!"))
class SampleKivy(App):
def build(self):
return screen1()
if __name__ == "__main__":
SampleKivy().run()

Python Kivy - App that open url in a native web browser

I try to make a simple app that open a web page inside Kivy after clicking a button placed on a "Screen One".
I used this topic (Python - Showing a web browser/iframe right into the app) as reference but I didn't understand how to use the code provided by Michael...
So I tried this... and when I launch the apk (build with Buildozer) it didn't work :')
import kivy
kivy.require('1.9.2')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
# MICHAEL'S CODE
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.android.PythonActivity').mActivity
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)
#run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('http://www.google.com/')
# END OF MICHAEL'S CODE
Builder.load_string('''
<ScreenOne>:
BoxLayout:
Label:
text: "SCREEN 1"
Button:
text: "GO GO GO TO GOOGLE !"
on_press: root.open_browser()
<ScreenTwo>:
BoxLayout:
Label:
text: "SCREEN 2"
Button:
text: "GO GO GO TO SCREEN 1"
on_press:
root.manager.transition.direction = "right"
root.manager.transition.duration = 1
root.manager.current = "screen_one"
''')
class ScreenOne(Screen):
def open_browser(self):
return Wv()
class ScreenTwo(Screen):
pass
screen_manager = ScreenManager()
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
class BrowserApp(App):
def build(self):
return screen_manager
app = BrowserApp()
app.run()
The app don't crash but just close when I start it.
What I'm doing wrong ? I'm sure that I don't use it the right way...
Log from adb logcat:
06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android
06-13 12:54:47.579 7429 7510 I python : Python for android ended.
From the log you posted in the comments I extracted the two important lines:
06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android
06-13 12:54:47.579 7429 7510 I python : Python for android ended.
This basically means that the copied code:
from android.runnable import run_on_ui_thread
won't work because it doesn't detect the android module. The module has a separate recipe, therefore you will need to add it to the requirements, so that it compiles the Cython code and adds it to your app, otherwise the import will always fail.
Basically you always want to search for 3-4 keywords when looking into such a messy logcat → "python", "Traceback", "Python for android", "kivy". There's a filter in buildozer for that if you use it:
android.logcat_filters = *:S python:D

How to save acceleration data in a file on my android device using python kivy?

I used jsonstorage and pickle but it did not work. Maybe I am doing it the wrong way? Activity appears and then disappears. The log says handler error. In the update() function value is storing to txt variable. I want to store this txt in to an external file on my mobile phone.
My code:
__version__ = '1.0' # declare the app version. Will be used by buildozer
from kivy.app import App # for the main app
from kivy.uix.floatlayout import FloatLayout # the UI layout
from kivy.uix.label import Label # a label to show information
from plyer import accelerometer # object to read the accelerometer
from kivy.clock import Clock # clock to schedule a method
class UI(FloatLayout): # the app ui
def __init__(self, **kwargs):
super(UI, self).__init__(**kwargs)
self.lblAcce = Label(text="Accelerometer: ") # create a label at the center
self.add_widget(self.lblAcce) # add the label at the screen
try:
accelerometer.enable() # enable the accelerometer
# if you want do disable it, just run: accelerometer.disable()
Clock.schedule_interval(self.update, 2.0/1) # 24 calls per second
except:
self.lblAcce.text = "Failed to start accelerometer" # error
def update(self, dt):
txt = ""
try:
txt = "Accelerometer:\nX = %.2f\nY = %.2f\nZ = %2.f " %(
accelerometer.acceleration[0], # read the X value
accelerometer.acceleration[1], # Y
accelerometer.acceleration[2]) # Z
except:
txt = "Cannot read accelerometer!" # error
self.lblAcce.text = txt # add the correct text
class Accelerometer(App): # our app
def build(self):
ui = UI() # create the UI
return ui # show it
if __name__ == '__main__':
Accelerometer().run() # start our app
Just use json store.it will surely work.
here is the code :
Add import jsonstore
Jsonstore=(/storage/emulated/0/hello.json);
json.put(txt)

android file not working with numpy recipe

I am using example from Kivy documentation, only line i added is import numpy. After packaging it for android i cant run on my phone it never starts. However on removing this line and then building works fine.
python file: main.py
__version__="1.0.0"
import colorsys
import numpy as np
import kivy
kivy.require('1.0.7')
from kivy.animation import Animation
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def animate(self, instance):
animation = Animation(pos=(100, 100), t='out_bounce')
animation += Animation(pos=(200, 100), t='out_bounce')
animation &= Animation(size=(500, 500))
animation += Animation(size=(100, 50))
animation.start(instance)
def build(self):
button = Button(size_hint=(None, None), text='plop',
on_press=self.animate)
return button
if __name__ == '__main__':
TestApp().run()
In buildozer.spec file under requirement i have included kivy, numpy.
Check the log to see what's wrong.

"ImportError: Cannot import name spawnu" while building apk using kivy

I was building a simple android app using kivy using the below code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.uix.floatlayout import FloatLayout
class TutorialApp(App):
def build(self):
f = FloatLayout()
s = Scatter()
l = Label(text="Hello!",
font_size=150)
f.add_widget(s)
s.add_widget(l)
return f
if __name__ == "__main__":
TutorialApp().run()
I am building this code into an apk using buildozer.
I wrote
buildozer android debug
This gave the following error:
http://imgur.com/z9TJMhC
How can I rectify this error.
It seems that python or the kivy compiler is unable to import a package named spawnu.Spawnu is part of pexpect package. Try
sudo pip install pexpect
sudo pip install pexpect --upgrade
Worked for me. Good Luck!!

Categories

Resources