这是在ApkIDE中使用的用于解析apk信息,如:名称、包名、版本、权限、支持的屏幕尺寸、屏幕密度等信息的工具类。
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Com.Popotu.ApkIDE
{
/// <summary>
/// 需要用到aapt.exe
/// </summary>
class AaptCmd
{
/// <summary>
/// 读取apk的信息,需要用到aapt.exe
/// </summary>
/// <param name="apk"></param>
public static void ParseAndFillApkInfo(Apk apk)
{
string blnk = " ", snewLine = "\r\n", dyh = "'";
if (string.IsNullOrEmpty(apk.FilePath) || !File.Exists(apk.FilePath)) throw new IOException("Apk File is NOT Exists.");
string dumpFile = Path.GetTempFileName();
List<string> cmdList = new List<string>();
cmdList.Add(string.Format("set path={0}", Path.Combine(Appres.JDK_HOME, "bin")));
cmdList.Add(string.Format("aapt dump badging \"{0}\" > \"{1}\"", apk.FilePath, dumpFile));
new ShellCmd().Run(cmdList.ToArray());
Encoding enc = Encoding.UTF8;
string txt = IOCommand.LoadText(dumpFile, enc);
string[] lines = txt.Split(snewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string temp;
string[] patterns;
int istart, iend;
apk.UsesPermissions = new List<string>();
apk.UsesFeatures = new List<string>();
foreach (string line in lines)
{
if (line.StartsWith((temp = "uses-permission:")))//uses-permission
{
temp = line.Replace(temp, string.Empty);
temp = temp.Replace("'", string.Empty);
apk.UsesPermissions.Add(temp);
}
else if (line.StartsWith((temp = "uses-feature:")))//uses-feature
{
temp = line.Replace(temp, string.Empty);
temp = temp.Replace("'", string.Empty);
apk.UsesFeatures.Add(temp);
}
else if (line.StartsWith("package"))//package
{
temp = "name='";
if (line.Contains(temp))//package
{
istart = line.IndexOf(temp) + temp.Length;
temp = dyh;
iend = line.IndexOf(temp, istart);
apk.Package = line.Substring(istart, iend - istart);
}
temp = "versionCode='";
if (line.Contains(temp))//versionCode
{
istart = line.IndexOf(temp) + temp.Length;
temp = dyh;
iend = line.IndexOf(temp, istart);
apk.VersionCode = line.Substring(istart, iend - istart);
}
temp = "versionName='";
if (line.Contains(temp))
{
istart = line.IndexOf(temp) + temp.Length;
temp = dyh;
iend = line.IndexOf(temp, istart);
apk.VersionName = line.Substring(istart, iend - istart);
}
}
else if (line.StartsWith((temp = "sdkVersion:")))//sdkVersion
{
temp = line.Replace(temp, string.Empty);
temp = temp.Replace("'", string.Empty);
apk.MinSdkVersion = temp;
}
else if (line.StartsWith((temp = "targetSdkVersion:")))//targetSdkVersion
{
temp = line.Replace(temp, string.Empty);
temp = temp.Replace("'", string.Empty);
apk.TargetSdkVersion = temp;
}
else if (line.StartsWith("application"))//application
{
temp = "label='";
if (line.Contains(temp))//package
{
istart = line.IndexOf(temp) + temp.Length;
temp = dyh;
iend = line.IndexOf(temp, istart);
apk.Name = line.Substring(istart, iend - istart);
}
temp = "icon='";
if (line.Contains(temp))//package
{
istart = line.IndexOf(temp) + temp.Length;
temp = dyh;
iend = line.IndexOf(temp, istart);
apk.IconPathInApk = line.Substring(istart, iend - istart);
}
}
else if (line.StartsWith((temp = "supports-screens:")))//supports-screens
{
temp = line.Replace(temp, string.Empty);
temp = temp.Replace("'", string.Empty);
patterns = temp.Split(blnk.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
apk.SupportsScreens = patterns;
}
else if (line.StartsWith((temp = "densities:")))//densities
{
temp = line.Replace(temp, string.Empty);
temp = temp.Replace("'", string.Empty);
patterns = temp.Split(blnk.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
apk.Densities = patterns;
}
}
}
}
}
网友评论