Ejemplo n.º 1
0
        private void signToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Show the dialog and get result.
            DialogResult result = openFileDialog5.ShowDialog();

            if (result == DialogResult.OK) // Test result.
            {
                String apk_path = openFileDialog5.FileName;

                string installPath   = GetJavaInstallationPath();
                string filePath      = @System.IO.Path.Combine(installPath, "bin\\Java.exe");
                String assembly_path = Path.GetDirectoryName(Application.ExecutablePath);
                if (System.IO.File.Exists(filePath))
                {
                    String signapk_path     = Path.Combine(assembly_path, "signapk.jar");
                    String certificate_path = Path.Combine(assembly_path, "testkey.x509.pem");
                    String key_path         = Path.Combine(assembly_path, "testkey.pk8");

                    if (!File.Exists(signapk_path))
                    {
                        MessageBox.Show(String.Format("Couldn't find signapk.jar from: {0}", signapk_path), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (!File.Exists(certificate_path))
                    {
                        MessageBox.Show(String.Format("Couldn't find testkey.x509.pem from: {0}", certificate_path), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (!File.Exists(key_path))
                    {
                        MessageBox.Show(String.Format("Couldn't find testkey.pk8 from: {0}", key_path), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    // Use ProcessStartInfo class.
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.CreateNoWindow        = true;
                    startInfo.UseShellExecute       = false;
                    startInfo.RedirectStandardError = true;
                    startInfo.FileName    = filePath;
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    //startInfo.Arguments = "-jar " + "\"" + signapk_path + "\" \"" + certificate_path + "\" \"" + key_path + "\" \"" + apk_path + "\" \"" + (Path.ChangeExtension(apk_path, String.Empty).TrimEnd('.') + "_signed.apk") + "\"";
                    startInfo.Arguments = String.Format("-jar \"{0}\" \"{1}\" \"{2}\" \"{3}\" \"{4}\"", signapk_path, certificate_path, key_path, apk_path, Path.ChangeExtension(apk_path, String.Empty).TrimEnd('.') + "_signed.apk");

                    PerformProcess(startInfo);
                }
                else
                {
                    String key_path = Path.Combine(assembly_path, "testkey.p12");
                    if (!File.Exists(key_path))
                    {
                        MessageBox.Show(String.Format("Couldn't find testkey.p12 from: {0}", key_path), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (!File.Exists(apk_path))
                    {
                        MessageBox.Show(String.Format("Couldn't find apk from: {0}", apk_path), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    this.Enabled = false;
                    SplashForm.ShowSplashScreen(this);
                    try
                    {
                        FileStream inputStream  = null;
                        FileStream outputStream = null;

                        try
                        {
                            var certificate = new X509Certificate2(key_path, "razerman");
                            inputStream  = new FileStream(apk_path, FileMode.Open);
                            outputStream = new FileStream(Path.ChangeExtension(apk_path, String.Empty).TrimEnd('.') + "_signed.apk", FileMode.OpenOrCreate);
                            SignApk.SignPackage(inputStream, certificate, outputStream, true);
                        }
                        catch (Exception b)
                        {
                            MessageBox.Show(b.StackTrace, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Environment.Exit(1);
                        }
                        finally
                        {
                            try
                            {
                                if (inputStream != null)
                                {
                                    inputStream.Close();
                                }
                                if (outputStream != null)
                                {
                                    outputStream.Close();
                                }
                            }
                            catch (IOException b)
                            {
                                MessageBox.Show(b.StackTrace, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                Environment.Exit(1);
                            }
                        }
                    }
                    catch (Exception b)
                    {
                        MessageBox.Show(b.StackTrace, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    SplashForm.CloseForm();
                    this.Enabled = true;
                }

                //            MessageBox.Show("Couldn't find Java.exe or SignApk.exe and can't sign!" + Environment.NewLine +
                //"Fixes: " + Environment.NewLine +
                //"1. Install Java and check JAVA_HOME Environment Variable is correct" + Environment.NewLine +
                //"2. Place SignApk.exe and ICSharpCode.SharpZipLib.dll in the same directory as this *.exe"
                //, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            System.Diagnostics.Debug.WriteLine(result); // <-- For debugging use.
        }