public string FingerprintFromApk() { string fingerprint = ""; if(File.Exists(_apk)) { using (var unzip = new Unzip (_apk)) { foreach (var entry in unzip.Entries) { if (entry.Name.ToLower().EndsWith (".rsa")) { unzip.Extract (entry.Name, "temp.rsa"); } } } String output; using (Process p = new Process ()) { p.StartInfo.FileName = _keytool; p.StartInfo.Arguments = " -v -printcert -file ./temp.rsa"; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; using (AutoResetEvent outputWaitHandle = new AutoResetEvent (false)) using (AutoResetEvent errorWaitHandle = new AutoResetEvent (false)) { output = HandleOutput (p, outputWaitHandle, errorWaitHandle, -1, false); fingerprint = ExtractMd5Fingerprint (output); File.Delete ("./temp.rsa"); } } } return fingerprint; }
private void ResignApk() { string unpackDirectory = "temp"; Directory.CreateDirectory (unpackDirectory); using (var unzip = new Unzip (_apkLocation)) { foreach (var entry in unzip.Entries) { if (!entry.Name.StartsWith ("META-INF/")) { string[] nameSplit = Regex.Split (entry.Name, "/"); StringBuilder sb = new StringBuilder (); sb.Append (string.Format("{0}/", unpackDirectory)); for (var i = 0; i < nameSplit.Length - 1; i++) { sb.Append (string.Format ("{0}/", nameSplit [i])); if (!Directory.Exists (sb.ToString ())) { Directory.CreateDirectory (sb.ToString ()); } } unzip.Extract (entry.Name, string.Format ("{0}/{1}", unpackDirectory, entry.Name)); } } } _apkLocation = PackageAndSignApk ("TestApk.apk", unpackDirectory); }
private void RepackageTestServer() { string testServer = string.Format("{0}{1}",Directory.GetCurrentDirectory (), "/Resources/TestServer.apk"); string testServerLocation = string.Format ("{0}{1}", Directory.GetCurrentDirectory(), "/testServer"); string tempManifestLocation = string.Format ("{0}{1}", testServerLocation, "/AndroidManifest.xml"); if (File.Exists (testServer) && File.Exists(tempManifestLocation)) { using (var unzip = new Unzip (testServer)) { foreach (var entry in unzip.Entries) { if (!entry.Name.Equals("AndroidManifest.xml")) { string[] nameSplit = Regex.Split (entry.Name, "/"); StringBuilder sb = new StringBuilder (); sb.Append ("testServer/"); for (var i = 0; i < nameSplit.Length - 1; i++) { sb.Append (string.Format("{0}/", nameSplit [i])); if(!Directory.Exists(sb.ToString())) { Directory.CreateDirectory (sb.ToString ()); } } unzip.Extract (entry.Name, string.Format("{0}/{1}", testServerLocation, entry.Name)); } } } } _testServerApkLocation = PackageAndSignApk ("TestServer.apk", testServerLocation); }
private void PrepareTestServer() { string manifestLocation = string.Format("{0}{1}",Directory.GetCurrentDirectory (), "/Resources/AndroidManifest.xml"); string testServerLocation = string.Format ("{0}{1}", Directory.GetCurrentDirectory(), "/testServer"); string tempManifestLocation = string.Format ("{0}{1}", testServerLocation, "/AndroidManifest.xml"); string dummyApk = string.Format("{0}/{1}",Directory.GetCurrentDirectory (),"dummy.apk"); Directory.CreateDirectory (testServerLocation); if (File.Exists (manifestLocation)) { string output; string fileContent = File.ReadAllText(manifestLocation); fileContent = fileContent.Replace ("#targetPackage#", _packageName); fileContent = fileContent.Replace ("#testPackage#", string.Format("{0}{1}",_packageName, ".test")); using (StreamWriter file = new StreamWriter(tempManifestLocation, true)) { file.Write(fileContent); } using (Process p = new Process ()) { p.StartInfo.FileName = _aaptLocation; p.StartInfo.Arguments = string.Format("package -M {0} -I {1}/{2} -F {3}", tempManifestLocation, _androidVersionHelper.GetHighestPlatformLocation(), "android.jar", dummyApk); p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; using (AutoResetEvent outputWaitHandle = new AutoResetEvent (false)) using (AutoResetEvent errorWaitHandle = new AutoResetEvent (false)) { output = HandleOutput (p, outputWaitHandle, errorWaitHandle, -1, false); Console.WriteLine (output); using (var unzip = new Unzip (dummyApk)) { unzip.Extract ("AndroidManifest.xml", tempManifestLocation); } RepackageTestServer (); } } } }