Akwbw commited on
Commit
da5c160
·
verified ·
1 Parent(s): 91eceac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import zipfile
4
+ import subprocess
5
+ import shutil
6
+ import time
7
+
8
+ # Page Config
9
+ st.set_page_config(page_title="Android Build Server", layout="centered")
10
+
11
+ st.title("📱 Android APK/AAB Generator")
12
+ st.markdown("Apna Android Studio Project (ZIP) upload karein aur server APK bana dega.")
13
+ st.warning("⚠️ Note: Project 'Build' folder free hona chahiye aur root mein 'gradlew' hona zaroori hai.")
14
+
15
+ # File Uploader
16
+ uploaded_file = st.file_uploader("Project ZIP File Upload Karein", type=["zip"])
17
+
18
+ if uploaded_file is not None:
19
+ # Button Layout
20
+ col1, col2 = st.columns(2)
21
+ build_apk = col1.button("🔨 Create APK")
22
+ build_aab = col2.button("📦 Create AAB")
23
+
24
+ if build_apk or build_aab:
25
+ status_text = st.empty()
26
+ progress_bar = st.progress(0)
27
+
28
+ # 1. Cleanup old files
29
+ if os.path.exists("project_extract"):
30
+ shutil.rmtree("project_extract")
31
+ os.makedirs("project_extract", exist_ok=True)
32
+
33
+ # 2. Save and Extract Zip
34
+ status_text.info("Extracting ZIP file...")
35
+ with open("uploaded_project.zip", "wb") as f:
36
+ f.write(uploaded_file.getbuffer())
37
+
38
+ with zipfile.ZipFile("uploaded_project.zip", "r") as zip_ref:
39
+ zip_ref.extractall("project_extract")
40
+
41
+ progress_bar.progress(20)
42
+
43
+ # 3. Find root directory (Jahan gradlew file ho)
44
+ project_root = None
45
+ for root, dirs, files in os.walk("project_extract"):
46
+ if "gradlew" in files:
47
+ project_root = root
48
+ break
49
+
50
+ if not project_root:
51
+ st.error("❌ ZIP file mein 'gradlew' nahi mila. Kya ye sahi Android Studio project hai?")
52
+ else:
53
+ # 4. Permissions fix karein
54
+ status_text.info("Setting up Gradle environment...")
55
+ gradlew_path = os.path.join(project_root, "gradlew")
56
+ subprocess.run(["chmod", "+x", gradlew_path])
57
+
58
+ # 5. Build Command Run Karein
59
+ build_type = "assembleRelease" if build_apk else "bundleRelease"
60
+ status_text.info(f"Building {build_type}... (Ismay 5-10 minute lag saktay hain)")
61
+
62
+ # Subprocess chala kar build karna
63
+ try:
64
+ result = subprocess.run(
65
+ [f"./gradlew", build_type],
66
+ cwd=project_root,
67
+ stdout=subprocess.PIPE,
68
+ stderr=subprocess.PIPE,
69
+ text=True
70
+ )
71
+
72
+ progress_bar.progress(80)
73
+
74
+ if result.returncode == 0:
75
+ status_text.success("✅ Build Successful!")
76
+ progress_bar.progress(100)
77
+
78
+ # Output file dhoondna
79
+ output_path = ""
80
+ file_ext = ".apk" if build_apk else ".aab"
81
+
82
+ for root, dirs, files in os.walk(os.path.join(project_root, "app", "build", "outputs")):
83
+ for file in files:
84
+ if file.endswith(file_ext) and "release" in file:
85
+ output_path = os.path.join(root, file)
86
+ break
87
+
88
+ if output_path and os.path.exists(output_path):
89
+ with open(output_path, "rb") as f:
90
+ btn = st.download_button(
91
+ label=f"⬇️ Download {file_ext.upper()}",
92
+ data=f,
93
+ file_name=f"app-release{file_ext}",
94
+ mime="application/vnd.android.package-archive"
95
+ )
96
+ else:
97
+ st.error("Build pass hogaya magar file nahi mili. Path check karein.")
98
+ else:
99
+ st.error("❌ Build Failed!")
100
+ st.text_area("Error Log", result.stderr, height=300)
101
+
102
+ except Exception as e:
103
+ st.error(f"System Error: {str(e)}")
104
+
105
+ # Cleanup
106
+ if os.path.exists("uploaded_project.zip"):
107
+ os.remove("uploaded_project.zip")