#python #android #permissions #kivy #buildozer
#python #Android #разрешения #kivy #buildozer
Вопрос:
Привет всем и, прежде всего, большое спасибо всем вам, кто помог мне, не зная об этом. Для такого новичка, как я, переполнение стека действительно ценно.
Я новичок в программировании, изучил Python3, чтобы посмотреть, смогу ли я, и пытаюсь создать приложение для Android для профессиональной деятельности моей подруги.
Я собираю информацию на ее веб-сайте, помещаю ее в файл JSON, немного изменяю ее и использую Kivy и Float Layout, чтобы добавить их на экран. Самая продвинутая часть (для меня) — это функция для совершения телефонного звонка на Android.
На моем ПК (Ubuntu или Windows) все работает очень хорошо, но когда дело доходит до Android, он вылетает при запуске (после «загрузки» я получаю черный экран).
Сначала Buildozer запросил множество требований (вы увидите это в файле .spec), после того, как эта проблема была решена, я запустил другой с разрешением Android, в котором было отказано в ACCESS_SURFACE_FLINGER. Я спросил с помощью builddozer.spec, и это не очень хорошо, как вы можете видеть в журналах.
Я также использовал некоторый код для запроса в моем main.py
from android.permissions import request_permissions, Permission
request_permissions([Permission.ACCESS_SURFACE_FLINGER])
и он возвращает это разрешение как отсутствие аргумента ACCESS_SURFACE_FLINGER.
Не могли бы вы помочь мне, пожалуйста? Я потратил 2 дня на поиски, как заставить это работать, и моих навыков (и Google) недостаточно.
Мой buildozer.spec :
[app]
# (str) Title of your application
title = ConcepTruelle
# (str) Package name
package.name = conceptruelle
# (str) Package domain (needed for android/ios packaging)
package.domain = org.conceptruelle
# (str) Source code where the main.py live
source.dir = .
# (list) Source files to include (let empty to include all the files)
source.include_exts = py,png,jpg,kv,atlas,py
# (str) Application versioning (method 1)
version = 0.1
# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = python3,hostpython3,kivy==master,scrapy,jnius,pyjnius,requests,simplejson,openssl,android,prettyprinter,twisted,w3lib,attrs,lxml,parsel,cssselect,itemadapter,cryptography,pyopenssl,zlib_wrapper,macholib,pydispatch,pydispatcher,libxslt,libxml2,service_identity,pyasn1,pyasn1_modules,google-auth-oauthlib,android.permissions
# (str) Icon of the application
icon.filename = %(source.dir)s/icon.png
# (str) Supported orientation (one of landscape, sensorLandscape, portrait or all)
orientation = portrait
#
# Android specific
#
# (bool) Indicate if the application should be fullscreen or not
fullscreen = 0
# (list) Permissions
android.permissions = ACCESS_SURFACE_FLINGER,INTERNET,ACCEPT_HANDOVER,BROADCAST_STICKY,PROCESS_OUTGOING_CALLS,ACTIVITY_RECOGNITION,WRITE_EXTERNAL_STORAGE
# (int) Target Android API, should be as high as possible.
#android.api = 27
# (int) Minimum API your APK will support.
#android.minapi = 21
# (int) Android SDK version to use
#android.sdk = 20
# (str) Android NDK version to use
#android.ndk = 19b
# (int) Android NDK API to use. This is the minimum API your app will support, it should usually match android.minapi.
#android.ndk_api = 21
# (bool) Use --private data storage (True) or --dir public storage (False)
#android.private_storage = True
# (str) Android NDK directory (if empty, it will be automatically downloaded.)
#android.ndk_path =
# (str) Android SDK directory (if empty, it will be automatically downloaded.)
#android.sdk_path =
# (str) ANT directory (if empty, it will be automatically downloaded.)
Мои журналы :
10-26 17:12:09.463 9229 9229 V SDL : nativeSetupJNI()
10-26 17:12:09.466 9229 9229 V SDL : AUDIO nativeSetupJNI()
10-26 17:12:09.468 9229 9229 V SDL : CONTROLLER nativeSetupJNI()
10-26 17:12:09.494 9229 9229 D hidapi : Initializing Bluetooth
10-26 17:12:09.495 9229 9229 D hidapi : Couldn't initialize Bluetooth, missing android.permission.BLUETOOTH
10-26 17:12:09.525 9229 9229 V PythonActivity: Setting env vars for start.c and Python to use
10-26 17:12:09.525 9229 9229 V PythonActivity: Access to our meta-data...
10-26 17:12:09.529 9229 9229 I PythonActivity: Surface will NOT be transparent
10-26 17:12:09.529 9229 9229 V PythonActivity: onResume()
10-26 17:12:09.530 9229 9229 V SDL : onResume()
10-26 17:12:09.557 604 942 W ServiceManager: Permission failure: android.permission.ACCESS_SURFACE_FLINGER from uid=10182 pid=9229
10-26 17:12:09.558 604 942 D PermissionCache: checking android.permission.ACCESS_SURFACE_FLINGER for uid=10182 => denied (814 us)
10-26 17:12:09.562 9229 9229 V SDL : surfaceCreated()
10-26 17:12:09.562 9229 9229 V SDL : surfaceChanged()
10-26 17:12:09.563 9229 9229 V SDL : pixel format RGB_565
10-26 17:12:09.565 9229 9229 V SDL : Window size: 1080x1731
10-26 17:12:09.565 9229 9229 V SDL : Device size: 1080x1920
10-26 17:12:09.578 9229 9229 V SDL : nativeResume()
10-26 17:12:09.579 9229 9326 V SDL : Running main function SDL_main from library /data/app/org.conceptruelle.conceptruelle-OBg7Tkf0OmcdRT4L0eMfDQ==/lib/arm/libmain.so
10-26 17:12:09.579 9229 9326 V PythonActivity: appConfirmedActive() -> preparing loading screen removal
10-26 17:12:09.580 9229 9326 V SDL : nativeRunMain()
10-26 17:12:09.582 9229 9326 I python : Initializing Python for Android
10-26 17:12:09.582 9229 9326 I python : Setting additional env vars from p4a_env_vars.txt
10-26 17:12:09.583 9229 9326 I python : Changing directory to the one provided by ANDROID_ARGUMENT
10-26 17:12:09.583 9229 9326 I python : /data/user/0/org.conceptruelle.conceptruelle/files/app
10-26 17:12:09.584 9229 9326 I python : Preparing to initialize python
10-26 17:12:09.584 9229 9326 I python : _python_bundle dir exists
10-26 17:12:09.585 9229 9326 I python : calculated paths to be...
10-26 17:12:09.585 9229 9326 I python : /data/user/0/org.conceptruelle.conceptruelle/files/app/_python_bundle/stdlib.zip:/data/user/0/org.conceptruelle.conceptruelle/files/app/_python_bundle/modules
10-26 17:12:09.590 9229 9326 I python : set wchar paths...
10-26 17:12:09.730 9229 9326 I python : Initialized python
10-26 17:12:09.730 9229 9326 I python : AND: Init threads
10-26 17:12:09.734 9229 9326 I python : testing python print redirection
10-26 17:12:09.738 9229 9326 I python : Android path ['.', '/data/user/0/org.conceptruelle.conceptruelle/files/app/_python_bundle/stdlib.zip', '/data/user/0/org.conceptruelle.conceptruelle/files/app/_python_bundle/modules', '/data/user/0/org.conceptruelle.conceptruelle/files/app/_python_bundle/site-packages']
10-26 17:12:09.739 9229 9326 I python : os.environ is environ({'PATH': '/sbin:/system/sbin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin', 'DOWNLOAD_CACHE': '/data/cache', 'ANDROID_BOOTLOGO': '1', 'ANDROID_ROOT': '/system', 'ANDROID_ASSETS': '/system/app', 'ANDROID_DATA': '/data', 'ANDROID_STORAGE': '/storage', 'EXTERNAL_STORAGE': '/sdcard', 'ASEC_MOUNTPOINT': '/mnt/asec', 'BOOTCLASSPATH': '/system/framework/core-oj.jar:/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/bouncycastle.jar:/system/framework/apache-xml.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/android.hidl.base-V1.0-java.jar:/system/framework/android.hidl.manager-V1.0-java.jar:/system/framework/framework-oahl-backward-compatibility.jar:/system/framework/android.test.base.jar:/system/framework/telephony-ext.jar', 'SYSTEMSERVERCLASSPATH': '/system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar:/system/framework/com.android.location.provider.jar', 'ANDROID_SOCKET_zygote_secondary': '11', 'ANDROID_ENTRYPOINT': 'main.pyc', 'ANDROID_ARGUMENT': '/data/user/0/org.conceptruelle.conceptruelle/files/app', 'ANDROID_APP_PATH': '/data/user/0/org.conceptruelle.conceptruelle/files/app', 'ANDROID_PRIVATE': '/data/user/0/org.conceptruelle.conceptruelle/files', 'ANDROID_UNPACK': '/data/user/0/org.conceptruelle.conceptruelle/files/app', 'PYTHONHOME': '/data/user/0/org.conceptruelle.conceptruelle/files/app', 'PYTHONPATH': '/data/user/0/org.conceptruelle.conceptruelle/files/app:/data/user/0/org.conceptruelle.conceptruelle/files/app/lib', 'PYTHONOPTIMIZE': '2', 'P4A_BOOTSTRAP': 'SDL2', 'PYTHON_NAME': 'python', 'P4A_IS_WINDOWED': 'True', 'P4A_ORIENTATION': 'portrait', 'P4A_NUMERIC_VERSION': 'None', 'P4A_MINSDK': '21', 'LC_CTYPE': 'C.UTF-8'})
10-26 17:12:09.742 9229 9326 I python : Android kivy bootstrap done. __name__ is __main__
10-26 17:12:09.743 9229 9326 I python : AND: Ran string
10-26 17:12:09.743 9229 9326 I python : Run user program, change dir and execute entrypoint
10-26 17:12:10.081 9229 9326 I python : [WARNING] [Config ] Older configuration version detected (0 instead of 21)
10-26 17:12:10.081 9229 9326 I python : [WARNING] [Config ] Upgrading configuration in progress.
10-26 17:12:10.102 9229 9326 I python : [INFO ] [Logger ] Record log in /data/user/0/org.conceptruelle.conceptruelle/files/app/.kivy/logs/kivy_20-10-26_0.txt
10-26 17:12:10.104 9229 9326 I python : [INFO ] [Kivy ] v1.11.1
10-26 17:12:10.105 9229 9326 I python : [INFO ] [Kivy ] Installed at "/data/user/0/org.conceptruelle.conceptruelle/files/app/_python_bundle/site-packages/kivy/__init__.pyc"
10-26 17:12:10.107 9229 9326 I python : [INFO ] [Python ] v3.8.1 (default, Oct 26 2020, 11:01:07)
10-26 17:12:10.107 9229 9326 I python : [Clang 8.0.2 (https://android.googlesource.com/toolchain/clang 40173bab62ec7462
10-26 17:12:10.109 9229 9326 I python : [INFO ] [Python ] Interpreter at ""
10-26 17:12:10.111 9229 9326 I python : [INFO ] [Logger ] Purge log fired. Analysing...
10-26 17:12:10.113 9229 9326 I python : [INFO ] [Logger ] Purge finished!
10-26 17:12:14.439 9229 9326 I python : [INFO ] [Factory ] 184 symbols loaded
10-26 17:12:15.352 9229 9326 I python : [INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
10-26 17:12:15.436 9229 9326 I python : [INFO ] [Text ] Provider: sdl2
10-26 17:12:15.520 9229 9326 I python : [INFO ] [Window ] Provider: sdl2
10-26 17:12:15.573 9229 9326 E libEGL : validate_display:92 error 3008 (EGL_BAD_DISPLAY)
10-26 17:12:15.575 9229 9326 V SDL : setOrientation() orientation=-1 width=800 height=600 resizable=true hint=
10-26 17:12:15.593 9229 9326 I python : [INFO ] [GL ] Using the "OpenGL ES 2" graphics system
10-26 17:12:15.597 9229 9326 I python : [INFO ] [GL ] Backend used <sdl2>
10-26 17:12:15.599 9229 9326 I python : [INFO ] [GL ] OpenGL version <b'OpenGL ES 3.2 V@258.0 (GIT@8e59954, I0be83d0d26) (Date:09/22/17)'>
10-26 17:12:15.600 9229 9326 I python : [INFO ] [GL ] OpenGL vendor <b'Qualcomm'>
10-26 17:12:15.602 9229 9326 I python : [INFO ] [GL ] OpenGL renderer <b'Adreno (TM) 418'>
10-26 17:12:15.604 9229 9326 I python : [INFO ] [GL ] OpenGL parsed version: 3, 2
10-26 17:12:15.606 9229 9326 I python : [INFO ] [GL ] Texture max size <16384>
10-26 17:12:15.607 9229 9326 I python : [INFO ] [GL ] Texture max units <16>
10-26 17:12:15.724 9229 9326 I python : [INFO ] [Window ] auto add sdl2 input provider
10-26 17:12:15.727 9229 9326 I python : [INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
10-26 17:12:15.855 9229 9326 I python : Traceback (most recent call last):
10-26 17:12:15.857 9229 9326 I python : 2020-10-26 17:12:15 [kivy] WARNING: stderr: Traceback (most recent call last):
10-26 17:12:15.858 9229 9326 I python : 2020-10-26 17:12:15 [kivy] WARNING: stderr: 2020-10-26 17:12:15 [kivy] WARNING: stderr: Traceback (most recent call last):
10-26 17:12:15.860 9229 9326 I python : 2020-10-26 17:12:15 [kivy] WARNING: stderr: 2020-10-26 17:12:15
Редактировать :
Вот код :
import kivy
kivy.require('1.9.1')
import scrapy
import os
import json
import pprint
import webbrowser
from kivy.app import App
from kivy.config import Config
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from scrapy.crawler import CrawlerProcess
from kivy.lang import Builder
from jnius import autoclass
from jnius import cast
Config.set('graphics', 'resizable', False)
Builder.load_string("""
<MyLabel1>
text_size: self.size
shorten: False
markup: True
background_color: 0.7, 0.7, 0.7, .2
canvas.before:
Color:
rgba: self.background_color
Rectangle:
pos: self.pos
size: self.width, 92
<MyLabel2>
text_size: self.size
shorten: False
markup: True
background_color: 0.9, 0.9, 0.9, .3
canvas.before:
Color:
rgba: self.background_color
Rectangle:
pos: self.pos
size: self.width, 92
<MyTitle>
markup: True
""")
class MyLabel1(Label):
"""
A Label class with dark grey background, markups and wrapping
"""
pass
class MyLabel2(Label):
"""
A Label class with light grey background, markups and wrapping
"""
pass
class MyTitle(Label):
"""
A Label class with markups for the main title
"""
pass
class AgendaSpider(scrapy.Spider):
"""
A scraper who retrieve informations on ConcepTruelle's website
"""
name = "agendaconceptruelle"
start_urls = [
'http://conceptruelle.fr/index.php/notre-agenda'
]
while os.path.exists('agenda.json'): os.remove('agenda.json')
def parse(self, response):
for agenda in response.css("div.title-header.ic-title-header.ic-float-left"):
yield {
'title': agenda.css("a ::text").get(),
}
for agenda in response.css("div.title-cat.ic-title-cat.ic-float-right"):
yield {
'category': agenda.css(" ::text").get(),
}
for agenda in response.css("div.nextdate.ic-next-date.ic-clearfix"):
yield {
'date': agenda.css("span ::text").get(),
}
for agenda in response.css("div.nextdate.ic-next-date.ic-clearfix"):
yield {
'time': agenda.css("span.ic-single-starttime ::text").get(),
}
for agenda in response.css("div.place.ic-place"):
yield {
'location': agenda.css(" ::text").get(),
}
for agenda in response.css("div.descshort.ic-descshort"):
yield {
'description': agenda.css(" ::text").get(),
}
class ConceptruelleApp(App):
def build(self):
"""
Scrape ConcepTruelle's website agenda and put it on screen
4 buttons give access to website, social medias and phone number
"""
def phonecall(self):
"""
Make a Phone Call on Android with Jnius
"""
Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
PythonActivity = autoclass('org.renpy.android.PythonActivity')
intent = Intent(Intent.ACTION_CALL)
intent.setData(Uri.parse("tel:" "0600000000"))
currentActivity = cast('android.app.Activity',
PythonActivity.mActivity)
currentActivity.startActivity(intent)
def website(self):
"""
Open ConcepTruelle's website
"""
webbrowser.open("http://conceptruelle.fr/")
def facebook(self):
"""
Open ConcepTruelle's Facebook page
"""
webbrowser.open("https://www.facebook.com/ConcepTruelle")
def instagram(self):
"""
Open ConcepTruelle's Instagram page
"""
webbrowser.open("https://www.instagram.com/conceptruelle")
# Main application : definition of title, labels and buttons put on a Float Layout
fl = FloatLayout()
lbl0 = MyTitle(text = " [size=28][color=000000]L'[/color][color=dd9514][i]Agenda[/i][/color] [color=000000][b]ConcepTruelle[/b][/size][/color]", markup=True, size_hint =(1, .1), pos_hint ={'center_x':.5, 'center_y':.97})
lbl1 = MyLabel1(text = event_title_1 "n" agenda1, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':1.03})
lbl2 = MyLabel2(text = event_title_2 "n" agenda2, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':.86})
lbl3 = MyLabel1(text = event_title_3 "n" agenda3, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':.69})
lbl4 = MyLabel2(text = event_title_4 "n" agenda4, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':.52})
lbl5 = MyLabel1(text = event_title_5 "n" agenda5, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':.35})
fl.add_widget(lbl0)
fl.add_widget(lbl1)
fl.add_widget(lbl2)
fl.add_widget(lbl3)
fl.add_widget(lbl4)
fl.add_widget(lbl5)
btn1 = Button(text = "Appel", size_hint =(.25, .1), pos_hint ={'center_x':.125, 'center_y':.05})
btn1.bind(on_press=phonecall)
btn2 = Button(text = "Site Web", size_hint =(.25, .1), pos_hint ={'center_x':.375, 'center_y':.05})
btn2.bind(on_press=website)
btn3 = Button(text = "Facebook", size_hint =(.25, .1), pos_hint ={'center_x':.625, 'center_y':.05})
btn3.bind(on_press=facebook)
btn4 = Button(text = "Instagram", size_hint =(.25, .1), pos_hint ={'center_x':.875, 'center_y':.05})
btn4.bind(on_press=instagram)
fl.add_widget(btn1)
fl.add_widget(btn2)
fl.add_widget(btn3)
fl.add_widget(btn4)
return fl
if __name__ == '__main__':
# using Scrapy to get informations from ConcepTruelle's website and puting it in a JSON file
process = CrawlerProcess(settings={
"FEEDS": {
"agenda.json": {"format": "json"},
},
})
process.crawl(AgendaSpider)
process.start()
# Opening the created JSON file and using PPRINT to get strings
with open("agenda.json") as f:
agenda_list = json.load(f)
pprint.pprint(agenda_list)
# Modifying the JSON to fit requirements
for agenda_dict in agenda_list:
for k,v in agenda_dict.items():
if v is not None and k == "time":
agenda_dict[k] = "à " str(agenda_dict[k])
if v is None:
agenda_dict[k] = "de 10:00 à 16:00"
for k,v in agenda_dict.items():
if k == "category" and v == "ntttttStagetttt":
agenda_dict[k] = "[color=0aa7f5]" str(agenda_dict[k]) "[/color]"
if k == "category" and v == "ntttttAteliertttt":
agenda_dict[k] = "[color=8113bd]" str(agenda_dict[k]) "[/color]"
for k,v in agenda_dict.items():
agenda_dict[k] = v.replace('t', '')
for k,v in agenda_dict.items():
agenda_dict[k] = v.replace('n', '')
# dumping the modified JSON file
with open('agenda.json', 'w') as outfile:
json.dump(agenda_list, outfile)
# creating titles and labels with the JSON informations
event_title_1 = str("[size=18] [b]" agenda_list[5].get("category") "[/b] [color=000000]-[/color] [b][color=dd9514]" str(agenda_list[0].get("title")) "[/b][/color][/size]")
event_title_2 = str("[size=18] [b]" agenda_list[6].get("category") "[/b] [color=000000]-[/color] [b][color=dd9514]" str(agenda_list[1].get("title")) "[/b][/color][/size]")
event_title_3 = str("[size=18] [b]" agenda_list[7].get("category") "[/b] [color=000000]-[/color] [b][color=dd9514]" str(agenda_list[2].get("title")) "[/b][/color][/size]")
event_title_4 = str("[size=18] [b]" agenda_list[8].get("category") "[/b] [color=000000]-[/color] [b][color=dd9514]" str(agenda_list[3].get("title")) "[/b][/color][/size]")
event_title_5 = str("[size=18] [b]" agenda_list[9].get("category") "[/b] [color=000000]-[/color] [b][color=dd9514]" str(agenda_list[4].get("title")) "[/b][/color][/size]")
agenda1 = str("[color=000000][i] " str(agenda_list[25].get("description")) "[/i]n Le " str(agenda_list[10].get("date")) " " str(agenda_list[15].get("time")) "n Lieu : " str(agenda_list[20].get("location")) "[/color]")
agenda2 = str("[color=000000][i] " str(agenda_list[26].get("description")) "[/i]n Le " str(agenda_list[11].get("date")) " " str(agenda_list[16].get("time")) "n Lieu : " str(agenda_list[21].get("location")) "[/color]")
agenda3 = str("[color=000000][i] " str(agenda_list[27].get("description")) "[/i]n Le " str(agenda_list[12].get("date")) " " str(agenda_list[17].get("time")) "n Lieu : " str(agenda_list[22].get("location")) "[/color]")
agenda4 = str("[color=000000][i] " str(agenda_list[28].get("description")) "[/i]n Le " str(agenda_list[13].get("date")) " " str(agenda_list[18].get("time")) "n Lieu : " str(agenda_list[23].get("location")) "[/color]")
agenda5 = str("[color=000000][i] " str(agenda_list[29].get("description")) "[/i]n Le " str(agenda_list[14].get("date")) " " str(agenda_list[19].get("time")) "n Lieu : " str(agenda_list[24].get("location")) "[/color]")
# Define main window's background color
Window.clearcolor = (1, 1, 1, 1)
# Call the main application
ConceptruelleApp().run()
Комментарии:
1. Я не думаю, что установленным пользователем приложениям разрешено это разрешение, и оно вам не нужно. Реальная проблема, скорее всего, в чем-то другом.
2. Понятно, что это не последняя моя проблема с этим проектом: D
Ответ №1:
Аргумент ACCESS_SURFACE_FLINGER не возвращается, поскольку для приложения с таким именем нет такого разрешения.
Список разрешений доступен в https://developer.android.com/reference/android/Manifest.permission и в соответствии с телефонным звонком самый простой способ — это использовать модуль PLYER.
Комментарии:
1. Но если я не запрашиваю это разрешение, сообщение все равно появляется :/
2. Вероятно, какая-то часть кода пытается получить к нему доступ, и, не видя кода, любой может быть наименее полезным.
3. Я добавил его в конце основного сообщения. Извините, что не поставил это в первый раз.
4. попробуйте поместить Window.clearcolor = (1, 1, 1, 1) в начале кода вместо конца
5. Мой плохой, это не clearcolor … и это не flinger, я получаю регистратор python в бесконечном цикле.