Decode Android AndroidManifest.xml file via C#
Android安装包APK文件中的AndroidManifest.xml记录了应用的详细信息,发布时该文件已被编译成二进制码,所以无法正常查看其内容。比如我想查看这个包的Package和Version信息(这指的是在服务端)。
AXMLPrinter2.jar可以很好的帮我做到这点,如果你有Java环境,可以通过以下命令行解码xml中的内容
java -jar AXMLPrinter2.jar AndroidManifest.xml > AndroidManifest.txt
当然你可以在dotnet程序里通过调用外部进程来运行此
ProcessStartInfo ph = new ProcessStartInfo("AXMLPrinter2.jar","AndroidManifest.xml > AndroidManifest.txt"); Process.Start(ph);
不过这在asp.net的web程序因权限问题会有诸多不便,因我的生产环境没有Java,所以想在C#下实现这个功能。AXMLPrinter2.jar是有源代码的(Eclipse project),你完全可以用C#重写一遍 :)
借助伟大的IKVM.NET (dotnet上java的实现),我可以把AXMLPrinter2.jar转换成一个dotnet的Assembly
ikvmc -out:AXMLPrinter2.dll AXMLPrinter2.jar
引用转换后的dll和IKVM的lib(IKVM.Runtime.dll,IKVM.OpenJDK.Core.dll),这样就可以很方便的在C#中调用AXMLPrinter2.jar里的方法了,下面是C#中的一段调用
public static string DecodeManifestXML(string filePath)
{
if (!File.Exists(filePath))
return null;
try
{
AXmlResourceParser parser = new AXmlResourceParser();
parser.open(new java.io.FileInputStream(filePath));
const string indent = " ";
StringBuilder xmlContent = new StringBuilder();
while (true)
{
int type = parser.next();
if (type == 1)
{
break;
}
switch (type)
{
case 0:
{
xmlContent.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
break;
}
case 2:
{
xmlContent.AppendFormat("{0}<{1}{2}", indent, getNamespacePrefix(parser.getPrefix()), parser.getName());
int namespaceCountBefore = parser.getNamespaceCount(parser.getDepth() - 1);
int namespaceCount = parser.getNamespaceCount(parser.getDepth());
for (int i = namespaceCountBefore; i != namespaceCount; ++i)
{
xmlContent.AppendFormat("{0}xmlns:{1}=\"{2}\"",
indent,
parser.getNamespacePrefix(i),
parser.getNamespaceUri(i));
}
for (int i = 0; i != parser.getAttributeCount(); ++i)
{
xmlContent.AppendFormat("{0}{1}{2}=\"{3}\"", indent,
getNamespacePrefix(parser.getAttributePrefix(i)),
parser.getAttributeName(i),
getAttributeValue(parser, i));
}
xmlContent.AppendFormat("{0}>", "");
break;
}
case 3:
{
xmlContent.AppendFormat("{0}</{1}{2}>", "",
getNamespacePrefix(parser.getPrefix()),
parser.getName());
break;
}
case 4:
{
xmlContent.AppendFormat("{0}{1}", indent, parser.getText());
break;
}
}
}
return xmlContent.ToString();
}
catch (Exception e)
{
throw e;
}
}
{
if (!File.Exists(filePath))
return null;
try
{
AXmlResourceParser parser = new AXmlResourceParser();
parser.open(new java.io.FileInputStream(filePath));
const string indent = " ";
StringBuilder xmlContent = new StringBuilder();
while (true)
{
int type = parser.next();
if (type == 1)
{
break;
}
switch (type)
{
case 0:
{
xmlContent.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
break;
}
case 2:
{
xmlContent.AppendFormat("{0}<{1}{2}", indent, getNamespacePrefix(parser.getPrefix()), parser.getName());
int namespaceCountBefore = parser.getNamespaceCount(parser.getDepth() - 1);
int namespaceCount = parser.getNamespaceCount(parser.getDepth());
for (int i = namespaceCountBefore; i != namespaceCount; ++i)
{
xmlContent.AppendFormat("{0}xmlns:{1}=\"{2}\"",
indent,
parser.getNamespacePrefix(i),
parser.getNamespaceUri(i));
}
for (int i = 0; i != parser.getAttributeCount(); ++i)
{
xmlContent.AppendFormat("{0}{1}{2}=\"{3}\"", indent,
getNamespacePrefix(parser.getAttributePrefix(i)),
parser.getAttributeName(i),
getAttributeValue(parser, i));
}
xmlContent.AppendFormat("{0}>", "");
break;
}
case 3:
{
xmlContent.AppendFormat("{0}</{1}{2}>", "",
getNamespacePrefix(parser.getPrefix()),
parser.getName());
break;
}
case 4:
{
xmlContent.AppendFormat("{0}{1}", indent, parser.getText());
break;
}
}
}
return xmlContent.ToString();
}
catch (Exception e)
{
throw e;
}
}
关键词:AndroidManifest, Decode, package, Apk, C#, IKVM.NET