Spaces:
Running
Running
Create utils/apk_process.py
Browse files- utils/apk_process.py +65 -0
utils/apk_process.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
import shutil
|
4 |
+
import zipfile
|
5 |
+
import streamlit as st
|
6 |
+
from utils.apk_debug import debug_apk
|
7 |
+
|
8 |
+
def process_xapk(xapk_path):
|
9 |
+
try:
|
10 |
+
folder = os.path.dirname(xapk_path)
|
11 |
+
name_without_ext = os.path.splitext(os.path.basename(xapk_path))[0]
|
12 |
+
zip_path = os.path.join(folder, f"{name_without_ext}.zip")
|
13 |
+
extract_dir = os.path.join(folder, name_without_ext)
|
14 |
+
|
15 |
+
shutil.move(xapk_path, zip_path)
|
16 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
17 |
+
zip_ref.extractall(extract_dir)
|
18 |
+
|
19 |
+
os.remove(zip_path)
|
20 |
+
|
21 |
+
command = f'java -jar APKEditor.jar m -i "{extract_dir}"'
|
22 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
23 |
+
|
24 |
+
if result.returncode == 0:
|
25 |
+
merged_apk_path = os.path.join(folder, f"{name_without_ext}_merged.apk")
|
26 |
+
st.write(f"Merged APK created at: {merged_apk_path}")
|
27 |
+
signed_apk_path = process_sign(merged_apk_path)
|
28 |
+
return signed_apk_path
|
29 |
+
else:
|
30 |
+
st.error(f"Error merging APK: {result.stderr}")
|
31 |
+
return None
|
32 |
+
|
33 |
+
except Exception as e:
|
34 |
+
st.error(f"Error processing XAPK: {str(e)}")
|
35 |
+
shutil.rmtree(extract_dir, ignore_errors=True)
|
36 |
+
return None
|
37 |
+
|
38 |
+
def process_sign(apk_path):
|
39 |
+
folder = os.path.dirname(apk_path)
|
40 |
+
command = f"java -jar uber-apk-signer.jar --apks {apk_path}"
|
41 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
42 |
+
|
43 |
+
if result.returncode == 0:
|
44 |
+
signed_apk_path = apk_path.replace('.apk', '-aligned-debugSigned.apk')
|
45 |
+
if os.path.exists(signed_apk_path):
|
46 |
+
st.write("APK signing successful!")
|
47 |
+
return signed_apk_path
|
48 |
+
else:
|
49 |
+
st.error("APK signing completed, but the signed file could not be found.")
|
50 |
+
return None
|
51 |
+
else:
|
52 |
+
st.error(f"Error signing APK: {result.stderr}")
|
53 |
+
return None
|
54 |
+
|
55 |
+
def xapk_debug(xapk_path, output_dir):
|
56 |
+
# Process the XAPK file first
|
57 |
+
signed_apk_path = process_xapk(xapk_path)
|
58 |
+
|
59 |
+
if signed_apk_path:
|
60 |
+
# Use the signed APK as input for debugging
|
61 |
+
debug_path = debug_apk(signed_apk_path, output_dir)
|
62 |
+
return debug_path
|
63 |
+
else:
|
64 |
+
st.error("Failed to process XAPK for debugging.")
|
65 |
+
return None
|