【C#与Apk二三事】Apk信息解析工具类

这是在ApkIDE中使用的用于解析apk信息,如:名称、包名、版本、权限、支持的屏幕尺寸、屏幕密度等信息的工具类。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Com.Popotu.ApkIDE
{
    /// 
    /// 需要用到aapt.exe
    /// 
    class AaptCmd
    {
        /// 
        /// 读取apk的信息,需要用到aapt.exe
        /// 
        /// 
        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 cmdList = new List();
            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();
            apk.UsesFeatures = new List();
            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;
                }
            }
        }
    }
}

你可能感兴趣的:(【C#与Apk二三事】Apk信息解析工具类)