Janabmalikj/Androidlanat
0
1import streamlit as st2import os3import zipfile4import subprocess5import shutil6import time7 8# Page Config9st.set_page_config(page_title="Android Build Server", layout="centered")10 11st.title("๐ฑ Android APK/AAB Generator")12st.markdown("Apna Android Studio Project (ZIP) upload karein aur server APK bana dega.")13st.warning("โ ๏ธ Note: Project 'Build' folder free hona chahiye aur root mein 'gradlew' hona zaroori hai.")14 15# File Uploader16uploaded_file = st.file_uploader("Project ZIP File Upload Karein", type=["zip"])17 18if uploaded_file is not None:19 # Button Layout20 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 files29 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 Zip34 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 = None45 for root, dirs, files in os.walk("project_extract"):46 if "gradlew" in files:47 project_root = root48 break49 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 karein54 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 Karein59 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 karna63 try:64 result = subprocess.run(65 [f"./gradlew", build_type],66 cwd=project_root,67 stdout=subprocess.PIPE,68 stderr=subprocess.PIPE,69 text=True70 )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 dhoondna79 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 break87 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 # Cleanup106 if os.path.exists("uploaded_project.zip"):107 os.remove("uploaded_project.zip")