-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathktroid.py
More file actions
executable file
·1522 lines (1251 loc) · 53.6 KB
/
ktroid.py
File metadata and controls
executable file
·1522 lines (1251 loc) · 53.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
ktroid: A CLI tool to generate, build, clean, and sign pure native Android projects.
"""
import argparse
import os
import shutil
import subprocess
import sys
import re
import urllib.request
import zipfile
import ssl
# Constants (Defaults)
DEFAULT_CONFIG = {
"java_version": "17",
"agp_version": "8.13.2",
"gradle_version": "9.3.1",
"kotlin_version": "2.2.21",
"compile_sdk": "35",
"min_sdk": "21",
"target_sdk": "35",
"build_tools_version": "35.0.0"
}
# Download URLs
CMD_TOOLS_URL = "https://dl.google.com/android/repository/commandlinetools-linux-14742923_latest.zip"
GRADLE_dist_URL = "https://services.gradle.org/distributions/gradle-9.3.1-bin.zip"
# ANSI Colors
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def print_success(msg):
print(f"{Colors.OKGREEN}{msg}{Colors.ENDC}")
def print_error(msg):
print(f"{Colors.FAIL}{msg}{Colors.ENDC}")
def print_warning(msg):
print(f"{Colors.WARNING}{msg}{Colors.ENDC}")
def print_info(msg):
print(f"{Colors.OKCYAN}{msg}{Colors.ENDC}")
def get_script_dir():
return os.path.dirname(os.path.realpath(__file__))
def get_config_dir():
return get_script_dir()
def load_config():
"""Load config from config.json in script dir or return defaults."""
config_path = os.path.join(get_config_dir(), "config.json")
config = DEFAULT_CONFIG.copy()
if os.path.exists(config_path):
try:
import json
with open(config_path, 'r') as f:
user_config = json.load(f)
config.update(user_config)
except Exception as e:
print_warning(f"Failed to load config file: {e}. Using defaults.")
return config
CONFIG = load_config()
def get_template_path(filename):
return os.path.join(get_script_dir(), 'templates', filename)
def run_command(command, cwd=None, show_output=True):
"""Run a shell command."""
try:
process = subprocess.Popen(
command,
cwd=cwd,
stdout=None if show_output else subprocess.PIPE,
stderr=None if show_output else subprocess.PIPE,
shell=True,
text=True
)
stdout, stderr = process.communicate()
if process.returncode != 0:
if not show_output and stderr:
print_error(f"Command failed: {command}")
print_error(f"Error output:\n{stderr}")
return False
return True
except Exception as e:
print_error(f"Execution failed: {e}")
return False
def check_env():
"""Verify the environment requirements."""
print_info("Checking environment...")
# Check JDK
try:
result = subprocess.run(['java', '-version'], capture_output=True, text=True)
version_output = result.stderr
if "version" in version_output:
print_success(f"[OK] Java found: {version_output.splitlines()[0]}")
req_java = CONFIG['java_version']
if req_java not in version_output and f"1.{req_java}" not in version_output:
print_warning(f"[WARN] Java {req_java} is recommended. Found version might differ.")
else:
print_error("[ERR] Java not found or version output parse error.")
except FileNotFoundError:
print_error("[ERR] Java not found in PATH.")
# Check ANDROID_HOME
android_home = os.environ.get('ANDROID_HOME')
if android_home:
print_success(f"[OK] ANDROID_HOME set to: {android_home}")
# Check platforms
platforms_dir = os.path.join(android_home, 'platforms')
if os.path.exists(platforms_dir):
platforms = os.listdir(platforms_dir)
print_success(f"[OK] Android platforms found: {', '.join(platforms)}")
else:
print_error("[ERR] $ANDROID_HOME/platforms directory not found.")
else:
print_error("[ERR] ANDROID_HOME environment variable is NOT set.")
# Check ADB
if shutil.which("adb"):
print_success("[OK] adb found.")
elif android_home and os.path.exists(os.path.join(android_home, "platform-tools", "adb")):
print_success(f"[OK] adb found in platform-tools (not in PATH).")
else:
print_error("[ERR] adb not found.")
# Check Gradle (System)
if shutil.which("gradle"):
print_success("[OK] System Gradle found (can be used to bootstrap wrapper).")
else:
print_warning("[WARN] System Gradle not found. 'ktroid create' requires gradle to generate the wrapper.")
# Check current directory wrapper
if os.path.exists("gradlew"):
print_success("[OK] Local Gradle wrapper (gradlew) found in current directory.")
def generate_project_structure(project_dir, project_name, package_name):
"""Generates the project files in the given directory."""
# 2. Create Directory Structure
app_dir = os.path.join(project_dir, 'app')
src_main_dir = os.path.join(app_dir, 'src', 'main')
java_dir = os.path.join(src_main_dir, 'java', *package_name.split('.'))
res_dir = os.path.join(src_main_dir, 'res')
values_dir = os.path.join(res_dir, 'values')
xml_dir = os.path.join(res_dir, 'xml')
drawable_dir = os.path.join(res_dir, 'drawable')
os.makedirs(java_dir, exist_ok=True)
os.makedirs(values_dir, exist_ok=True)
os.makedirs(xml_dir, exist_ok=True)
os.makedirs(drawable_dir, exist_ok=True)
# 3. Copy/Render Templates
def render_template(template_name, dest_path):
with open(get_template_path(template_name), 'r') as f:
content = f.read()
# Replacements
replacements = {
'{project_name}': project_name,
'{package_name}': package_name,
'{package_path}': package_name.replace('.', '/'),
'8.13.2': CONFIG['agp_version'],
'2.2.21': CONFIG['kotlin_version'],
'{agp_version}': CONFIG['agp_version'],
'{kotlin_version}': CONFIG['kotlin_version'],
'{compile_sdk}': CONFIG['compile_sdk'],
'{min_sdk}': CONFIG['min_sdk'],
'{target_sdk}': CONFIG['target_sdk'],
'{version_code}': "1",
'{version_name}': "1.0",
'{java_version}': CONFIG['java_version']
}
for k, v in replacements.items():
content = content.replace(k, str(v))
with open(dest_path, 'w') as f:
f.write(content)
render_template('settings.gradle', os.path.join(project_dir, 'settings.gradle'))
render_template('root_build.gradle', os.path.join(project_dir, 'build.gradle'))
render_template('gitignore', os.path.join(project_dir, '.gitignore'))
render_template('gradle.properties', os.path.join(project_dir, 'gradle.properties'))
render_template('project_readme.md', os.path.join(project_dir, 'README.md'))
render_template('app_build.gradle', os.path.join(app_dir, 'build.gradle'))
render_template('proguard-rules.pro', os.path.join(app_dir, 'proguard-rules.pro'))
render_template('AndroidManifest.xml', os.path.join(src_main_dir, 'AndroidManifest.xml'))
render_template('MainActivity.kt', os.path.join(java_dir, 'MainActivity.kt'))
render_template('colors.xml', os.path.join(values_dir, 'colors.xml'))
render_template('strings.xml', os.path.join(values_dir, 'strings.xml'))
render_template('themes.xml', os.path.join(values_dir, 'themes.xml'))
render_template('data_extraction_rules.xml', os.path.join(xml_dir, 'data_extraction_rules.xml'))
render_template('backup_rules.xml', os.path.join(xml_dir, 'backup_rules.xml'))
# Splash & Logo
render_template('splash_background.xml', os.path.join(drawable_dir, 'splash_background.xml'))
# Copy Logo
logo_src = os.path.join(get_script_dir(), 'img', 'logo.png')
if os.path.exists(logo_src):
shutil.copy(logo_src, os.path.join(drawable_dir, 'logo.png'))
print_info("Applied custom logo and splash screen.")
else:
print_warning("Warning: img/logo.png not found. App icon might be missing.")
# 4. Generate Wrapper
print_info("Generating Gradle wrapper...")
if shutil.which("gradle"):
cmd = f"gradle wrapper --gradle-version {CONFIG['gradle_version']}"
if not run_command(cmd, cwd=project_dir, show_output=False):
print_warning("Warning: Failed to generate gradle wrapper.")
else:
print_error("Error: System 'gradle' not found. Cannot generate wrapper offline without it.")
print_success(f"Project '{project_name}' configured successfully.")
def cmd_create(args):
"""Create a new Android project."""
project_name = args.project_name
if args.package_name:
package_name = args.package_name
else:
package_name = f"com.example.{project_name.lower()}"
project_dir = os.path.join(os.getcwd(), project_name)
if os.path.exists(project_dir):
print_error(f"Error: Directory '{project_name}' already exists.")
sys.exit(1)
print_info(f"Creating project '{project_name}' at {project_dir}...")
print_info(f"Package: {package_name}")
os.makedirs(project_dir)
generate_project_structure(project_dir, project_name, package_name)
def cmd_init(args):
"""Initialize a project in the current directory."""
cwd = os.getcwd()
default_name = os.path.basename(cwd)
# Interactive Prompts
project_name = input(f"Project Name [{default_name}]: ").strip() or default_name
default_package = f"com.example.{project_name.lower()}"
package_name = input(f"Package Name [{default_package}]: ").strip() or default_package
print_info(f"Initializing project '{project_name}' in current directory...")
# Safety Check: Warn if directory is clear
if os.listdir(cwd):
print_warning("Warning: Current directory is NOT empty.")
confirm = input("Continue anyway? (y/n): ")
if confirm.lower() != 'y':
print_info("Aborting.")
return
generate_project_structure(cwd, project_name, package_name)
def cmd_build(args):
"""Run build commands using glob wrapper."""
if not os.path.exists("./gradlew"):
print_error("Error: gradlew not found. Are you in the project root?")
sys.exit(1)
# Ensure executable
os.chmod("./gradlew", 0o755)
suffix = args.action
cmd = ""
if suffix == "debug":
cmd = "./gradlew assembleDebug"
elif suffix == "release":
cmd = "./gradlew assembleRelease"
elif suffix == "bundle":
cmd = "./gradlew bundleRelease"
else:
# Default build (assembleDebug)
cmd = "./gradlew assembleDebug"
print_info(f"Running: {cmd}")
if run_command(cmd):
print_success("Build successful.")
# Print output paths logic could be improved with find, but simple success msg is start.
if suffix == "debug" or suffix == "build":
print_success(f"Output: app/build/outputs/apk/debug/app-debug.apk")
elif suffix == "release":
print_success(f"Output: app/build/outputs/apk/release/app-release-unsigned.apk (or signed if configured)")
elif suffix == "bundle":
print_success(f"Output: app/build/outputs/bundle/release/app-release.aab")
# Verify signature if release
if suffix == "release":
out_apk = "app/build/outputs/apk/release/app-release-unsigned.apk"
# Note: If signed, AGP might name it differently like input name.
# Actually standard AGP with signingConfig produces 'app-release.apk'
if os.path.exists("app/build/outputs/apk/release/app-release.apk"):
out_apk = "app/build/outputs/apk/release/app-release.apk"
if os.path.exists(out_apk):
verify_apk(out_apk)
else:
print_warning(f"Could not find APK to verify at {out_apk}")
else:
print_error("Build failed.")
sys.exit(1)
def verify_apk(apk_path):
"""Verify APK signature using apksigner or jarsigner."""
print_info(f"Verifying signature for: {os.path.basename(apk_path)}")
# Try apksigner (Best)
# Usually in $ANDROID_HOME/build-tools/<version>/apksigner
apksigner = shutil.which("apksigner")
if not apksigner and os.environ.get("ANDROID_HOME"):
# Try to find it manually
bt_dir = os.path.join(os.environ["ANDROID_HOME"], "build-tools")
if os.path.exists(bt_dir):
versions = sorted(os.listdir(bt_dir))
if versions:
candidate = os.path.join(bt_dir, versions[-1], "apksigner")
if os.path.exists(candidate):
apksigner = candidate
verified = False
if apksigner:
cmd = f"{apksigner} verify --verbose {apk_path}"
# We need output
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.returncode == 0:
print_success("[OK] APK Verified (apksigner).")
verified = True
else:
print_error(f"[ERR] Verification failed: {result.stderr}")
else:
# Fallback to jarsigner
print_info("apksigner not found. Falling back to jarsigner...")
if shutil.which("jarsigner"):
cmd = f"jarsigner -verify -verbose -certs {apk_path}"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if "jar verified" in result.stdout:
print_success("[OK] APK Verified (jarsigner).")
verified = True
if "CN=Android Debug" in result.stdout:
print_warning("[WARN] Signed with DEBUG key.")
else:
print_error("[ERR] Verification failed.")
else:
print_warning("[WARN] Neither apksigner nor jarsigner found. Cannot verify.")
if not verified:
print_error("WARNING: App is NOT signed properly.")
def cmd_clean(args):
"""Run gradlew clean."""
if not os.path.exists("./gradlew"):
print_error("Error: gradlew not found.")
sys.exit(1)
os.chmod("./gradlew", 0o755)
print_info("Running clean...")
run_command("./gradlew clean")
print_success("Clean complete.")
def cmd_signing(args):
"""Configure signing."""
print_info("Configuring Signing...")
props_file = "signing.properties"
if os.path.exists(props_file):
print_warning(f"'{props_file}' already exists.")
overwrite = input("Overwrite? (y/n): ")
if overwrite.lower() != 'y':
return
keystore_path = input("Enter path to Keystore (leave empty to generate new): ").strip()
store_password = ""
key_alias = ""
key_password = ""
if not keystore_path:
print_info("Generating new keystore...")
keystore_path = "release.keystore"
key_alias = "key0"
# Ideally use getpass.getpass()
import getpass
pwd = getpass.getpass("Enter new keystore password: ")
pwd_confirm = getpass.getpass("Confirm password: ")
if pwd != pwd_confirm:
print_error("Passwords do not match.")
return
store_password = pwd
key_password = pwd
# dname
dname = "CN=Android Dev, OU=Ktroid, O=Ktroid, L=Unknown, S=Unknown, C=US"
cmd = (f'keytool -genkey -v -keystore {keystore_path} -alias {key_alias} -keyalg RSA '
f'-keysize 2048 -validity 10000 -storepass {store_password} -keypass {key_password} '
f'-dname "{dname}"')
if run_command(cmd):
print_success(f"Keystore generated at {keystore_path}")
else:
print_error("Failed to generate keystore. Ensure 'keytool' (JDK) is in PATH.")
return
else:
import getpass
store_password = getpass.getpass("Enter keystore password: ")
key_alias = input("Enter key alias: ")
key_password = getpass.getpass("Enter key password: ")
# Write properties
with open(props_file, 'w') as f:
f.write(f"storeFile={keystore_path}\n")
f.write(f"storePassword={store_password}\n")
f.write(f"keyAlias={key_alias}\n")
f.write(f"keyPassword={key_password}\n")
print_success(f"Signing configured in {props_file}. You can now run 'ktroid build release'.")
def cmd_info(args):
"""Extract info from build.gradle."""
build_file = "app/build.gradle"
if not os.path.exists(build_file):
print("Error: app/build.gradle not found.")
sys.exit(1)
with open(build_file, 'r') as f:
content = f.read()
def find_val(key):
match = re.search(fr'{key}\s+"?([^"\n]+)"?', content)
if match: return match.group(1)
# Try = style
match = re.search(fr'{key}\s*=\s*"?([^"\n]+)"?', content)
if match: return match.group(1)
return "Unknown"
print("Project Info:")
print(f" Application ID: {find_val('applicationId')}")
print(f" Version Code: {find_val('versionCode')}")
print(f" Version Name: {find_val('versionName')}")
print(f" Min SDK: {find_val('minSdk')}")
print(f" Target SDK: {find_val('targetSdk')}")
print(f" Compile SDK: {find_val('compileSdk')}")
def cmd_config(args):
"""Generate default configuration file."""
config_dir = get_config_dir()
config_path = os.path.join(config_dir, "config.json")
if not os.path.exists(config_dir):
os.makedirs(config_dir)
if os.path.exists(config_path) and not args.init:
print_info(f"Configuration file exists at: {config_path}")
with open(config_path, 'r') as f:
print(f.read())
print_info(f"\nEdit this file to update SDK/AGP versions without updating the tool.")
else:
if os.path.exists(config_path):
print_warning("Overwriting existing configuration...")
import json
with open(config_path, 'w') as f:
json.dump(DEFAULT_CONFIG, f, indent=4)
print_success(f"Configuration file created at: {config_path}")
def download_progress_hook(count, block_size, total_size):
"""Simple progress bar hook."""
global start_time
if count == 0:
start_time = time.time()
return
duration = time.time() - start_time
progress_size = int(count * block_size)
percent = int(count * block_size * 100 / total_size)
# Simple bar: [===========> ]
bar_len = 30
filled_len = int(bar_len * percent / 100)
bar = '=' * filled_len + '>' + ' ' * (bar_len - filled_len - 1)
# MB conversion
size_mb = total_size / (1024 * 1024)
prog_mb = progress_size / (1024 * 1024)
sys.stdout.write(f"\rDownloading: {prog_mb:.1f} MB / {size_mb:.1f} MB [{bar}] {percent}%")
sys.stdout.flush()
import time # Need time for progress bar
def cmd_setup(args):
"""Setup Android SDK and Gradle with interactive feedback."""
print_info("=== ktroid Setup Wizard ===")
# 1. Define Dependencies
print_info("\nRequired Dependencies:")
print("1. Java JDK 17+ (Required to run Gradle/Android tools)")
print("2. Gradle (Build Tool)")
print("3. Android Command Line Tools (sdkmanager, apksigner, etc.)")
print("----------------------------------------------------------")
dest_root = os.path.expanduser("~/android")
# --- Helper Functions ---
def install_component(name, url, dest_folder, verify_func):
# 1. Check
sys.stdout.write(f"Checking {name}...")
sys.stdout.flush()
if verify_func(silent=True):
sys.stdout.write(f"\r[{name}] Status: INSTALLED [ OK ] \n")
return True
sys.stdout.write(f"\r[{name}] Status: MISSING \n")
# 2. Prompt
print_info(f"-> {name} is required.")
print(f" Download URL: {url}")
print(f" Target: {dest_folder}")
confirm = input(f" Download and install {name}? (y/n): ")
if confirm.lower() != 'y':
print_warning(f" Skipping {name}.")
return False
# 3. Setup Dir
os.makedirs(dest_root, exist_ok=True)
# 4. Download
filename = url.split("/")[-1]
filepath = os.path.join(dest_root, filename)
if not os.path.exists(filepath):
print(f" Downloading {filename}...")
try:
urllib.request.urlretrieve(url, filepath, download_progress_hook)
print() # Newline after progress
except Exception as e:
print_error(f"\n Download failed: {e}")
return False
print(f" Extracting {filename}...")
try:
with zipfile.ZipFile(filepath, 'r') as zip_ref:
zip_ref.extractall(dest_folder)
print_success(" Extraction complete. [ OK ]")
except Exception as e:
print_error(f" Extraction failed: {e}")
return False
return True
# --- 0. Java Check (Prerequisite) ---
sys.stdout.write("Checking Java JDK...")
sys.stdout.flush()
if shutil.which("java"):
sys.stdout.write("\r[Java JDK] Status: INSTALLED [ OK ] \n")
else:
sys.stdout.write("\r[Java JDK] Status: MISSING (Please install JDK 17 manualy) \n")
print_warning(" Warning: Java is required for Gradle and Android Tools.")
# --- 1. Gradle Setup ---
def verify_gradle(silent=False):
# 1. Check System PATH
if shutil.which("gradle"):
return True
# 2. Check ~/android
if os.path.exists(dest_root):
g_dirs = [d for d in os.listdir(dest_root) if d.startswith("gradle-") and os.path.isdir(os.path.join(dest_root, d))]
if g_dirs:
# We found a folder, assume it matches if it has bin/gradle
latest_gradle = sorted(g_dirs)[-1]
gradle_bin = os.path.join(dest_root, latest_gradle, "bin", "gradle")
if os.path.exists(gradle_bin):
return True
return False
if install_component("Gradle", GRADLE_DIST_URL, dest_root, verify_gradle):
# Post-install verify
if not verify_gradle(silent=True) and not shutil.which("gradle"):
# It might be installed but not in PATH for this session
pass
# --- 2. CommandLine Tools Setup ---
def verify_cmdline(silent=False):
# 1. Check System PATH
if shutil.which("sdkmanager"):
return True
# 2. Check ~/android
sdkmanager = os.path.join(dest_root, "cmdline-tools", "latest", "bin", "sdkmanager")
if os.path.exists(sdkmanager): return True
return False
if install_component("Android SDK Tools", CMD_TOOLS_URL, dest_root, verify_cmdline):
# Fix folder structure logic
base_cmd = os.path.join(dest_root, "cmdline-tools")
original_bin = os.path.join(base_cmd, "bin") # Extracted as cmdline-tools/bin
if os.path.exists(original_bin):
print_info(" Structuring SDK correctly (cmdline-tools/latest)...")
latest_dir = os.path.join(base_cmd, "latest")
temp_dir = os.path.join(base_cmd, "temp_move")
if not os.path.exists(latest_dir):
os.rename(base_cmd, temp_dir)
os.makedirs(base_cmd)
os.rename(temp_dir, latest_dir)
print_success(" Structure fixed. [ OK ]")
# --- Final Path Exports ---
print("\n------------------------------------------------")
print_info("Setup Summary & Exports")
print("------------------------------------------------")
bashrc_content = []
# Check what we have found/installed to print exports
# 1. Android Home
if os.path.exists(dest_root):
print(f"export ANDROID_HOME=\"{dest_root}\"")
bashrc_content.append(f'export ANDROID_HOME="{dest_root}"')
# 2. Cmdline Tools
cmd_bin = os.path.join(dest_root, "cmdline-tools", "latest", "bin")
if os.path.exists(cmd_bin):
print(f"export PATH=\"$PATH:{cmd_bin}\"")
bashrc_content.append(f'export PATH="$PATH:{cmd_bin}"')
# 3. Platform Tools
plat_bin = os.path.join(dest_root, "platform-tools")
if os.path.exists(plat_bin):
print(f"export PATH=\"$PATH:{plat_bin}\"")
bashrc_content.append(f'export PATH="$PATH:{plat_bin}"')
# 4. Gradle
g_dirs = []
if os.path.exists(dest_root):
g_dirs = [d for d in os.listdir(dest_root) if d.startswith("gradle-")]
if g_dirs:
g_bin = os.path.join(dest_root, sorted(g_dirs)[-1], "bin")
print(f"export PATH=\"$PATH:{g_bin}\"")
bashrc_content.append(f'export PATH="$PATH:{g_bin}"')
print("\nTo make these permanent, add them to ~/.bashrc")
# --- Advanced Features: Dictionaries ---
COMMON_DEPS = {
"retrofit": "com.squareup.retrofit2:retrofit:2.9.0",
"gson": "com.google.code.gson:gson:2.10.1",
"glide": "com.github.bumptech.glide:glide:4.16.0",
"coil": "io.coil-kt:coil-compose:2.5.0",
"navigation": "androidx.navigation:navigation-compose:2.7.5",
"lifecycle": "androidx.lifecycle:lifecycle-runtime-ktx:2.6.2",
"coroutines": "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3",
"okhttp": "com.squareup.okhttp3:okhttp:4.12.0",
"material": "com.google.android.material:material:1.11.0",
"room": "androidx.room:room-runtime:2.6.1"
}
COMMON_PERMS = {
"internet": "android.permission.INTERNET",
"camera": "android.permission.CAMERA",
"storage": "android.permission.WRITE_EXTERNAL_STORAGE",
"read_storage": "android.permission.READ_EXTERNAL_STORAGE",
"location": "android.permission.ACCESS_FINE_LOCATION",
"background_location": "android.permission.ACCESS_BACKGROUND_LOCATION",
"network_state": "android.permission.ACCESS_NETWORK_STATE",
"wifi_state": "android.permission.ACCESS_WIFI_STATE",
"bluetooth": "android.permission.BLUETOOTH",
"record_audio": "android.permission.RECORD_AUDIO"
}
def cmd_dep(args):
"""Manage dependencies."""
if not args.name:
print_info("Available Shortcuts:")
for k, v in COMMON_DEPS.items():
print(f" {k:<12} -> {v}")
print("\nUsage:")
print(" ktroid dep <shortcut> (e.g., ktroid dep glide)")
print(" ktroid dep <coord> (e.g., ktroid dep com.foo:bar:1.2)")
return
dep_str = COMMON_DEPS.get(args.name.lower(), args.name)
build_file = "app/build.gradle"
if not os.path.exists(build_file):
print_error("Error: app/build.gradle not found.")
return
print_info(f"Adding dependency: {dep_str}")
with open(build_file, "r") as f:
lines = f.readlines()
new_lines = []
in_dependencies = False
added = False
for line in lines:
new_lines.append(line)
if "dependencies {" in line:
in_dependencies = True
if in_dependencies and "}" in line and not added:
# Add before closing brace
# Remove the brace we just added to strict logic
new_lines.pop()
new_lines.append(f" implementation '{dep_str}'\n")
new_lines.append(line)
added = True
in_dependencies = False
with open(build_file, "w") as f:
f.writelines(new_lines)
print_success("Dependency added successfully.")
def cmd_perm(args):
"""Add permissions."""
if not args.name:
print_info("Common Permissions:")
for k, v in COMMON_PERMS.items():
print(f" {k:<18} -> {v}")
print("\nUsage: ktroid perm <name>")
return
perm_name = COMMON_PERMS.get(args.name.lower())
if not perm_name:
# Assume user knows what they are doing if not in list
if "." in args.name:
perm_name = args.name
else:
print_error(f"Unknown permission shortcut: {args.name}")
return
manifest_file = "app/src/main/AndroidManifest.xml"
if not os.path.exists(manifest_file):
print_error("Error: AndroidManifest.xml not found.")
return
print_info(f"Adding permission: {perm_name}")
with open(manifest_file, "r") as f:
lines = f.readlines()
# check if already exists
for line in lines:
if perm_name in line:
print_warning("Permission already exists.")
return
new_lines = []
added = False
for line in lines:
# Insert before <application
if "<application" in line and not added:
new_lines.append(f' <uses-permission android:name="{perm_name}" />\n')
added = True
new_lines.append(line)
with open(manifest_file, "w") as f:
f.writelines(new_lines)
print_success("Permission added.")
def cmd_logo(args):
"""Change app logo with multiple density support."""
src_image = args.path
if not os.path.exists(src_image):
print_error(f"Error: Image '{src_image}' not found.")
return
res_dir = "app/src/main/res"
if not os.path.exists(res_dir):
print_error("Error: Project structure not found (res/ missing).")
return
# Check if PIL/Pillow is available for resizing
try:
from PIL import Image
has_pil = True
except ImportError:
has_pil = False
print_warning("PIL/Pillow not found. Installing single logo without density variants.")
if has_pil:
# Generate multiple densities
densities = {
"mdpi": 48,
"hdpi": 72,
"xhdpi": 96,
"xxhdpi": 144,
"xxxhdpi": 192
}
print_info("Generating app icons for multiple densities...")
try:
img = Image.open(src_image)
# Convert to RGBA if needed
if img.mode != 'RGBA':
img = img.convert('RGBA')
for density, size in densities.items():
# Create density folder
density_dir = os.path.join(res_dir, f"mipmap-{density}")
os.makedirs(density_dir, exist_ok=True)
# Resize and save
resized = img.resize((size, size), Image.Resampling.LANCZOS)
output_path = os.path.join(density_dir, "ic_launcher.png")
resized.save(output_path, "PNG")
print_success(f" ✓ {density}: {size}x{size}px")
# Also copy to drawable for backward compatibility
drawable_dir = os.path.join(res_dir, "drawable")
os.makedirs(drawable_dir, exist_ok=True)
resized_96 = img.resize((96, 96), Image.Resampling.LANCZOS)
resized_96.save(os.path.join(drawable_dir, "logo.png"), "PNG")
print_success("App icon updated for all densities.")
print_info("Note: Clean and rebuild to see changes.")
except Exception as e:
print_error(f"Failed to process image: {e}")
else:
# Fallback: Just copy to drawable
dest_dir = os.path.join(res_dir, "drawable")
os.makedirs(dest_dir, exist_ok=True)
dest_path = os.path.join(dest_dir, "logo.png")
try:
shutil.copy(src_image, dest_path)
print_success(f"Logo updated from {src_image}")
print_info("Install Pillow for automatic multi-density icon generation: pip install Pillow")
except Exception as e:
print_error(f"Failed to copy image: {e}")
def get_connected_devices():
"""Return list of connected devices."""
try:
res = subprocess.run(["adb", "devices"], capture_output=True, text=True)
lines = res.stdout.splitlines()
devices = []
for line in lines[1:]: # Skip header
parts = line.split()
if len(parts) >= 2 and parts[1] == "device":
devices.append(parts[0])
return devices
except:
return []
def cmd_logs(args):
"""Smart Logcat Viewer."""
# 1. Get Package Name (from build.gradle or user input?)
# Parsing build.gradle for applicationId
app_id = None
if os.path.exists("app/build.gradle"):
with open("app/build.gradle", "r") as f:
cnt = f.read()
m = re.search(r'applicationId\s+"?([^"\n]+)"?', cnt)
if m: app_id = m.group(1)
if not app_id:
print_error("Could not find applicationId in app/build.gradle.")
return
print_info(f"Filtering logs for: {app_id}")
# Get PID for app
try:
pid_res = subprocess.run(f"adb shell pidof {app_id}", shell=True, capture_output=True, text=True)
pid = pid_res.stdout.strip()
if not pid:
print_warning("App is not running. Showing all logs containing package name...")
filter_cmd = app_id
else:
print_info(f"PID found: {pid}")
filter_cmd = f" --pid={pid}"
# Run logcat
# Simple colorizer logic is hard in python subprocess pipe loop
# We'll just run adb logcat and let grep handle it or raw
print_success("Ctrl+C to stop.")
# Color command for grep if possible or just raw
# Using grep to filter is better
cmd = f"adb logcat -v time | grep '{app_id}'" if not pid else f"adb logcat -v time --pid={pid}"
# Run
os.system(cmd)
except KeyboardInterrupt:
print()
except Exception as e:
print_error(f"Logcat error: {e}")
def cmd_run(args):
"""Build, Install and Run."""
# 1. Select Device
devices = get_connected_devices()
if not devices:
print_error("No connected devices found. Connect a device via USB or start emulator.")
return
target_device = devices[0]
if len(devices) > 1:
print_info("Multiple devices found:")
for i, d in enumerate(devices):
print(f"{i+1}. {d}")
try:
sel = int(input("Select device (number): "))
target_device = devices[sel-1]
except:
print_error("Invalid selection.")
return
print_info(f"Target: {target_device}")
# 2. Build Debug
print_info("Building Debug APK...")
# Call cmd_build logic? Or invoke gradle directly?
# Better to invoke existing logic.
# Re-using cmd_build args shim
class BuildArgs:
action = "debug"
try:
# Use subprocess to call self? No, just call function if possible.
# But cmd_build expects args object
# Let's just run gradle directly here for simplicity or simulate
if not os.path.exists("./gradlew"):
print_error("gradlew not found.")
return
if not run_command("./gradlew assembleDebug"):
print_error("Build failed.")
return
except Exception as e:
print_error(f"Build Error: {e}")
return
# 3. Install
apk = "app/build/outputs/apk/debug/app-debug.apk"
if not os.path.exists(apk):
print_error("APK not found after build.")
return
print_info("Installing...")
if not run_command(f"adb -s {target_device} install -r {apk}"):
print_error("Install failed.")
return
# 4. Launch
print_info("Launching...")
# Need package name / main activity
# Default: package/.MainActivity