Пример #1
0
        public void Aapt2Compile()
        {
            var path = Path.Combine(Root, "temp", "Aapt2Compile");

            Directory.CreateDirectory(path);
            var resPath     = Path.Combine(path, "res");
            var archivePath = Path.Combine(path, "flata");

            Directory.CreateDirectory(resPath);
            Directory.CreateDirectory(archivePath);
            Directory.CreateDirectory(Path.Combine(resPath, "values"));
            Directory.CreateDirectory(Path.Combine(resPath, "layout"));
            File.WriteAllText(Path.Combine(resPath, "values", "strings.xml"), @"<?xml version='1.0' ?><resources><string name='foo'>foo</string></resources>");
            File.WriteAllText(Path.Combine(resPath, "layout", "main.xml"), @"<?xml version='1.0' ?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' />");
            var          errors = new List <BuildErrorEventArgs> ();
            IBuildEngine engine = new MockBuildEngine(TestContext.Out, errors);
            var          task   = new Aapt2Compile {
                BuildEngine           = engine,
                ToolPath              = GetPathToAapt2(),
                ResourceDirectories   = new ITaskItem [] { new TaskItem(resPath) },
                FlatArchivesDirectory = archivePath,
            };

            Assert.True(task.Execute(), "task should have succeeded.");
            var flatArchive = Path.Combine(archivePath, "compiled.flata");

            Assert.True(File.Exists(flatArchive), $"{flatArchive} should have been created.");
            using (var apk = ZipHelper.OpenZip(flatArchive)) {
                Assert.AreEqual(2, apk.EntryCount, $"{flatArchive} should have 2 entries.");
            }
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }
Пример #2
0
        public void Aapt2CompileFixesUpErrors()
        {
            var path = Path.Combine(Root, "temp", "Aapt2CompileFixesUpErrors");

            Directory.CreateDirectory(path);
            var resPath      = Path.Combine(path, "res");
            var archivePath  = Path.Combine(path, "flata");
            var flatFilePath = Path.Combine(path, "flat");

            Directory.CreateDirectory(resPath);
            Directory.CreateDirectory(archivePath);
            Directory.CreateDirectory(Path.Combine(resPath, "values"));
            Directory.CreateDirectory(Path.Combine(resPath, "layout"));
            File.WriteAllText(Path.Combine(resPath, "values", "strings.xml"), @"<?xml version='1.0' ?><resources><string name='foo'>foo</string</resources>");
            File.WriteAllText(Path.Combine(resPath, "layout", "main.xml"), @"<?xml version='1.0' ?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
	android:orientation='vertical'
	android:layout_width='fill_parent'
	android:layout_height='fill_parent'
	>
<Button  
	android:id='@+id/myButton'
	android:layout_width='fill_parent' 
	android:layout_height='wrap_content' 
	android:text='@string/hello'
	/>
</LinearLayout>
");
            var          errors             = new List <BuildErrorEventArgs> ();
            IBuildEngine engine             = new MockBuildEngine(TestContext.Out, errors);
            var          directorySeperator = Path.DirectorySeparatorChar;
            var          current            = Directory.GetCurrentDirectory();

            try {
                Directory.SetCurrentDirectory(path);
                var task = new Aapt2Compile {
                    BuildEngine         = engine,
                    ToolPath            = GetPathToAapt2(),
                    ResourceDirectories = new ITaskItem [] {
                        new TaskItem(resPath, new Dictionary <string, string> ()
                        {
                            { "ResourceDirectory", resPath },
                        }
                                     )
                    },
                    FlatArchivesDirectory = archivePath,
                    FlatFilesDirectory    = flatFilePath,
                    ResourceNameCaseMap   = $"Layout{directorySeperator}Main.xml|layout{directorySeperator}main.axml;Values{directorySeperator}Strings.xml|values{directorySeperator}strings.xml",
                };
                Assert.False(task.Execute(), "task should not have succeeded.");
            } finally {
                Directory.SetCurrentDirectory(current);
            }
            Assert.AreEqual(2, errors.Count, $"Two Error should have been raised. {string.Join (" ", errors.Select (e => e.Message))}");
            Assert.AreEqual($"Resources{directorySeperator}Values{directorySeperator}Strings.xml", errors[0].File, $"`values{directorySeperator}strings.xml` should have been replaced with `Resources{directorySeperator}Values{directorySeperator}Strings.xml`");
            Assert.AreEqual($"Resources{directorySeperator}Values{directorySeperator}Strings.xml", errors [1].File, $"`values{directorySeperator}strings.xml` should have been replaced with `Resources{directorySeperator}Values{directorySeperator}Strings.xml`");
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }
Пример #3
0
        void CallAapt2Compile(IBuildEngine engine, string dir)
        {
            var errors = new List <BuildErrorEventArgs> ();
            var task   = new Aapt2Compile {
                BuildEngine         = engine,
                ToolPath            = GetPathToAapt2(),
                ResourceDirectories = new ITaskItem [] { new TaskItem(dir) },
            };

            Assert.True(task.Execute(), "task should have succeeded.");
        }
Пример #4
0
        public void Aapt2DaemonInstances(int maxInstances, int expectedMax, int expectedInstances, int numLayouts)
        {
            var path = Path.Combine(Root, "temp", TestName);

            Directory.CreateDirectory(path);
            var resPath      = Path.Combine(path, "res");
            var archivePath  = Path.Combine(path, "flata");
            var flatFilePath = Path.Combine(path, "flat");

            Directory.CreateDirectory(resPath);
            Directory.CreateDirectory(archivePath);
            Directory.CreateDirectory(flatFilePath);
            Directory.CreateDirectory(Path.Combine(resPath, "values"));
            Directory.CreateDirectory(Path.Combine(resPath, "layout"));
            File.WriteAllText(Path.Combine(resPath, "values", "strings.xml"), @"<?xml version='1.0' ?><resources><string name='foo'>foo</string></resources>");
            for (int i = 0; i < numLayouts; i++)
            {
                File.WriteAllText(Path.Combine(resPath, "layout", $"main{i}.xml"), @"<?xml version='1.0' ?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' />");
            }
            File.WriteAllText(Path.Combine(path, "AndroidManifest.xml"), @"<?xml version='1.0' ?><manifest xmlns:android='http://schemas.android.com/apk/res/android' package='Foo.Foo' />");
            File.WriteAllText(Path.Combine(path, "foo.map"), @"a\nb");
            var errors              = new List <BuildErrorEventArgs> ();
            var warnings            = new List <BuildWarningEventArgs> ();
            List <ITaskItem> files  = new List <ITaskItem> ();
            IBuildEngine4    engine = new MockBuildEngine(System.Console.Out, errors, warnings);

            files.Add(CreateTaskItemForResourceFile(resPath, "values", "strings.xml"));
            for (int i = 0; i < numLayouts; i++)
            {
                files.Add(CreateTaskItemForResourceFile(resPath, "layout", $"main{i}.xml"));
            }
            var task = new Aapt2Compile {
                BuildEngine            = engine,
                ToolPath               = GetPathToAapt2(),
                ResourcesToCompile     = files.ToArray(),
                ResourceDirectories    = new ITaskItem [] { new TaskItem(resPath) },
                FlatArchivesDirectory  = archivePath,
                FlatFilesDirectory     = flatFilePath,
                DaemonMaxInstanceCount = maxInstances,
                DaemonKeepInDomain     = false,
            };

            Assert.True(task.Execute(), $"task should have succeeded. {string.Join (";", errors.Select (x => x.Message))}");
            Aapt2Daemon daemon = (Aapt2Daemon)engine.GetRegisteredTaskObject(typeof(Aapt2Daemon).FullName, RegisteredTaskObjectLifetime.Build);

            Assert.IsNotNull(daemon, "Should have got a Daemon");
            Assert.AreEqual(expectedMax, daemon.MaxInstances, $"Expected {expectedMax} but was {daemon.MaxInstances}");
            Assert.AreEqual(expectedInstances, daemon.CurrentInstances, $"Expected {expectedInstances} but was {daemon.CurrentInstances}");
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }
Пример #5
0
        void CallAapt2Compile(IBuildEngine engine, string dir, string outputPath, List <ITaskItem> output = null)
        {
            var errors = new List <BuildErrorEventArgs> ();
            var task   = new Aapt2Compile {
                BuildEngine         = engine,
                ToolPath            = GetPathToAapt2(),
                ResourceDirectories = new ITaskItem [] {
                    new TaskItem(dir, new Dictionary <string, string> {
                        { "Hash", output != null ? GetHash(dir) : "compiled" }
                    }),
                },
                FlatArchivesDirectory = outputPath,
            };

            Assert.True(task.Execute(), "task should have succeeded.");
            output?.AddRange(task.CompiledResourceFlatArchives);
        }
Пример #6
0
        public void Aapt2CompileFiles()
        {
            var path = Path.Combine(Root, "temp", "Aapt2CompileFiles");

            Directory.CreateDirectory(path);
            var resPath      = Path.Combine(path, "res");
            var archivePath  = Path.Combine(path, "flata");
            var flatFilePath = Path.Combine(path, "flat");

            Directory.CreateDirectory(resPath);
            Directory.CreateDirectory(archivePath);
            Directory.CreateDirectory(flatFilePath);
            Directory.CreateDirectory(Path.Combine(resPath, "values"));
            Directory.CreateDirectory(Path.Combine(resPath, "layout"));
            File.WriteAllText(Path.Combine(resPath, "values", "strings.xml"), @"<?xml version='1.0' ?><resources><string name='foo'>foo</string></resources>");
            File.WriteAllText(Path.Combine(resPath, "layout", "main.xml"), @"<?xml version='1.0' ?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' />");
            List <ITaskItem> files = new List <ITaskItem> ();

            files.Add(CreateTaskItemForResourceFile(resPath, "values", "strings.xml"));
            files.Add(CreateTaskItemForResourceFile(resPath, "layout", "main.xml"));
            var          errors = new List <BuildErrorEventArgs> ();
            IBuildEngine engine = new MockBuildEngine(TestContext.Out, errors);
            var          task   = new Aapt2Compile {
                BuildEngine           = engine,
                ToolPath              = GetPathToAapt2(),
                ResourcesToCompile    = files.ToArray(),
                ResourceDirectories   = new ITaskItem [] { new TaskItem(resPath) },
                FlatArchivesDirectory = archivePath,
                FlatFilesDirectory    = flatFilePath,
            };

            Assert.True(task.Execute(), $"task should have succeeded. {string.Join (";", errors.Select (x => x.Message))}");
            var flatArchive = Path.Combine(archivePath, "compiled.flata");

            Assert.False(File.Exists(flatArchive), $"{flatArchive} should not have been created.");
            foreach (var file in files)
            {
                string f = Path.Combine(flatFilePath, file.GetMetadata("_FlatFile"));
                Assert.True(File.Exists(f), $"{f} should have been created.");
            }
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }
Пример #7
0
        void CallAapt2Compile(IBuildEngine engine, string dir, string outputPath, string flatFilePath, List <ITaskItem> output = null, int maxInstances = 0, bool keepInDomain = false)
        {
            var       errors = new List <BuildErrorEventArgs> ();
            ITaskItem item;

            if (File.Exists(dir))
            {
                item = CreateTaskItemForResourceFile(dir);
            }
            else
            {
                item = new TaskItem(dir, new Dictionary <string, string> {
                    { "ResourceDirectory", dir },
                    { "Hash", output != null ? Files.HashString(dir) : "compiled" },
                    { "_FlatFile", output != null ? Files.HashString(dir) + ".flata" : "compiled.flata" },
                    { "_ArchiveDirectory", outputPath }
                });
            }
            var task = new Aapt2Compile {
                BuildEngine        = engine,
                ToolPath           = GetPathToAapt2(),
                ResourcesToCompile = new ITaskItem [] {
                    item,
                },
                ResourceDirectories = new ITaskItem [] {
                    new TaskItem(dir),
                },
                FlatArchivesDirectory  = outputPath,
                FlatFilesDirectory     = flatFilePath,
                DaemonMaxInstanceCount = maxInstances,
                DaemonKeepInDomain     = keepInDomain,
            };
            MockBuildEngine mockEngine = (MockBuildEngine)engine;

            Assert.True(task.Execute(), $"task should have succeeded. {string.Join (" ", mockEngine.Errors.Select (x => x.Message))}");
            output?.AddRange(task.CompiledResourceFlatArchives);
        }
Пример #8
0
        public void CompareAapt2AndManagedParserOutput()
        {
            var path = Path.Combine("temp", TestName);

            CreateResourceDirectory(path);
            File.WriteAllText(Path.Combine(Root, path, "foo.map"), @"a\nb");
            Directory.CreateDirectory(Path.Combine(Root, path, "java"));
            IBuildEngine engine       = new MockBuildEngine(TestContext.Out);
            var          aapt2Compile = new Aapt2Compile {
                BuildEngine         = engine,
                ToolPath            = GetPathToAapt2(),
                ResourceDirectories = new ITaskItem [] {
                    new TaskItem(Path.Combine(Root, path, "lp", "res"), new Dictionary <string, string> {
                        { "Hash", "lp" }
                    }),
                    new TaskItem(Path.Combine(Root, path, "res"), new Dictionary <string, string> {
                        { "Hash", "compiled" }
                    }),
                },
                FlatArchivesDirectory = Path.Combine(Root, path),
            };

            Assert.IsTrue(aapt2Compile.Execute(), "Aapt2 Compile should have succeeded.");
            int platform = 0;

            using (var b = new Builder()) {
                platform = b.GetMaxInstalledPlatform();
            }
            string resPath   = Path.Combine(Root, path, "res");
            string rTxt      = Path.Combine(Root, path, "R.txt");
            var    aapt2Link = new Aapt2Link {
                BuildEngine                 = engine,
                ToolPath                    = GetPathToAapt2(),
                ResourceDirectories         = new ITaskItem [] { new TaskItem(resPath) },
                ManifestFiles               = new ITaskItem [] { new TaskItem(Path.Combine(Root, path, "AndroidManifest.xml")) },
                AdditionalResourceArchives  = new ITaskItem [] { new TaskItem(Path.Combine(Root, path, "lp.flata")) },
                CompiledResourceFlatArchive = new TaskItem(Path.Combine(Root, path, "compiled.flata")),
                OutputFile                  = Path.Combine(Root, path, "foo.apk"),
                AssemblyIdentityMapFile     = Path.Combine(Root, path, "foo.map"),
                JavaPlatformJarPath         = Path.Combine(AndroidSdkDirectory, "platforms", $"android-{platform}", "android.jar"),
                JavaDesignerOutputDirectory = Path.Combine(Root, path, "java"),
                ResourceSymbolsTextFile     = rTxt,
            };

            Assert.IsTrue(aapt2Link.Execute(), "Aapt2 Link should have succeeded.");

            FileAssert.Exists(rTxt, $"{rTxt} should have been created.");

            var task = new GenerateResourceDesigner {
                BuildEngine = engine
            };

            task.UseManagedResourceGenerator = true;
            task.DesignTimeBuild             = false;
            task.Namespace           = "MonoAndroidApplication4.MonoAndroidApplication4";
            task.NetResgenOutputFile = Path.Combine(Root, path, "Resource.designer.aapt2.cs");
            task.ProjectDir          = Path.Combine(Root, path);
            task.ResourceDirectory   = Path.Combine(Root, path, "res") + Path.DirectorySeparatorChar;
            task.Resources           = new TaskItem [] {
                new TaskItem(Path.Combine(Root, path, "res", "values", "strings.xml"), new Dictionary <string, string> ()
                {
                    { "LogicalName", "values\\strings.xml" },
                }),
            };
            task.AdditionalResourceDirectories = new TaskItem [] {
                new TaskItem(Path.Combine(Root, path, "lp", "res")),
            };
            task.ResourceFlagFile    = Path.Combine(Root, path, "AndroidResgen.flag");
            task.IsApplication       = true;
            task.JavaPlatformJarPath = aapt2Link.JavaPlatformJarPath;
            Assert.IsTrue(task.Execute(), "Task should have executed successfully.");

            File.WriteAllText(task.ResourceFlagFile, string.Empty);
            File.Delete(Path.Combine(Root, path, "R.txt.bak"));
            File.Move(rTxt, Path.Combine(Root, path, "R.txt.bak"));

            task.UseManagedResourceGenerator = true;
            task.DesignTimeBuild             = true;
            task.NetResgenOutputFile         = Path.Combine(Root, path, "Resource.designer.managed.cs");
            Assert.IsTrue(task.Execute(), "Task should have executed successfully.");
            string aapt2Designer   = Path.Combine(Root, path, "Resource.designer.aapt2.cs");
            string managedDesigner = Path.Combine(Root, path, "Resource.designer.managed.cs");

            CompareFilesIgnoreRuntimeInfoString(managedDesigner, aapt2Designer);
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }