Akwbw commited on
Commit
e6abcb7
Β·
verified Β·
1 Parent(s): cb8b90f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -65
app.py CHANGED
@@ -3,33 +3,17 @@ import os
3
  import zipfile
4
  import subprocess
5
  import shutil
6
- import glob
7
 
8
  # Page Config
9
  st.set_page_config(page_title="Android Build Server", layout="centered")
10
 
11
- st.title("πŸ“± Android APK Generator (Auto-Signed)")
12
- st.markdown("Zip upload karein. Server APK banayega aur **Sign** karke dega taake install ho sakay.")
13
 
14
- # --- HELPER FUNCTION: FIND TOOLS ---
15
- def find_tool(tool_name):
16
- # System path mein dhoondo
17
- tool_path = shutil.which(tool_name)
18
- if tool_path:
19
- return tool_path
20
-
21
- # Agar nahi mila, to Android SDK folder mein dhoondo
22
- sdk_root = os.environ.get("ANDROID_HOME", "/opt/android-sdk")
23
- found_files = glob.glob(f"{sdk_root}/build-tools/*/{tool_name}")
24
- if found_files:
25
- return found_files[-1] # Latest version utha lo
26
- return None
27
-
28
- # --- HELPER FUNCTION: GENERATE KEYSTORE ---
29
  def generate_keystore():
30
  keystore_path = "debug.keystore"
31
  if not os.path.exists(keystore_path):
32
- # Keytool command to create a generic key
33
  cmd = [
34
  "keytool", "-genkey", "-v",
35
  "-keystore", keystore_path,
@@ -44,7 +28,6 @@ def generate_keystore():
44
  subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
45
  return keystore_path
46
 
47
- # --- MAIN UI ---
48
  uploaded_file = st.file_uploader("Project ZIP File Upload Karein", type=["zip"])
49
 
50
  if uploaded_file is not None:
@@ -80,11 +63,9 @@ if uploaded_file is not None:
80
  gradlew_path = os.path.join(project_root, "gradlew")
81
  subprocess.run(["chmod", "+x", gradlew_path])
82
 
83
- # 5. Build (Use assembleRelease for production feel)
84
- # Hum 'assembleRelease' use karenge, jo unsigned APK dega, phir hum usay sign karenge
85
  status_text.info("Building APK... (Wait karein)")
86
-
87
- # Note: Hum 'clean' nahi kar rahay taake jaldi ho, agar error aye to clean add kar lena
88
  build_cmd = ["./gradlew", "assembleRelease", "--no-daemon", "--stacktrace"]
89
 
90
  try:
@@ -99,7 +80,7 @@ if uploaded_file is not None:
99
  progress_bar.progress(60)
100
 
101
  if result.returncode == 0:
102
- status_text.info("βœ… Build Successful! Ab Signing kar rahay hain...")
103
 
104
  # 6. Find Unsigned APK
105
  unsigned_apk = None
@@ -110,51 +91,47 @@ if uploaded_file is not None:
110
  break
111
 
112
  if unsigned_apk:
113
- # 7. SIGNING PROCESS ✍️
114
  keystore = generate_keystore()
115
- apksigner = find_tool("apksigner")
116
-
117
- if apksigner:
118
- signed_apk_name = "production_ready_app.apk"
119
-
120
- # Align APK (Optimization)
121
- zipalign = find_tool("zipalign")
122
- aligned_apk = "aligned.apk"
123
- if zipalign:
124
- subprocess.run([zipalign, "-v", "-p", "4", unsigned_apk, aligned_apk], stdout=subprocess.DEVNULL)
125
- apk_to_sign = aligned_apk
126
- else:
127
- apk_to_sign = unsigned_apk
128
 
129
- # Sign APK
130
- sign_cmd = [
131
- apksigner, "sign",
132
- "--ks", keystore,
133
- "--ks-pass", "pass:android",
134
- "--out", signed_apk_name,
135
- apk_to_sign
136
- ]
137
-
138
- sign_result = subprocess.run(sign_cmd, capture_output=True, text=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
- if sign_result.returncode == 0:
141
- status_text.success("πŸŽ‰ APK Signed & Ready!")
142
- progress_bar.progress(100)
143
-
144
- with open(signed_apk_name, "rb") as f:
145
- st.download_button(
146
- label="⬇️ Download Signed APK",
147
- data=f,
148
- file_name=signed_apk_name,
149
- mime="application/vnd.android.package-archive"
150
- )
151
- else:
152
- st.error("Build pass hui, magar Signing fail hogayi.")
153
- st.text_area("Signing Error", sign_result.stderr)
154
  else:
155
- st.error("Server par 'apksigner' tool nahi mila.")
 
156
  else:
157
- st.error("Build ho gayi magar APK file dhoondne mein masla hua.")
158
 
159
  else:
160
  st.error("❌ Build Failed!")
 
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
  keystore_path = "debug.keystore"
16
  if not os.path.exists(keystore_path):
 
17
  cmd = [
18
  "keytool", "-genkey", "-v",
19
  "-keystore", keystore_path,
 
28
  subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
29
  return keystore_path
30
 
 
31
  uploaded_file = st.file_uploader("Project ZIP File Upload Karein", type=["zip"])
32
 
33
  if uploaded_file is not None:
 
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:
 
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
 
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
+ # Step A: ZipAlign (Important for installation)
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
+ # Step B: APKSigner
109
+ sign_cmd = [
110
+ "apksigner", "sign",
111
+ "--ks", keystore,
112
+ "--ks-pass", "pass:android",
113
+ "--out", signed_apk_name,
114
+ apk_to_sign
115
+ ]
116
+
117
+ sign_result = subprocess.run(sign_cmd, capture_output=True, text=True)
118
+
119
+ if sign_result.returncode == 0:
120
+ status_text.success("πŸŽ‰ APK Signed & Ready to Install!")
121
+ progress_bar.progress(100)
122
 
123
+ with open(signed_apk_name, "rb") as f:
124
+ st.download_button(
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!")