buildozer / app.py
Serg4451D's picture
Create app.py
8228a19 verified
import gradio as gr
import os
import subprocess
import tempfile
def create_buildozer_ini():
"""Создает базовый buildozer.ini файл"""
buildozer_config = """[app]
title = MyApp
package.name = myapp
package.domain = org.test
source.dir = .
source.include_exts = py
version = 0.1
requirements = python3,kivy,kivymd
orientation = portrait
osx.python_version = 3
osx.kivy_version = 1.9.1
fullscreen = 0
android.permissions = INTERNET
android.api = 27
android.minapi = 21
android.sdk = 20
android.ndk = 19b
android.private_storage = True
android.accept_sdk_license = True
"""
with open("buildozer.ini", "w") as f:
f.write(buildozer_config)
def build_apk(code):
try:
# Создаем временную директорию
with tempfile.TemporaryDirectory() as temp_dir:
os.chdir(temp_dir)
# Создаем main.py с предоставленным кодом
with open("main.py", "w") as f:
f.write(code)
# Создаем buildozer.ini
create_buildozer_ini()
# Запускаем buildozer для сборки APK
process = subprocess.Popen(
["buildozer", "android", "debug"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
output, error = process.communicate()
# Проверяем, создался ли APK
apk_path = os.path.join(temp_dir, "bin")
if os.path.exists(apk_path):
apk_files = [f for f in os.listdir(apk_path) if f.endswith('.apk')]
if apk_files:
return os.path.join(apk_path, apk_files[0])
return f"Ошибка при сборке: {error.decode()}"
except Exception as e:
return f"Произошла ошибка: {str(e)}"
# Создаем интерфейс Gradio
iface = gr.Interface(
fn=build_apk,
inputs=gr.Textbox(lines=10, label="Введите код на Python с Kivy MD"),
outputs=gr.File(label="Скачать APK"),
title="Kivy MD to APK Builder",
description="Введите код на Python с Kivy MD для создания APK файла"
)
# Запускаем интерфейс
iface.launch()