0

I am new to Python, so this might be a way too simple question. I am trying to parse AndroidManifest.xml file to find the main activity.

XML file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.heartrateapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MonitorActivity" />
        <activity android:name=".SubmitActivity" />
    </application>

</manifest>

The activity with the following intent is the main activity.

<action android:name="android.intent.action.MAIN" />

Python parser:

from xml.dom.minidom import parseString

  data = ''
  with open('AndroidManifest.xml','r') as f:
    data = f.read()
  dom = parseString(data)
  activities = dom.getElementsByTagName('activity')
  perms = dom.getElementsByTagName('uses-permission')

  for activity in activities:
    print activity.getAttribute('android:name')
    print activity.getElementsByTagName('intent-filter')
 for perm in perms:
    print perm.getAttribute('android:name')

How can I find the main activity? For now I have used print to check. I would like to create an object to store the Main Activity, intents, services, perms. etc. Could someone please help me out?

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
funkadelic
  • 83
  • 10
  • "activity with the following intent is the main activity" is incorrect, there can be several with category Launcher – Nick Cardoso Feb 21 '17 at 03:54
  • @NickCardoso thanks! Should it be `` ? – funkadelic Feb 21 '17 at 03:56
  • Yes - and in case you need it - [other help with intents](https://developer.android.com/guide/components/intents-filters.html) (Sorry, I dont know any python to actually answer you) – Nick Cardoso Feb 21 '17 at 03:58
  • @NickCardoso i followed this http://stackoverflow.com/questions/15526805/two-main-activities-in-androidmanifest-xml – funkadelic Feb 21 '17 at 03:59
  • Sure, the question code is misleading then. action.MAIN = Main entry point, category.LAUNCHER = show activity shortcut in app drawer and optional category.DEFAULT = normal intents (vs looking for an item to open a link with etc.) Additionally, when parsing remember there can be multiple category and action items in each activity – Nick Cardoso Feb 21 '17 at 04:04
  • @NickCardoso thanks a lot! – funkadelic Feb 21 '17 at 04:09

2 Answers2

2

This worked for finding main activity. How do I add to a class instead of using print?

for activity in activities:
    print activity.getAttribute('android:name')
    intents = activity.getElementsByTagName('intent-filter')
    for intent in intents:
        actions = intent.getElementsByTagName('action')
        for action in actions:
            if  action.getAttribute('android:name') == 'android.intent.action.MAIN':
                print action.getAttribute('android:name')
funkadelic
  • 83
  • 10
0

As mentioned you're looking for the Activity which contains a <action android:name="android.intent.action.MAIN" /> intent filter.

Being as there is supposed to be only one of these in a manifest, you can stop when you find the first one.

So from your sample and my knowledge of other languages I'd expect your code to look like

from xml.dom.minidom import parseString

  data = ''
  with open('AndroidManifest.xml','r') as f:
      data = f.read()
      dom = parseString(data)
      activities = dom.getElementsByTagName('activity')
      perms = dom.getElementsByTagName('uses-permission')

      mainactivity = None #or whatever python null is

      for activity in activities:
          filters = activity.getElementsByTagName('intent-filter')
          for intent in filters:
              if intent.getAttribute('android:name') == "android.intent.action.MAIN"
                  mainactivity = activity
                  break #end intent loop 
           if mainactivity
              break #end activity loop

      print mainactivity.getAttribute('android:name')
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124