成功安装后应用程序在 Android 上崩溃

问题描述 投票:0回答:0

我使用 Python 中的 Kivy 库创建了一个应用程序。该应用程序的目的是使用我的 API 从 YouTube 下载音乐。当我运行 main.py 文件时,该应用程序在我的桌面上运行良好。然而,在使用 buildozer 将应用程序构建到 APK 并将其安装到我的 Android 智能手机上后,应用程序在显示图像后立即崩溃。

这是我的 main.py 文件:

from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from googleapiclient.discovery import build
from pytube import YouTube
from kivymd.app import MDApp
from kivymd.uix.button import MDFillRoundFlatButton
from kivymd.uix.textfield import MDTextField
from kivymd.uix.list import OneLineListItem
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.uix.widget import Widget
from kivymd.theming import ThemeManager
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.dialog import MDDialog
from kivy.app import App
import logging
import os


Builder.load_string('''
<RecycleViewRow@OneLineListItem>:
    text: ''
    video_id: ''
    on_release: app.on_video_selected(root.video_id)

<RV>:
    viewclass: 'RecycleViewRow'
    RecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
''')

class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = []

class MusicDownloaderApp(MDApp):
    def build(self):
        logging.info('Building the app')
        self.theme_cls.primary_palette = 'Teal'  
        self.theme_cls.primary_hue = '700'
        self.theme_cls.theme_style = 'Light'
        


        layout = MDBoxLayout(orientation='vertical', padding='32dp', spacing='20dp')

        # Add a BoxLayout at the top for the title/logo
        title_layout = MDBoxLayout(orientation='horizontal', size_hint=(1, None), height="50dp", padding='2dp', spacing='7dp' )
        layout.add_widget(title_layout)

        # Add a label as a title
        title = Label(text="Music Downloader", font_size='20sp')
        title_layout.add_widget(title)

        title_layout.add_widget(Widget())

        # Or add an image as a logo
        logo = Image(source="download.png", size_hint=(None, None), size=("80dp", "80dp"))
        title_layout.add_widget(logo)

        title_layout.add_widget(Widget())

        title_layout.add_widget(Widget())

        self.search_bar = MDTextField(
            hint_text="Enter song title or artist name",
            size_hint=(2, None),
            height="40dp"
        )
        layout.add_widget(self.search_bar)

        self.search_button = MDFillRoundFlatButton(
            text="Search",
            on_release=self.perform_search
        )
        layout.add_widget(self.search_button)

        self.results_list = RV()
        layout.add_widget(self.results_list)

        return layout

    def perform_search(self, instance):
        query = self.search_bar.text
        logging.info(f'Performing search with query {query}')
        if not query.strip():
            return

        api_key = '<my-api-key'
        youtube = build('youtube', 'v3', developerKey=api_key)

        request = youtube.search().list(
            part="snippet",
            maxResults=50,
            q=query
        )
        response = request.execute()

        results = []
        for item in response['items']:
            if item['id']['kind'] == "youtube#video":
                video_title = item['snippet']['title']
                video_id = item['id']['videoId']
                results.append({'text': video_title, 'video_id': video_id})

        self.results_list.data = results

    def on_video_selected(self, video_id):
        logging.info(f'Selected video with id {video_id}')
        youtube_url = f"[https://www.youtube.com/watch?v={video_id}]"
        youtube = YouTube(youtube_url)
         # Get the path to the app's internal storage directory
        app_path = App.get_running_app().user_data_dir

        # Define a subdirectory to save the downloads
        download_dir = os.path.join(app_path, 'downloads')

        # Create the download directory if it doesn't exist
        if not os.path.exists(download_dir):
            os.makedirs(download_dir)

        # Download the video to the download directory
        youtube.streams.filter(only_audio=True).first().download(download_dir)
        
        dialog = MDDialog(title='Finished Downloading', text='Enjoy your music!')
        dialog.open()


        

if __name__ == "__main__":
    MusicDownloaderApp().run()

这是我的 buildozer.spec 文件:

[app]

# (str) Title of your application
title = Music Downloader

# (str) Package name
package.name = musicdownloader

# (str) Package domain (needed for android/ios packaging)
package.domain = org.test

# (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

# (str) Application versioning (method 1)
version = 0.1

# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = python3,kivy,kivymd,pytube,google-api-python-client


# (str) Presplash of the application
presplash.filename = /content/download.png

# (str) Icon of the application
icon.filename = /content/download.png

#
# OSX Specific
#

#
# author = © Copyright Info

# change the major version of python used by the app
osx.python_version = 3

# Kivy version to use
osx.kivy_version = 1.9.1

#
# Android specific
#

# (bool) Indicate if the application should be fullscreen or not
fullscreen = 0


# (list) Permissions
# (See https://python-for-android.readthedocs.io/en/latest/buildoptions/#build-options-1 for all the supported syntaxes and properties)
android.permissions = INTERNET,WRITE_EXTERNAL_STORAGE


#
# iOS specific
#

# (str) Path to a custom kivy-ios folder
#ios.kivy_ios_dir = ../kivy-ios
# Alternately, specify the URL and branch of a git checkout:
ios.kivy_ios_url = https://github.com/kivy/kivy-ios
ios.kivy_ios_branch = master

# Another platform dependency: ios-deploy
# Uncomment to use a custom checkout
#ios.ios_deploy_dir = ../ios_deploy
# Or specify URL and branch
ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
ios.ios_deploy_branch = 1.10.0

# (bool) Whether or not to sign the code
ios.codesign.allowed = false



我尝试过的:

我检查了 logcat 日志,但找不到与崩溃相关的任何特定错误。 我已确保在 buildozer.spec 文件中声明了必要的权限(INTERNET 和 WRITE_EXTERNAL_STORAGE)。 我已验证所需的库(kivy、kivymd、pytube、google-api-python-client)包含在 buildozer.spec 文件的要求部分中。 我已经确认 APK 安装成功,没有任何错误或警告。

我不确定是否需要对我的 main.py 或 buildozer.spec 文件进行任何更改来解决此问题。

任何建议或指导将不胜感激。

python android kivy apk
© www.soinside.com 2019 - 2024. All rights reserved.