userHG
userHG

Reputation: 589

ParseError while parsing AndroidManifest.xml in python

I'm trying to parse an AndroiManifest.xml file to get informations and I have this error when I'm charging my file

xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0

Here is my code :

import xml.etree.cElementTree as ET

tree = ET.ElementTree(file='AndroidManifest.xml')
root = tree.getroot()

My XML file seems well formed :

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="132074037"
    android:versionName="193.0.0.21.98"
    android:installLocation="0"
    package="com.facebook.orca">

How can I fix that and parse my XML to get a 'android:versionName' tag ?

Upvotes: 0

Views: 565

Answers (1)

userHG
userHG

Reputation: 589

Solved

I was trying to parse an AndroidManifest.xml after I've unzipped an apk but with this method, the AndroidManifest.xml is encoded so it's impossible to open, read or parse it. I was able to read it only by using Android Studio that automatically decodes an AndroidManifest file.

To parse an AndroidManifest.xml after unzipping an apk, the best way is to use aapt command line :

 /Users/{Path_to_your_sdk}/sdk/build-tools/28.0.3/aapt dump
 badging com.squareup.cash.apk | sed -n
 "s/.*versionName='\([^']*\).*/\1/p"

And you will obtain the versionName of your app. Hope it will help.

Upvotes: 1

Related Questions