Ejemplo n.º 1
0
 public static void GetAPKInfo(string path, out string apk_id, out string activity)
 {
     apk_id   = "";
     activity = "";
     using (MemoryStream ms = new MemoryStream())
     {
         using (var file = System.IO.Compression.ZipFile.OpenRead(path))
         {
             var entry = file.GetEntry("AndroidManifest.xml");
             if (entry != null)
             {
                 using (var manifestStream = entry.Open())
                 {
                     manifestStream.CopyTo(ms);
                     ms.Seek(0, SeekOrigin.Begin);
                 }
             }
         }
         var reader = new AndroidXmlReader(ms);
         while (reader.Read())
         {
             if (reader.NodeType == System.Xml.XmlNodeType.Element && reader.Name == "manifest")
             {
                 if (reader.MoveToAttribute("package"))
                 {
                     apk_id = reader.Value;
                 }
             }
             else if (reader.NodeType == System.Xml.XmlNodeType.Element && reader.Name == "activity")
             {
                 if (reader.MoveToAttribute("name", "http://schemas.android.com/apk/res/android"))
                 {
                     activity = reader.Value;
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
        public static int GetAndroidSdkVersion(int?specificVersion, string binDir)
        {
            if (specificVersion.HasValue)
            {
                return(specificVersion.Value);
            }

            string tempDir = null;

            try
            {
                tempDir = ExpandApk(binDir);
                var manifest = Directory.EnumerateFiles(tempDir, "AndroidManifest.xml", SearchOption.AllDirectories).FirstOrDefault();
                if (string.IsNullOrEmpty(manifest))
                {
                    return(DefaultSdkLevel);
                }

                using var stream = File.OpenRead(manifest);
                using var reader = new AndroidXmlReader(stream);
                while (reader.Read())
                {
                    // <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
                    if (reader.NodeType == System.Xml.XmlNodeType.Element && reader.Name == "uses-sdk")
                    {
                        if (reader.MoveToAttribute("targetSdkVersion", AndroidNamespace))
                        {
                            return(int.Parse(reader.Value));
                        }

                        throw new Exception("android:targetSdkVersion attribute not found...");
                    }
                }
            }
            finally
            {
                CleanupTempDir(tempDir);
            }

            return(DefaultSdkLevel);
        }
Ejemplo n.º 3
0
        public static string GetAppActivity(string binDir)
        {
            string tempDir = null;

            try
            {
                tempDir = ExpandApk(binDir);
                var manifest = Directory.EnumerateFiles(tempDir, "AndroidManifest.xml", SearchOption.AllDirectories).FirstOrDefault();
                using var stream = File.OpenRead(manifest);
                using var reader = new AndroidXmlReader(stream);
                var activities = new List <string>();
                while (reader.Read())
                {
                    /* <application android:label="TestApp.Android" android:theme="@style/MainTheme" android:name="android.app.Application" android:allowBackup="true" android:icon="@mipmap/icon" android:debuggable="true" android:extractNativeLibs="true">
                     *  <activity android:configChanges="orientation|smallestScreenSize|screenLayout|screenSize|uiMode" android:icon="@mipmap/icon" android:label="TestApp" android:theme="@style/MainTheme" android:name="crc64c7fff72bd74cd533.MainActivity">
                     *    <intent-filter>
                     *      <action android:name="android.intent.action.MAIN" />
                     *      <category android:name="android.intent.category.LAUNCHER" />
                     *    </intent-filter>
                     *  </activity> */

                    if (reader.NodeType == XmlNodeType.Element &&
                        reader.Name == "activity" &&
                        reader.MoveToAttribute("name", AndroidNamespace))
                    {
                        activities.Add(reader.Value);
                    }
                }

                // TODO: We may want to look at the intents
                return(activities.FirstOrDefault(x => x.EndsWith(".MainActivity")));
            }
            finally
            {
                CleanupTempDir(tempDir);
            }
        }
Ejemplo n.º 4
0
        public apkreader(string path)
        {
            var manifest = "AndroidManifest.xml";

            //读取apk,通过解压的方式读取
            using (var zip = ZipFile.Read(path))
            {
                using (Stream zipstream = zip[manifest].OpenReader())
                {
                    //将解压出来的文件保存到一个路径(必须这样)
                    using (var fileStream = File.Create(manifest, (int)zipstream.Length))
                    {
                        // Initialize the bytes array with the stream length and then fill it with data
                        byte[] bytesInStream = new byte[zipstream.Length];
                        zipstream.Read(bytesInStream, 0, bytesInStream.Length);
                        // Use write method to write to the file specified above
                        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
                    }
                }
            }

            //读取解压文件的字节数
            byte[] data = File.ReadAllBytes(manifest);
            if (data.Length == 0)
            {
                throw new IOException("Empty file");
            }

            #region 读取文件内容
            using (var stream = new MemoryStream(data))
            {
                var reader = new AndroidXmlReader(stream);

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                    {
                        AndroidInfo info = new AndroidInfo();
                        androidInfos.Add(info);
                        info.Name     = reader.Name;
                        info.Settings = new List <AndroidSetting>();
                        for (int i = 0; i < reader.AttributeCount; i++)
                        {
                            reader.MoveToAttribute(i);

                            AndroidSetting setting = new AndroidSetting()
                            {
                                Name = reader.Name, Value = reader.Value
                            };
                            info.Settings.Add(setting);
                        }
                        reader.MoveToElement();
                        break;
                    }
                    }
                }
            }
            #endregion

            File.Delete(manifest);

            StringBuilder builder = new StringBuilder();
            foreach (var androidInfo in androidInfos)
            {
                builder.Append(string.Format("{0}:", androidInfo.Name));
                foreach (var setting in androidInfo.Settings)
                {
                    builder.Append("{");
                    builder.Append(string.Format("'{0}':'{1}'", setting.Name, setting.Value));
                    builder.Append("},");
                }
                builder.Append("\n\n");
            }
            Console.WriteLine(builder.ToString());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 获取主配置文件信息
        /// </summary>
        /// <param name="apkStream">apk流</param>
        /// <returns>安卓信息列表</returns>
        public static IList <AndroidInfo> GetManifestInfo(Stream apkStream)
        {
            if (apkStream == null)
            {
                throw new ArgumentNullException("apk流不能为null");
            }

            var androidInfos = new List <AndroidInfo>();

            byte[] manifestData = null;

            using (var zipfile = new ICSharpCode.SharpZipLib.Zip.ZipFile(apkStream))
            {
                foreach (ICSharpCode.SharpZipLib.Zip.ZipEntry item in zipfile)
                {
                    if (item.Name.ToLower() == "androidmanifest.xml")
                    {
                        using (Stream strm = zipfile.GetInputStream(item))
                        {
                            using (BinaryReader s = new BinaryReader(strm))
                            {
                                manifestData = s.ReadBytes((int)item.Size);
                            }
                        }

                        break;
                    }
                }

                zipfile.Close();
            }

            if (manifestData.IsNullOrLength0())
            {
                return(null);
            }

            #region 读取文件内容

            using (var stream = new MemoryStream(manifestData))
            {
                using (var reader = new AndroidXmlReader(stream))
                {
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            AndroidInfo info = new AndroidInfo();
                            androidInfos.Add(info);
                            info.Name     = reader.Name;
                            info.Settings = new List <KeyValueInfo <string, string> >();
                            for (int i = 0; i < reader.AttributeCount; i++)
                            {
                                reader.MoveToAttribute(i);

                                var setting = new KeyValueInfo <string, string>()
                                {
                                    Key = reader.Name, Value = reader.Value
                                };
                                info.Settings.Add(setting);
                            }
                            reader.MoveToElement();

                            break;
                        }
                    }

                    reader.Close();
                }
            }

            #endregion

            return(androidInfos);
        }