Finding APK package/activity name

During my research I had a problem of finding the package and activity name of the android APK file. Googling only results to tons of messed up solution which usually didnt work.

In this post I am sharing a piece of code I wrote in Python to analyse the result of android aapt tool (*well I am a Python freak so you are going to feel Python in this blog!):

def get_package_activity_name(apk_address):
	command = "aapt dump badging %s" %apk_address
	aapt_result = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()[0]
	lines = aapt_result.split("\n")
	myDic = {}
	for line in lines:
		if len(splitedline)==2:
	package = myDic['package'].split("'")[1]
	activity = myDic['launchable-activity'].split("'")[1].split(".")[-1]
return package, activity

I hope you can use this code or the idea of how to use aapt for obtaining package and main activity names.