Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,144 +1,108 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
import zipfile
|
| 4 |
-
import subprocess
|
| 5 |
-
import shutil
|
| 6 |
|
| 7 |
-
# Page Config
|
| 8 |
st.set_page_config(page_title="Android Build Server", layout="centered")
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
st.title("π± Android APK Generator (Final Production)")
|
| 11 |
-
st.markdown("Zip upload karein. Server APK banayega aur **Auto-Sign** karega.")
|
| 12 |
-
|
| 13 |
-
# --- HELPER: GENERATE KEY ---
|
| 14 |
def generate_keystore():
|
| 15 |
-
|
| 16 |
-
if not os.path.exists(
|
| 17 |
-
|
| 18 |
-
"keytool",
|
| 19 |
-
"-
|
| 20 |
-
"-
|
| 21 |
-
"-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
if
|
| 34 |
if st.button("π¨ Create & Sign APK"):
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
with zipfile.ZipFile("uploaded_project.zip", "r") as zip_ref:
|
| 48 |
-
zip_ref.extractall("project_extract")
|
| 49 |
-
progress_bar.progress(20)
|
| 50 |
-
|
| 51 |
-
# 3. Find Root
|
| 52 |
-
project_root = None
|
| 53 |
-
for root, dirs, files in os.walk("project_extract"):
|
| 54 |
-
if "gradlew" in files:
|
| 55 |
-
project_root = root
|
| 56 |
-
break
|
| 57 |
-
|
| 58 |
-
if not project_root:
|
| 59 |
-
st.error("β ZIP file mein 'gradlew' nahi mila.")
|
| 60 |
-
else:
|
| 61 |
-
# 4. Permissions
|
| 62 |
-
status_text.info("Setting up Gradle...")
|
| 63 |
-
gradlew_path = os.path.join(project_root, "gradlew")
|
| 64 |
-
subprocess.run(["chmod", "+x", gradlew_path])
|
| 65 |
-
|
| 66 |
-
# 5. Build
|
| 67 |
-
status_text.info("Building APK... (Wait karein)")
|
| 68 |
-
# Hum assembleRelease chala rahe hain
|
| 69 |
-
build_cmd = ["./gradlew", "assembleRelease", "--no-daemon", "--stacktrace"]
|
| 70 |
-
|
| 71 |
-
try:
|
| 72 |
-
result = subprocess.run(
|
| 73 |
-
build_cmd,
|
| 74 |
-
cwd=project_root,
|
| 75 |
-
stdout=subprocess.PIPE,
|
| 76 |
-
stderr=subprocess.PIPE,
|
| 77 |
-
text=True
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
progress_bar.progress(60)
|
| 81 |
-
|
| 82 |
-
if result.returncode == 0:
|
| 83 |
-
status_text.info("β
Build Successful! Signing APK...")
|
| 84 |
-
|
| 85 |
-
# 6. Find Unsigned APK
|
| 86 |
-
unsigned_apk = None
|
| 87 |
-
for root, dirs, files in os.walk(project_root):
|
| 88 |
-
for file in files:
|
| 89 |
-
if file.endswith(".apk") and "release" in file:
|
| 90 |
-
unsigned_apk = os.path.join(root, file)
|
| 91 |
-
break
|
| 92 |
-
|
| 93 |
-
if unsigned_apk:
|
| 94 |
-
# 7. SIGNING (Direct System Commands)
|
| 95 |
-
keystore = generate_keystore()
|
| 96 |
-
signed_apk_name = "Installable_App.apk"
|
| 97 |
-
aligned_apk = "aligned.apk"
|
| 98 |
|
| 99 |
-
|
| 100 |
-
try:
|
| 101 |
-
subprocess.run(["zipalign", "-v", "-p", "4", unsigned_apk, aligned_apk],
|
| 102 |
-
stdout=subprocess.DEVNULL, check=True)
|
| 103 |
-
apk_to_sign = aligned_apk
|
| 104 |
-
except:
|
| 105 |
-
# Agar align fail ho, to original use kar lo
|
| 106 |
-
apk_to_sign = unsigned_apk
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
label="β¬οΈ Download Installable APK",
|
| 126 |
-
data=f,
|
| 127 |
-
file_name=signed_apk_name,
|
| 128 |
-
mime="application/vnd.android.package-archive"
|
| 129 |
-
)
|
| 130 |
-
else:
|
| 131 |
-
st.error("Build hui magar Signing fail hogayi.")
|
| 132 |
-
st.text_area("Signing Log", sign_result.stderr)
|
| 133 |
-
else:
|
| 134 |
-
st.error("APK file generate nahi hui.")
|
| 135 |
-
|
| 136 |
else:
|
| 137 |
-
st.error("β Build Failed!")
|
| 138 |
-
st.text_area("Error Log",
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
if os.path.exists("uploaded_project.zip"):
|
| 144 |
-
os.remove("uploaded_project.zip")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os, zipfile, subprocess, shutil
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
st.set_page_config(page_title="Android Build Server", layout="centered")
|
| 5 |
+
st.title("π± Android APK Generator")
|
| 6 |
+
st.markdown("Flutter ya Native Android β dono projects ka APK banayega aur **Auto-Sign** karega.")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def generate_keystore():
|
| 9 |
+
kp = "debug.keystore"
|
| 10 |
+
if not os.path.exists(kp):
|
| 11 |
+
subprocess.run([
|
| 12 |
+
"keytool","-genkey","-v","-keystore",kp,"-storepass","android",
|
| 13 |
+
"-alias","androiddebugkey","-keypass","android","-keyalg","RSA",
|
| 14 |
+
"-keysize","2048","-validity","10000",
|
| 15 |
+
"-dname","CN=AndroidDebug,O=Android,C=US"
|
| 16 |
+
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 17 |
+
return kp
|
| 18 |
+
|
| 19 |
+
def find_apk(d):
|
| 20 |
+
for r,_,fs in os.walk(d):
|
| 21 |
+
for f in fs:
|
| 22 |
+
if f.endswith(".apk") and "release" in f.lower():
|
| 23 |
+
return os.path.join(r,f)
|
| 24 |
+
return None
|
| 25 |
|
| 26 |
+
def sign_apk(unsigned):
|
| 27 |
+
al = "aligned.apk"; sg = "Installable_App.apk"
|
| 28 |
+
try:
|
| 29 |
+
subprocess.run(["zipalign","-v","-p","4",unsigned,al],
|
| 30 |
+
stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,check=True)
|
| 31 |
+
target = al
|
| 32 |
+
except:
|
| 33 |
+
target = unsigned
|
| 34 |
+
generate_keystore()
|
| 35 |
+
r = subprocess.run([
|
| 36 |
+
"apksigner","sign","--ks","debug.keystore","--ks-pass","pass:android",
|
| 37 |
+
"--out",sg,target
|
| 38 |
+
], capture_output=True, text=True)
|
| 39 |
+
return sg if r.returncode == 0 else None
|
| 40 |
+
f = st.file_uploader("Project ZIP Upload Karein", type=["zip"])
|
| 41 |
|
| 42 |
+
if f is not None:
|
| 43 |
if st.button("π¨ Create & Sign APK"):
|
| 44 |
+
sts = st.empty(); bar = st.progress(0)
|
| 45 |
+
if os.path.exists("project_extract"): shutil.rmtree("project_extract")
|
| 46 |
+
os.makedirs("project_extract")
|
| 47 |
+
sts.info("π¦ ZIP extract ho raha hai...")
|
| 48 |
+
with open("uploaded_project.zip","wb") as w: w.write(f.getbuffer())
|
| 49 |
+
with zipfile.ZipFile("uploaded_project.zip","r") as z: z.extractall("project_extract")
|
| 50 |
+
bar.progress(15)
|
| 51 |
+
|
| 52 |
+
flutter_root = None; native_root = None
|
| 53 |
+
for r,_,fs in os.walk("project_extract"):
|
| 54 |
+
if "pubspec.yaml" in fs and not flutter_root: flutter_root = r
|
| 55 |
+
if "gradlew" in fs and not native_root: native_root = r
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
unsigned = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
if flutter_root:
|
| 60 |
+
sts.info("π¦ Flutter project β flutter build apk chal raha hai...")
|
| 61 |
+
bar.progress(20)
|
| 62 |
+
with open(os.path.join(flutter_root,"android","local.properties"),"w") as lf:
|
| 63 |
+
lf.write(f"sdk.dir={os.environ.get('ANDROID_HOME','/opt/android-sdk')}\n")
|
| 64 |
+
lf.write(f"flutter.sdk={os.environ.get('FLUTTER_HOME','/opt/flutter')}\n")
|
| 65 |
+
lf.write("flutter.buildMode=release\n")
|
| 66 |
+
r1 = subprocess.run(["flutter","pub","get"], cwd=flutter_root, capture_output=True, text=True)
|
| 67 |
+
if r1.returncode != 0:
|
| 68 |
+
st.error("β flutter pub get fail!"); st.text(r1.stderr[-2000:])
|
| 69 |
+
else:
|
| 70 |
+
bar.progress(35)
|
| 71 |
+
r2 = subprocess.run(["flutter","build","apk","--release"], cwd=flutter_root, capture_output=True, text=True)
|
| 72 |
+
bar.progress(85)
|
| 73 |
+
if r2.returncode == 0:
|
| 74 |
+
apk_dir = os.path.join(flutter_root,"build","app","outputs","flutter-apk")
|
| 75 |
+
unsigned = find_apk(apk_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
else:
|
| 77 |
+
st.error("β Flutter Build Failed!")
|
| 78 |
+
st.text_area("Error Log", r2.stderr[-3000:], height=250)
|
| 79 |
+
|
| 80 |
+
elif native_root:
|
| 81 |
+
sts.info("π© Native Android project β gradlew assembleRelease chal raha hai...")
|
| 82 |
+
bar.progress(20)
|
| 83 |
+
with open(os.path.join(native_root,"local.properties"),"w") as lf:
|
| 84 |
+
lf.write(f"sdk.dir={os.environ.get('ANDROID_HOME','/opt/android-sdk')}\n")
|
| 85 |
+
subprocess.run(["chmod","+x",os.path.join(native_root,"gradlew")])
|
| 86 |
+
bar.progress(25)
|
| 87 |
+
r = subprocess.run(["./gradlew","assembleRelease","--no-daemon","--stacktrace"],
|
| 88 |
+
cwd=native_root, capture_output=True, text=True)
|
| 89 |
+
bar.progress(85)
|
| 90 |
+
if r.returncode == 0:
|
| 91 |
+
unsigned = find_apk(native_root)
|
| 92 |
+
else:
|
| 93 |
+
st.error("β Gradle Build Failed!")
|
| 94 |
+
st.text_area("Error Log", r.stderr[-3000:], height=250)
|
| 95 |
+
else:
|
| 96 |
+
st.error("β ZIP mein na pubspec.yaml mila, na gradlew.")
|
| 97 |
+
|
| 98 |
+
if unsigned and os.path.exists(unsigned):
|
| 99 |
+
sts.info("π APK sign ho raha hai...")
|
| 100 |
+
sg = sign_apk(unsigned)
|
| 101 |
+
if sg:
|
| 102 |
+
bar.progress(100); sts.success("π APK Ready!")
|
| 103 |
+
with open(sg,"rb") as dl:
|
| 104 |
+
st.download_button("β¬οΈ Download APK", data=dl, file_name=sg, mime="application/vnd.android.package-archive")
|
| 105 |
+
else:
|
| 106 |
+
st.error("Signing fail ho gayi.")
|
| 107 |
|
| 108 |
+
if os.path.exists("uploaded_project.zip"): os.remove("uploaded_project.zip")
|
|
|