· 5 years ago · Jul 13, 2020, 07:22 PM
1from kivymd.app import MDApp
2from kivy.lang import Builder
3from kivy.utils import platform
4from kivy.clock import mainthread
5
6
7if platform == 'android':
8 from jnius import autoclass, cast
9 from android.runnable import run_on_ui_thread
10 from android import activity
11
12 Intent = autoclass('android.content.Intent')
13 Activity = autoclass('android.app.Activity')
14 PythonActivity = autoclass('org.kivy.android.PythonActivity')
15 File = autoclass('java.io.File')
16 Paths = autoclass('java.nio.file.Paths')
17else:
18 def run_on_ui_thread(*args):
19 return
20
21KV = """
22#: import p kivy.utils.platform
23
24Screen:
25 name: 'first'
26 MDRaisedButton:
27 id: btn
28 center: root.center
29 pos_hint: {'center_y': .8}
30 text: 'open'
31 on_release:
32 app.get_picture() if p == 'android' else app.dummy()
33 MDRaisedButton:
34 text: 'share'
35 on_release: app.share_something() if p == 'android' else app.dummy()
36 pos_hint: {'center_y': .5}
37 center: root.center
38 MDLabel:
39 id: lbl
40 text: 'test'
41 halign: 'center'
42 vlign: 'middle'
43 pos_hint: {'center_y': .2}
44"""
45
46
47class GalleryApp(MDApp):
48
49 PICK_IMAGE = 1
50
51 def build(self):
52 if platform == 'android':
53 activity.bind(on_activity_result=self.activity_result)
54 return Builder.load_string(KV)
55
56 def dummy(self):
57 self.root.ids.lbl.text = 'compile to apk' if \
58 self.root.ids.lbl.text == 'test' else 'test'
59
60 @run_on_ui_thread
61 def share_something(self):
62 sendIntent = Intent()
63 sendIntent.setAction(Intent.ACTION_SEND)
64 sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
65 sendIntent.setType("text/plain")
66
67 shareIntent = Intent.createChooser(sendIntent, None)
68 currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
69 currentActivity.startActivity(shareIntent)
70
71 @run_on_ui_thread
72 def get_picture(self):
73 photoPickerIntent = Intent(Intent.ACTION_PICK)
74 photoPickerIntent.setType("image/*")
75 currentActivity = cast(
76 'android.app.Activity', PythonActivity.mActivity)
77 currentActivity.startActivityForResult(
78 photoPickerIntent, self.PICK_IMAGE)
79
80 @mainthread
81 def activity_result(self, requestCode, resultCode, data):
82 if requestCode == self.PICK_IMAGE and Activity.RESULT_OK:
83 uri = data.getData()
84 self.root.ids.lbl.text = str(uri)
85 # you do the rest!
86
87
88if __name__ == '__main__':
89 app = GalleryApp()
90 app.run()
91
92####################################################################àà
93# buildozer#
94
95[app]
96
97# (str) Title of your application
98title = Test
99
100# (str) Package name
101package.name = test
102
103# (str) Package domain (needed for android/ios packaging)
104package.domain = org.test
105
106# (str) Source code where the main.py live
107source.dir = .
108
109# (list) Source files to include (let empty to include all the files)
110source.include_exts = py,png,jpg,kv,atlas
111
112# (list) List of inclusions using pattern matching
113#source.include_patterns = assets/*,images/*.png
114
115# (list) Source files to exclude (let empty to not exclude anything)
116source.exclude_exts = spec
117
118# (list) List of directory to exclude (let empty to not exclude anything)
119source.exclude_dirs = tests,bin
120
121# (list) List of exclusions using pattern matching
122#source.exclude_patterns = license,images/*/*.jpg
123
124# (str) Application versioning (method 1)
125version = 0.1
126
127# (str) Application versioning (method 2)
128# version.regex = __version__ = ['"](.*)['"]
129# version.filename = %(source.dir)s/main.py
130
131# (list) Application requirements
132# comma separated e.g. requirements = sqlite3,kivy
133requirements = python3,kivy,git+https://github.com/HeaTTheatR/KivyMD.git
134
135# (str) Custom source folders for requirements
136# Sets custom source for any requirements with recipes
137# requirements.source.kivy = ../../kivy
138
139# (list) Garden requirements
140#garden_requirements =
141
142# (str) Presplash of the application
143#presplash.filename = %(source.dir)s/data/presplash.png
144
145# (str) Icon of the application
146#icon.filename = %(source.dir)s/data/icon.png
147
148# (str) Supported orientation (one of landscape, sensorLandscape, portrait or all)
149orientation = portrait
150
151# (list) List of service to declare
152#services = NAME:ENTRYPOINT_TO_PY,NAME2:ENTRYPOINT2_TO_PY
153
154#
155# OSX Specific
156#
157
158#
159# author = © Copyright Info
160
161# change the major version of python used by the app
162osx.python_version = 3
163
164# Kivy version to use
165osx.kivy_version = 1.9.1
166
167#
168# Android specific
169#
170
171# (bool) Indicate if the application should be fullscreen or not
172fullscreen = 0
173
174# (string) Presplash background color (for new android toolchain)
175# Supported formats are: #RRGGBB #AARRGGBB or one of the following names:
176# red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray,
177# darkgray, grey, lightgrey, darkgrey, aqua, fuchsia, lime, maroon, navy,
178# olive, purple, silver, teal.
179#android.presplash_color = #FFFFFF
180
181# (list) Permissions
182android.permissions = INTERNET,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE,WAKE_LOCK
183
184# (int) Target Android API, should be as high as possible.
185android.api = 29
186
187# (int) Minimum API your APK will support.
188#android.minapi = 21
189
190# (int) Android SDK version to use
191#android.sdk = 20
192
193# (str) Android NDK version to use
194#android.ndk = 19b
195
196# (int) Android NDK API to use. This is the minimum API your app will support, it should usually match android.minapi.
197#android.ndk_api = 21
198
199# (bool) Use --private data storage (True) or --dir public storage (False)
200#android.private_storage = True
201
202# (str) Android NDK directory (if empty, it will be automatically downloaded.)
203#android.ndk_path =
204
205# (str) Android SDK directory (if empty, it will be automatically downloaded.)
206#android.sdk_path =
207
208# (str) ANT directory (if empty, it will be automatically downloaded.)
209#android.ant_path =
210
211# (bool) If True, then skip trying to update the Android sdk
212# This can be useful to avoid excess Internet downloads or save time
213# when an update is due and you just want to test/build your package
214android.skip_update = True
215
216# (bool) If True, then automatically accept SDK license
217# agreements. This is intended for automation only. If set to False,
218# the default, you will be shown the license when first running
219# buildozer.
220# android.accept_sdk_license = False
221
222# (str) Android entry point, default is ok for Kivy-based app
223#android.entrypoint = org.renpy.android.PythonActivity
224
225# (str) Android app theme, default is ok for Kivy-based app
226# android.apptheme = "@android:style/Theme.NoTitleBar"
227
228# (list) Pattern to whitelist for the whole project
229#android.whitelist =
230
231# (str) Path to a custom whitelist file
232#android.whitelist_src =
233
234# (str) Path to a custom blacklist file
235#android.blacklist_src =
236
237# (list) List of Java .jar files to add to the libs so that pyjnius can access
238# their classes. Don't add jars that you do not need, since extra jars can slow
239# down the build process. Allows wildcards matching, for example:
240# OUYA-ODK/libs/*.jar
241#android.add_jars = foo.jar,bar.jar,path/to/more/*.jar
242
243# (list) List of Java files to add to the android project (can be java or a
244# directory containing the files)
245#android.add_src =
246
247# (list) Android AAR archives to add (currently works only with sdl2_gradle
248# bootstrap)
249#android.add_aars =
250
251# (list) Gradle dependencies to add (currently works only with sdl2_gradle
252# bootstrap)
253#android.gradle_dependencies =
254
255# (list) add java compile options
256# this can for example be necessary when importing certain java libraries using the 'android.gradle_dependencies' option
257# see https://developer.android.com/studio/write/java8-support for further information
258# android.add_compile_options = "sourceCompatibility = 1.8", "targetCompatibility = 1.8"
259
260# (list) Gradle repositories to add {can be necessary for some android.gradle_dependencies}
261# please enclose in double quotes
262# e.g. android.gradle_repositories = "maven { url 'https://kotlin.bintray.com/ktor' }"
263#android.add_gradle_repositories =
264
265# (list) packaging options to add
266# see https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html
267# can be necessary to solve conflicts in gradle_dependencies
268# please enclose in double quotes
269# e.g. android.add_packaging_options = "exclude 'META-INF/common.kotlin_module'", "exclude 'META-INF/*.kotlin_module'"
270#android.add_gradle_repositories =
271
272# (list) Java classes to add as activities to the manifest.
273#android.add_activities = com.example.ExampleActivity
274
275# (str) OUYA Console category. Should be one of GAME or APP
276# If you leave this blank, OUYA support will not be enabled
277#android.ouya.category = GAME
278
279# (str) Filename of OUYA Console icon. It must be a 732x412 png image.
280#android.ouya.icon.filename = %(source.dir)s/data/ouya_icon.png
281
282# (str) XML file to include as an intent filters in <activity> tag
283#android.manifest.intent_filters =
284
285# (str) launchMode to set for the main activity
286#android.manifest.launch_mode = standard
287
288# (list) Android additional libraries to copy into libs/armeabi
289#android.add_libs_armeabi = libs/android/*.so
290#android.add_libs_armeabi_v7a = libs/android-v7/*.so
291#android.add_libs_arm64_v8a = libs/android-v8/*.so
292#android.add_libs_x86 = libs/android-x86/*.so
293#android.add_libs_mips = libs/android-mips/*.so
294
295# (bool) Indicate whether the screen should stay on
296# Don't forget to add the WAKE_LOCK permission if you set this to True
297android.wakelock = True
298
299# (list) Android application meta-data to set (key=value format)
300#android.meta_data =
301
302# (list) Android library project to add (will be added in the
303# project.properties automatically.)
304#android.library_references =
305
306# (list) Android shared libraries which will be added to AndroidManifest.xml using <uses-library> tag
307#android.uses_library =
308
309# (str) Android logcat filters to use
310android.logcat_filters = *:S python:D
311
312# (bool) Copy library instead of making a libpymodules.so
313#android.copy_libs = 1
314
315# (str) The Android arch to build for, choices: armeabi-v7a, arm64-v8a, x86, x86_64
316android.arch = armeabi-v7a
317
318# (int) overrides automatic versionCode computation (used in build.gradle)
319# this is not the same as app version and should only be edited if you know what you're doing
320# android.numeric_version = 1
321
322#
323# Python for android (p4a) specific
324#
325
326# (str) python-for-android fork to use, defaults to upstream (kivy)
327#p4a.fork = kivy
328
329# (str) python-for-android branch to use, defaults to master
330#p4a.branch = master
331
332# (str) python-for-android git clone directory (if empty, it will be automatically cloned from github)
333#p4a.source_dir =
334
335# (str) The directory in which python-for-android should look for your own build recipes (if any)
336#p4a.local_recipes =
337
338# (str) Filename to the hook for p4a
339#p4a.hook =
340
341# (str) Bootstrap to use for android builds
342# p4a.bootstrap = sdl2
343
344# (int) port number to specify an explicit --port= p4a argument (eg for bootstrap flask)
345#p4a.port =
346
347
348#
349# iOS specific
350#
351
352# (str) Path to a custom kivy-ios folder
353#ios.kivy_ios_dir = ../kivy-ios
354# Alternately, specify the URL and branch of a git checkout:
355ios.kivy_ios_url = https://github.com/kivy/kivy-ios
356ios.kivy_ios_branch = master
357
358# Another platform dependency: ios-deploy
359# Uncomment to use a custom checkout
360#ios.ios_deploy_dir = ../ios_deploy
361# Or specify URL and branch
362ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
363ios.ios_deploy_branch = 1.7.0
364
365# (str) Name of the certificate to use for signing the debug version
366# Get a list of available identities: buildozer ios list_identities
367#ios.codesign.debug = "iPhone Developer: <lastname> <firstname> (<hexstring>)"
368
369# (str) Name of the certificate to use for signing the release version
370#ios.codesign.release = %(ios.codesign.debug)s
371
372
373[buildozer]
374
375# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
376log_level = 2
377
378# (int) Display warning if buildozer is run as root (0 = False, 1 = True)
379warn_on_root = 1
380
381# (str) Path to build artifact storage, absolute or relative to spec file
382# build_dir = ./.buildozer
383
384# (str) Path to build output (i.e. .apk, .ipa) storage
385# bin_dir = ./bin
386
387# -----------------------------------------------------------------------------
388# List as sections
389#
390# You can define all the "list" as [section:key].
391# Each line will be considered as a option to the list.
392# Let's take [app] / source.exclude_patterns.
393# Instead of doing:
394#
395#[app]
396#source.exclude_patterns = license,data/audio/*.wav,data/images/original/*
397#
398# This can be translated into:
399#
400#[app:source.exclude_patterns]
401#license
402#data/audio/*.wav
403#data/images/original/*
404#
405
406
407# -----------------------------------------------------------------------------
408# Profiles
409#
410# You can extend section / key with a profile
411# For example, you want to deploy a demo version of your application without
412# HD content. You could first change the title to add "(demo)" in the name
413# and extend the excluded directories to remove the HD content.
414#
415#[app@demo]
416#title = My Application (demo)
417#
418#[app:source.exclude_patterns@demo]
419#images/hd/*
420#
421# Then, invoke the command line with the "demo" profile:
422#
423#buildozer --profile demo android debug