0

Is it possible to find the launch class name programmatically? For example, if the manifest is as below:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MyActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity>...</activity>
</application>

The output will be MyActivity.class.

martin_joerg
  • 322
  • 1
  • 4
  • 10
lucky1928
  • 1,622
  • 8
  • 28

1 Answers1

1

You can use xml.el to handle XML data. The following function should provide you with the class names you are looking for:

(require 'xml)
(require 'seq)

(defun activity-from-manifest (manifest)
  (seq-filter 'identity
              (mapcar (lambda (node)
                        (let ((name (xml-get-attribute-or-nil node 'android:name)))
                          (and name (substring (concat name ".class") 1))))
                      (xml-get-children (car manifest) 'activity))))

You can obtain the parsed XML data to pass into the function either from an XML file or from the region of a buffer as follows:

(activity-from-manifest (xml-parse-file "AndroidManifest.xml"))
(activity-from-manifest (xml-parse-region))

Note that this code works for the XML structure you have shown. If your data is wrapped inside a manifest element like in typical manifest files then you would need to use

(xml-get-children
 (car (xml-get-children (car manifest) 'application))
 'activity)

instead of (xml-get-children (car manifest) 'activity).

martin_joerg
  • 322
  • 1
  • 4
  • 10