【C#與Apk二三事】Apk信息解析工具類

這是在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;
                }
            }
        }
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末徒像,一起剝皮案震驚了整個濱河市黍特,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌锯蛀,老刑警劉巖灭衷,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異旁涤,居然都是意外死亡翔曲,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門劈愚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瞳遍,“玉大人,你說我怎么就攤上這事菌羽「吊澹” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵算凿,是天一觀的道長。 經常有香客問我犁功,道長氓轰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任浸卦,我火速辦了婚禮署鸡,結果婚禮上,老公的妹妹穿的比我還像新娘限嫌。我一直安慰自己靴庆,他們只是感情好,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布怒医。 她就那樣靜靜地躺著炉抒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪稚叹。 梳的紋絲不亂的頭發(fā)上焰薄,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天拿诸,我揣著相機與錄音,去河邊找鬼塞茅。 笑死亩码,一個胖子當著我的面吹牛,可吹牛的內容都是我干的野瘦。 我是一名探鬼主播描沟,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鞭光!你這毒婦竟也來了吏廉?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤衰猛,失蹤者是張志新(化名)和其女友劉穎迟蜜,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體啡省,經...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡娜睛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了卦睹。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片畦戒。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖结序,靈堂內的尸體忽然破棺而出障斋,到底是詐尸還是另有隱情,我是刑警寧澤徐鹤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布垃环,位于F島的核電站,受9級特大地震影響返敬,放射性物質發(fā)生泄漏遂庄。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一劲赠、第九天 我趴在偏房一處隱蔽的房頂上張望涛目。 院中可真熱鬧,春花似錦凛澎、人聲如沸霹肝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沫换。三九已至,卻和暖如春轧叽,著一層夾襖步出監(jiān)牢的瞬間苗沧,已是汗流浹背刊棕。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留待逞,地道東北人甥角。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像识樱,于是被迫代替她去往敵國和親嗤无。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內容