Exemple #1
0
        protected override string GenerateCommandLineCommands()
        {
            var cmd = new CommandLineBuilder();

            if (!UseProguard)
            {
                // Add the JavaOptions if they are not null
                // These could be any of the additional options
                if (!string.IsNullOrEmpty(JavaOptions))
                {
                    cmd.AppendSwitch(JavaOptions);
                }

                // Add the specific -XmxN to override the default heap size for the JVM
                // N can be in the form of Nm or NGB (e.g 100m or 1GB )
                cmd.AppendSwitchIfNotNull("-Xmx", JavaMaximumHeapSize);

                cmd.AppendSwitchIfNotNull("-jar ", Path.Combine(ProguardJarPath));
            }

            if (!ClassesOutputDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                ClassesOutputDirectory += Path.DirectorySeparatorChar;
            }
            var classesFullPath = Path.GetFullPath(ClassesOutputDirectory);

            if (File.Exists(ProguardJarInput))
            {
                File.Delete(ProguardJarInput);
            }
            using (var zip = ZipArchive.Open(ProguardJarInput, FileMode.Create)) {
                foreach (var file in Directory.GetFiles(classesFullPath, "*", SearchOption.AllDirectories))
                {
                    zip.AddFile(file, Path.Combine(Path.GetDirectoryName(file.Substring(classesFullPath.Length)), Path.GetFileName(file)));
                }
            }

            var acwLines = File.ReadAllLines(AcwMapFile);

            using (var appcfg = File.CreateText(ProguardGeneratedApplicationConfiguration))
                for (int i = 0; i + 3 < acwLines.Length; i += 4)
                {
                    try {
                        var java = acwLines [i + 3].Substring(acwLines [i + 3].IndexOf(';') + 1);
                        appcfg.WriteLine("-keep class " + java + " { *; }");
                    } catch {
                        // skip invalid lines
                    }
                }

            var injars  = new List <string> ();
            var libjars = new List <string> ();

            injars.Add(ProguardJarInput);
            if (JavaLibrariesToEmbed != null)
            {
                foreach (var jarfile in JavaLibrariesToEmbed)
                {
                    injars.Add(jarfile.ItemSpec);
                }
            }

            using (var xamcfg = File.Create(ProguardCommonXamarinConfiguration))
                GetType().Assembly.GetManifestResourceStream("proguard_xamarin.cfg").CopyTo(xamcfg);

            var configs = ProguardConfigurationFiles
                          .Replace("{sdk.dir}", AndroidSdkDirectory + Path.DirectorySeparatorChar)
                          .Replace("{intermediate.common.xamarin}", ProguardCommonXamarinConfiguration)
                          .Replace("{intermediate.references}", ProguardGeneratedReferenceConfiguration)
                          .Replace("{intermediate.application}", ProguardGeneratedApplicationConfiguration)
                          .Replace("{project}", string.Empty)        // current directory anyways.
                          .Split(';')
                          .Select(s => s.Trim())
                          .Where(s => !string.IsNullOrWhiteSpace(s));

            foreach (var file in configs)
            {
                if (File.Exists(file))
                {
                    cmd.AppendSwitchIfNotNull("-include ", file);
                }
                else
                {
                    Log.LogWarning("Proguard configuration file '{0}' was not found.", file);
                }
            }

            libjars.Add(JavaPlatformJarPath);
            if (ExternalJavaLibraries != null)
            {
                foreach (var jarfile in ExternalJavaLibraries.Select(p => p.ItemSpec))
                {
                    libjars.Add(jarfile);
                }
            }

            var enclosingChar = OS.IsWindows ? "\"" : string.Empty;

            cmd.AppendSwitchUnquotedIfNotNull("-injars ", $"{enclosingChar}'" + string.Join($"'{Path.PathSeparator}'", injars.Distinct()) + $"'{enclosingChar}");

            cmd.AppendSwitchIfNotNull("-libraryjars ", $"{enclosingChar}'" + string.Join($"'{Path.PathSeparator}'", libjars.Distinct()) + $"'{enclosingChar}");

            cmd.AppendSwitchIfNotNull("-outjars ", ProguardJarOutput);

            if (EnableLogging)
            {
                cmd.AppendSwitchIfNotNull("-dump ", DumpOutput);
                cmd.AppendSwitchIfNotNull("-printseeds ", PrintSeedsOutput);
                cmd.AppendSwitchIfNotNull("-printusage ", PrintUsageOutput);
                cmd.AppendSwitchIfNotNull("-printmapping ", PrintMappingOutput);
            }

            // http://stackoverflow.com/questions/5701126/compile-with-proguard-gives-exception-local-variable-type-mismatch#7587680
            cmd.AppendSwitch("-optimizations !code/allocation/variable");

            return(cmd.ToString());
        }
Exemple #2
0
        protected override string GenerateCommandLineCommands()
        {
            var cmd = new CommandLineBuilder();

            if (!UseProguard)
            {
                // Add the JavaOptions if they are not null
                // These could be any of the additional options
                if (!string.IsNullOrEmpty(JavaOptions))
                {
                    cmd.AppendSwitch(JavaOptions);
                }

                // Add the specific -XmxN to override the default heap size for the JVM
                // N can be in the form of Nm or NGB (e.g 100m or 1GB )
                cmd.AppendSwitchIfNotNull("-Xmx", JavaMaximumHeapSize);

                cmd.AppendSwitchIfNotNull("-jar ", Path.Combine(ProguardJarPath));
            }

            if (!ClassesOutputDirectory.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                ClassesOutputDirectory += Path.DirectorySeparatorChar;
            }
            var classesFullPath = Path.GetFullPath(ClassesOutputDirectory);

            if (File.Exists(ProguardJarInput))
            {
                File.Delete(ProguardJarInput);
            }
            var zip = new Ionic.Zip.ZipFile(ProguardJarInput, new System.Text.UTF8Encoding(false));

            foreach (var file in Directory.GetFiles(classesFullPath, "*", SearchOption.AllDirectories))
            {
                zip.AddFile(file, Path.GetDirectoryName(file.Substring(classesFullPath.Length)));
            }
            zip.Save();
            zip.Dispose();

            var acwLines = File.ReadAllLines(AcwMapFile);

            using (var appcfg = File.CreateText(ProguardGeneratedApplicationConfiguration))
                for (int i = 0; i + 3 < acwLines.Length; i += 4)
                {
                    try {
                        var java = acwLines [i + 3].Substring(acwLines [i + 3].IndexOf(';') + 1);
                        appcfg.WriteLine("-keep class " + java + " { *; }");
                    } catch {
                        // skip invalid lines
                    }
                }

            var injars  = new List <string> ();
            var libjars = new List <string> ();

            injars.Add(ProguardJarInput);
            if (JavaLibrariesToEmbed != null)
            {
                foreach (var jarfile in JavaLibrariesToEmbed)
                {
                    injars.Add(jarfile.ItemSpec);
                }
            }

            using (var xamcfg = File.Create(ProguardCommonXamarinConfiguration))
                GetType().Assembly.GetManifestResourceStream("proguard_xamarin.cfg").CopyTo(xamcfg);

            var configs = ProguardConfigurationFiles
                          .Replace("{sdk.dir}", Path.GetDirectoryName(Path.GetDirectoryName(ProguardHome)) + Path.DirectorySeparatorChar)
                          .Replace("{intermediate.common.xamarin}", ProguardCommonXamarinConfiguration)
                          .Replace("{intermediate.references}", ProguardGeneratedReferenceConfiguration)
                          .Replace("{intermediate.application}", ProguardGeneratedApplicationConfiguration)
                          .Replace("{project}", string.Empty)        // current directory anyways.
                          .Split(';')
                          .Select(s => s.Trim())
                          .Where(s => !string.IsNullOrWhiteSpace(s));

            foreach (var file in configs)
            {
                if (File.Exists(file))
                {
                    cmd.AppendSwitchIfNotNull("-include ", file);
                }
                else
                {
                    Log.LogWarning("Proguard configuration file '{0}' was not found.", file);
                }
            }

            libjars.Add(JavaPlatformJarPath);
            if (ExternalJavaLibraries != null)
            {
                foreach (var jarfile in ExternalJavaLibraries.Select(p => p.ItemSpec))
                {
                    libjars.Add(jarfile);
                }
            }

            cmd.AppendSwitch("\"-injars");
            cmd.AppendSwitch(string.Join(Path.PathSeparator.ToString(), injars.Distinct().Select(s => '\'' + s + '\'')) + "\"");

            cmd.AppendSwitch("\"-libraryjars");
            cmd.AppendSwitch(string.Join(Path.PathSeparator.ToString(), libjars.Distinct().Select(s => '\'' + s + '\'')) + "\"");

            cmd.AppendSwitch("-outjars");
            cmd.AppendSwitch('"' + ProguardJarOutput + '"');

            if (EnableLogging)
            {
                cmd.AppendSwitchIfNotNull("-dump ", DumpOutput);
                cmd.AppendSwitchIfNotNull("-printseeds ", PrintSeedsOutput);
                cmd.AppendSwitchIfNotNull("-printusage ", PrintUsageOutput);
                cmd.AppendSwitchIfNotNull("-printmapping ", PrintMappingOutput);
            }

            return(cmd.ToString());
        }