Skip to content

Commit

Permalink
Merge pull request #16 from aktsk/fix/error-handling
Browse files Browse the repository at this point in the history
Close #14, #15 Fix error handling&Update old actions
  • Loading branch information
tkmru authored Dec 11, 2024
2 parents e6e4051 + 09e8749 commit 42de585
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 33 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -54,7 +54,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -68,4 +68,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v3
64 changes: 48 additions & 16 deletions apkutil/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ def cmd_set_debuggable(args):

print('Aligning APK by zipalign...')
try:
util.align(apk_path)
result = util.align(apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
return

print('Signing APK by apksigner...')
try:
util.sign(apk_path)
result = util.sign(apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand Down Expand Up @@ -97,15 +101,19 @@ def cmd_set_network(args):

print('Aligning APK by zipalign...')
try:
util.align(apk_path)
result = util.align(apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
return

print('Signing APK by apksigner...')
try:
util.sign(apk_path)
result = util.sign(apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand All @@ -117,7 +125,9 @@ def cmd_set_network(args):
def cmd_all(args):
print('Decoding APK by Apktool...')
try:
util.decode(args.apk_path)
result = util.decode(args.apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand Down Expand Up @@ -153,15 +163,19 @@ def cmd_all(args):

print('Aligning APK by zipalign...')
try:
util.align(apk_path)
result = util.align(apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
return

print('Signing APK by apksigner...')
try:
util.sign(apk_path)
result = util.sign(apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand All @@ -173,7 +187,9 @@ def cmd_all(args):
def cmd_decode(args):
print('Decoding APK by Apktool...')
try:
util.decode(args.apk_path, no_res=args.no_res, no_src=args.no_src)
result = util.decode(args.apk_path, no_res=args.no_res, no_src=args.no_src)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand All @@ -194,23 +210,29 @@ def cmd_build(args):
if args.output is None:
apk_path = args.dir_name + ".patched.apk"
try:
util.build(args.dir_name, apk_path, aapt2=args.aapt2)
result = util.build(args.dir_name, apk_path, aapt2=args.aapt2)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
return

print('Aligning APK by zipalign...')
try:
util.align(apk_path)
result = util.align(apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
return

print('Signing APK by apksigner...')
try:
util.sign(apk_path)
result = util.sign(apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand All @@ -221,7 +243,9 @@ def cmd_build(args):
def cmd_align(args):
print('Aligning APK by zipalign...')
try:
util.align(args.apk_path)
result = util.align(args.apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand All @@ -231,7 +255,9 @@ def cmd_align(args):
def cmd_sign(args):
print('Signing APK by apksigner...')
try:
util.sign(args.apk_path)
result = util.sign(args.apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand All @@ -243,7 +269,9 @@ def cmd_sign(args):
def cmd_info(args):
print('Getting package name by aapt...')
try:
util.get_packagename(args.apk_path)
result = util.get_packagename(args.apk_path)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand All @@ -252,7 +280,9 @@ def cmd_info(args):
def cmd_screenshot(args):
print('Getting a screenshot from connected device...')
try:
file_name = util.get_screenshot()
result = file_name = util.get_screenshot()
if not result:
raise Exception()
print(Fore.CYAN + 'Output: ' + file_name)
except Exception as e:
print(e)
Expand All @@ -262,7 +292,9 @@ def cmd_screenshot(args):
def cmd_pull_apks(args):
print('Pulling APKs from device...')
try:
util.pull_apks(args.keyword)
result = util.pull_apks(args.keyword)
if not result:
raise Exception()
except Exception as e:
print(e)
print(Fore.RED + 'Failed')
Expand Down
36 changes: 24 additions & 12 deletions apkutil/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def pull_apks(keyword):
package_name = get_package_name(keyword)
if not package_name:
print(f"No package found matching keyword: {keyword}")
return
return False

print(f"Package found: {package_name}")

apk_paths = get_apk_paths(package_name)
if not apk_paths:
print(f"No APK paths found for package: {package_name}")
return
return False

pull_apk_files(apk_paths)

Expand All @@ -40,8 +40,10 @@ def pull_apks(keyword):
except (IndexError, FileNotFoundError):
print('adb not found.')
print('Please install Android SDK Build Tools.')
return False
except Exception as e:
print(f"An error occurred: {e}")
return False

def get_package_name(keyword):
adb_path = glob.glob(ANDROID_HOME + '/platform-tools/adb')[0]
Expand Down Expand Up @@ -94,10 +96,13 @@ def decode(apk_path, no_res=False, no_src=False):
# unsupported `apktool d -f`
errs = errs.replace('Use -f switch if you want to overwrite it.', '')
raise Exception(errs)

return True

except FileNotFoundError as e:
print('apktool not found.')
print('Please install apktool')
return False


def build(dir_name, apk_path, aapt2=False):
Expand Down Expand Up @@ -125,6 +130,7 @@ def build(dir_name, apk_path, aapt2=False):
except FileNotFoundError as e:
print('apktool not found.')
print('Please install apktool.')
return False

def align(apk_path):
try:
Expand All @@ -139,10 +145,12 @@ def align(apk_path):
raise Exception(errs)

move('/tmp/apkutil_tmp.aligned.apk', apk_path)
return True

except (IndexError, FileNotFoundError) as e:
print('zipalign not found.')
print('Please install Android SDK Build Tools.')

return False

def sign(apk_path):
home_dir = os.environ['HOME']
Expand All @@ -159,7 +167,7 @@ def sign(apk_path):

except:
print('Please place `~/apkutil.json` containing the keystore information')
return
return False

try:
if not os.path.isfile(apk_path):
Expand All @@ -180,11 +188,15 @@ def sign(apk_path):
print(Fore.CYAN + outs)

if (errs is not None) and (len(errs) != 0):
raise Exception(errs)
print(Fore.RED + errs)
return False

return True

except (IndexError, FileNotFoundError) as e:
print('apksigner not found.')
print('Please install Android SDK Build Tools.')
return False


def get_packagename(apk_path):
Expand All @@ -204,11 +216,13 @@ def get_packagename(apk_path):

if (errs is not None) and (len(errs) != 0):
raise Exception(errs)

return True

except (IndexError, FileNotFoundError) as e:
print('apksigner not found.')
print('Please install Android SDK Build Tools.')

return False

def get_screenshot():
try:
Expand All @@ -229,12 +243,9 @@ def get_screenshot():
pull_cmd = [adb_path]
pull_cmd.append('pull')
pull_cmd.append(screenshot_path)
outs, errs = run_subprocess(pull_cmd)
if (errs is not None) and (len(errs) != 0):
raise Exception(errs)

if (outs is not None) and (len(outs) != 0):
print(outs)
_, errs = run_subprocess(pull_cmd)
# Logs are output to stderr even if command execution is successful.
print(errs)

rm_cmd = [adb_path]
rm_cmd.append('shell')
Expand All @@ -249,6 +260,7 @@ def get_screenshot():
except (IndexError, FileNotFoundError) as e:
print('adb not found.')
print('Please install Android SDK Build Tools.')
return False


def check_sensitive_files(target_path):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="apkutil",
version="0.1.7",
version="0.1.8",
description="decode, patch, build, etc",
author="Taichi Kotake",
packages=['apkutil'],
Expand Down

0 comments on commit 42de585

Please sign in to comment.