I try to read the manifest of a third app in my emulator, so I have used the PackageManager to list all apps and then picked a random app to read its manifest. The problem is that it returns be bad characters like that:
����>����r��������������������������������������l�����
There is my listing function:
val apps = context.packageManager.getInstalledApplications(PackageManager.GET_META_DATA or PackageManager.GET_SHARED_LIBRARY_FILES)
for (i in 0 until apps.size)
{
Log.i("List", "Application $i = ${apps[i]}")
if (i == 80)
{
val content = readManifest(apps[i].publicSourceDir)
}
}
And there is my function where I try to read the manifest :
private fun readManifest(path: String): String
{
Log.i("readManifest", "path = $path")
val apk = ZipFile(path)
val manifest = apk.getEntry("AndroidManifest.xml")
val stream = apk.getInputStream(manifest)
val content = StringBuilder()
if (manifest != null)
{
val bufferedReader = BufferedReader(InputStreamReader(apk.getInputStream(manifest), "utf-8"))
var line = bufferedReader.readLine()
while (line != null)
{
Log.i("readManifest", "line = $line")
content.append(line)
line = bufferedReader.readLine()
}
}
apk.close()
stream.close()
return content.toString()
}
I also tried to use another function that I found here but with the same result.
Why do I get this weird characters ? I also put the android.permission.READ_EXTERNAL_STORAGE in my manifest
After some research about the apk format, it seems that the Manifest is compiled in the apk file. So it's why I couldn't read it.
To read it, we must decompile the apk before, either with a library (that I couldn't find) either in coding it.
check this out get readable androidmanifest
if (fileName.endsWith(".apk") || fileName.endsWith(".zip")) {
String entryName = args.length > 1? args[1] : "AndroidManifest.xml";
zip = new ZipFile(fileName);
ZipEntry entry = zip.getEntry(entryName);
is = zip.getInputStream(entry);
} else {
is = new FileInputStream(fileName);
}
try {
Document doc = new CompressedXmlParser().parseDOM(is);
dumpNode(doc.getChildNodes().item(0), "");
}
catch (Exception e) {
System.err.println("Failed AXML decode: " + e);
e.printStackTrace();
}
is.close();
if (zip != null) {
zip.close();
}
}
private static void dumpNode(Node node, String indent) {
System.out.println(indent + node.getNodeName() + " " + attrsToString(node.getAttributes()) + " -> " + node.getNodeValue());
NodeList children = node.getChildNodes();
for (int i = 0, n = children.getLength(); i < n; ++i)
dumpNode(children.item(i), indent + " ");
}
private static String attrsToString(NamedNodeMap attrs) {
StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0, n = attrs.getLength(); i < n; ++i) {
if (i != 0)
sb.append(", ");
Node attr = attrs.item(i);
sb.append(attr.getNodeName() + "=" + attr.getNodeValue());
}
sb.append(']');
return sb.toString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.