Last active
July 27, 2016 22:48
-
-
Save frozax/78e117d9d7e9747ca93d9eae6ca82466 to your computer and use it in GitHub Desktop.
Helper script to run logcat from an apk (to install) or package (to (re-)start, eventually remove data)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import sys | |
import argparse | |
import re | |
# change this for you | |
ADB_PATH = "D:\\gamedev\\android-sdk\\platform-tools\\adb.exe" | |
AAPT_PATH = "d:\\gamedev\\android-sdk\\build-tools\\23.0.3\\aapt.exe" | |
parser = argparse.ArgumentParser(description='Clever logcat') | |
parser.add_argument('--package', '-p', type=str, help='activity to check (eg: com.example.app)') | |
parser.add_argument('--apk', '-a', type=str, help='install apk (eg: app.apk)') | |
parser.add_argument('--force', '-f', action='store_true', help='force restart of activity') | |
parser.add_argument('--remove-data', '-d', action='store_true', help='remove save data') | |
args = parser.parse_args() | |
package = args.package | |
if args.package and args.apk: | |
print("apk or activity, not both") | |
# add log types to ignore | |
ignore = [ | |
] | |
def _run(cmd, task_name): | |
print task_name + "...", | |
try: | |
ret = subprocess.check_output(cmd, shell=True) | |
except subprocess.CalledProcessError: | |
print("Error") | |
sys.exit(1) | |
print "OK!" | |
return ret | |
def _run_adb(params, task_name): | |
return _run(ADB_PATH + " " + params, task_name) | |
def _get_pid(p): | |
pid_line = _run_adb("shell ps | grep " + p, "get pid") | |
pid = pid_line.split()[1] | |
return int(pid) | |
if args.apk: | |
# find package name | |
ret = _run(AAPT_PATH + " dump badging " + args.apk + " | grep package", "Extracting package name") | |
package = re.match("package: name='(.*)' versionCode=.*", ret).group(1) | |
# install it | |
ret = _run_adb("install -rtdg " + args.apk, "Installing") | |
pid = None | |
if package: | |
# force kill if needed | |
if args.force: | |
pid = _run_adb("shell am force-stop " + package, "Killing") | |
if args.remove_data: | |
_run_adb("shell pm clear " + package, "Removing data") | |
# run package | |
_run_adb("shell monkey -p " + package + " -c android.intent.category.LAUNCHER 1", "Launching app") | |
# find pid | |
pid = _get_pid(package) | |
cmd = ADB_PATH + " logcat -v time -s " + ' '.join([('"%s:S"' % a) for a in ignore]) + " *:V" | |
if pid: | |
cmd += " | grep " + str(pid) | |
subprocess.Popen(cmd, shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment