Created
October 27, 2015 16:18
-
-
Save nguyentruongtho/766cd5c59587713d5755 to your computer and use it in GitHub Desktop.
Start an activity using adb without pain.
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
#!/usr/bin/env python | |
""" | |
Running an activity from terminal using adb is more complex than necessary, | |
you have to find the package and activity names, combine them together | |
and place them in a command that you probably will forget right after | |
using it. | |
We should only give the name of an activity for running one! | |
usage: Run this command from the root folder of the project: `activity.py TestActivity` | |
""" | |
import os | |
import sys | |
from xml.dom.minidom import parseString | |
LIST_MANIFESTS = 'find . -name "AndroidManifest.xml" -not -path "**/build/*"' | |
ADB_ACTIVITY_COMMAND = 'adb shell "am start -n %s"' | |
def manifests(): | |
(stdin, stdout, stderr) = os.popen3(LIST_MANIFESTS) | |
return stdout.read().split() | |
def find_activity(manifest_file_content, target_activity): | |
dom = parseString(manifest_file_content) | |
manifest_node = dom.getElementsByTagName('manifest')[0] | |
packageName = manifest_node.getAttribute('package') | |
found_activities = set([]) | |
activities = dom.getElementsByTagName('activity') | |
for activity in activities: | |
activity_name = activity.getAttribute('android:name') | |
if activity_name.endswith(target_activity): | |
found_activities.add(packageName + "/" + activity_name) | |
return found_activities | |
target_activity = sys.argv[1] | |
print "Searching: " + target_activity | |
activities = set([]) | |
for manifest in manifests(): | |
with open(manifest, 'r') as f: | |
data = f.read() | |
if target_activity in data: | |
activities.update(find_activity(data, target_activity)) | |
if len(activities) == 1: | |
activity = next(iter(activities)) | |
print "Found " + activity | |
command = ADB_ACTIVITY_COMMAND % (activity) | |
print "Run: " + command | |
os.system(command) | |
elif len(activities) == 0: | |
print "Activity not found" | |
else: | |
print "We have found more than one activities:" | |
print "\n\t+".join(activities) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment