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.

1 Answer 1

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.