温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python——使用ElementTree解析AndroidManifest.xml文件

发布时间:2020-07-16 12:02:32 来源:网络 阅读:3799 作者:wauoen 栏目:移动开发
  1. XML内容如下:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dongbao.wauoen.interappactivitytesting" platformBuildVersionCode="23" platformBuildVersionName="6.0-2704002">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:exported="true" android:name="com.dongbao.wauoen.interappactivitytesting.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:enabled="@bool/use_activity" android:exported="true" android:icon="@drawable/ic_chooser" android:label="@string/choose_file" android:name="com.ipaulpro.afilechooser.FileChooserActivity"/>
        <activity android:exported="true" android:name="com.dongbao.wauoen.interappactivitytesting.ExampleActivity" android:permission="android.permission.READ_EXTERNAL_STORAGE"/>
    </application>
</manifest>

2.解析过程:主要获取开放activity的信息,并保存到xml文件中

from ActivityInfo import ActivityInfo

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
import sys

# Manifest namespace
namespace = '{http://schemas.android.com/apk/res/android}'
# Exported activities
exported_activities = []


# XML analyze
def __xml_analyze__():
    tree = ET.parse(r"./app/AndroidManifest.xml")
    root = tree.getroot()
    # print tree.namespace
    # 1.get package name
    packagename = root.attrib["package"]
    # 2.get exported activity
    application = root.find("application")
    __get_exported_activities__(application, packagename)
    for activity in exported_activities:
        print activity.__get_name__(), activity.__get_package_name__(), activity.__get_permission__()
    # 3.save result to xml file
    __save_result_to_xml__()


# get exported activities
def __get_exported_activities__(application, packagename):
    for activity in application:
        # 2.1 MainActivity name
        if __is_main_activity__(activity):
            name = activity.attrib[namespace+'name']
            main_activtiy = ActivityInfo(name)
            main_activtiy.__set_package_name__(packagename)
            if activity.find(namespace+'permission') is not None:
                permission = activity.attrib[name+'permission']
                main_activtiy.__set_permission__(permission)
            exported_activities.append(main_activtiy)
        else:
            # 2.1 exported activities except mainactivtiy
            exported = activity.attrib[namespace+'exported']
            if exported:
                activity_name = activity.attrib[namespace+'name']
                exported_activity = ActivityInfo(activity_name)
                exported_activity.__set_package_name__(packagename)
                permission = activity.attrib.get(namespace+'permission', None)
                if permission is not None:
                    # permission = activity.attrib[name+'permission']
                    # print permission
                    exported_activity.__set_permission__(permission)
                exported_activities.append(exported_activity)


# save result to xml file
def __save_result_to_xml__():
    tree = ET.ElementTree()
    root = ET.Element('data')
    tree._setroot(root)
    for activity in exported_activities:
        node = ET.Element('activity')
        node.set('name', activity.__get_name__())
        node.set('packagename', activity.__get_package_name__())
        root.append(node)
        if activity.__get_permission__() is not None:
            node.set('permission', activity.__get_permission__())
    tree.write("output.xml")


# the activity is or not MainActivity
def __is_main_activity__(activity):
    intent_filter = activity.find("intent-filter")
    if intent_filter is not None:
        action = intent_filter.find('action')
        category = intent_filter.find('category')
        if action is not None and category is not None \
                and action.attrib[namespace + "name"] == 'android.intent.action.MAIN'\
                and category.attrib[namespace + "name"] == 'android.intent.category.LAUNCHER':
            print 'this is main activity'
            return True
    return False


# main fun
if __name__ == "__main__":
    __xml_analyze__()

3.ActivityInfo 

class ActivityInfo:

    def __init__(self, name):
        self.name = name
        self.package_name = ''
        self.permission = ''

    def __set_package_name__(self, package_name):
        self.package_name = package_name

    def __get_package_name__(self):
        return self.package_name

    def __set_name__(self, name):
        self.name = name

    def __get_name__(self):
        return self.name

    def __set_permission__(self, permission):
        self.permission = permission

    def __get_permission__(self):
        return self.permission




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI