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

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

            Directory.CreateDirectory(resPath);
            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' />");
            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> ();
            IBuildEngine engine = new MockBuildEngine(TestContext.Out, errors);

            CallAapt2Compile(engine, resPath);
            var outputFile = Path.Combine(path, "resources.apk");
            var task       = new Aapt2Link {
                BuildEngine                 = engine,
                ToolPath                    = GetPathToAapt2(),
                ResourceDirectories         = new ITaskItem [] { new TaskItem(resPath) },
                ManifestFiles               = new ITaskItem [] { new TaskItem(Path.Combine(path, "AndroidManifest.xml")) },
                CompiledResourceFlatArchive = new TaskItem(Path.Combine(path, "compiled.flata")),
                OutputFile                  = outputFile,
                AssemblyIdentityMapFile     = Path.Combine(path, "foo.map"),
                ExtraArgs                   = "--no-crunch "
            };

            Assert.False(task.Execute(), "task should have failed.");
            Assert.AreEqual(1, errors.Count, "One error should have been raised.");
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }
Пример #2
0
        public void Aapt2Link()
        {
            var path = Path.Combine(Root, "temp", "Aapt2Link");

            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 libPath = Path.Combine(path, "lp");

            Directory.CreateDirectory(libPath);
            Directory.CreateDirectory(Path.Combine(libPath, "0", "res", "values"));
            Directory.CreateDirectory(Path.Combine(libPath, "1", "res", "values"));
            File.WriteAllText(Path.Combine(libPath, "0", "res", "values", "strings.xml"), @"<?xml version='1.0' ?><resources><string name='foo1'>foo1</string></resources>");
            File.WriteAllText(Path.Combine(libPath, "1", "res", "values", "strings.xml"), @"<?xml version='1.0' ?><resources><string name='foo2'>foo2</string></resources>");
            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> ();
            IBuildEngine engine   = new MockBuildEngine(TestContext.Out, errors, warnings);
            var          archives = new List <ITaskItem>();

            CallAapt2Compile(engine, resPath, archivePath);
            CallAapt2Compile(engine, Path.Combine(libPath, "0", "res"), archivePath, archives);
            CallAapt2Compile(engine, Path.Combine(libPath, "1", "res"), archivePath, archives);
            int platform = 0;

            using (var b = new Builder()) {
                platform = b.GetMaxInstalledPlatform();
            }
            var outputFile = Path.Combine(path, "resources.apk");
            var task       = new Aapt2Link {
                BuildEngine                 = engine,
                ToolPath                    = GetPathToAapt2(),
                ResourceDirectories         = new ITaskItem [] { new TaskItem(resPath) },
                ManifestFiles               = new ITaskItem [] { new TaskItem(Path.Combine(path, "AndroidManifest.xml")) },
                AdditionalResourceArchives  = archives.ToArray(),
                CompiledResourceFlatArchive = new TaskItem(Path.Combine(archivePath, "compiled.flata")),
                OutputFile                  = outputFile,
                AssemblyIdentityMapFile     = Path.Combine(path, "foo.map"),
                JavaPlatformJarPath         = Path.Combine(AndroidSdkPath, "platforms", $"android-{platform}", "android.jar"),
            };

            Assert.True(task.Execute(), "task should have succeeded.");
            Assert.AreEqual(0, errors.Count, "There should be no errors.");
            Assert.LessOrEqual(0, warnings.Count, "There should be 0 warnings.");
            Assert.True(File.Exists(outputFile), $"{outputFile} should have been created.");
            using (var apk = ZipHelper.OpenZip(outputFile)) {
                Assert.AreEqual(3, apk.EntryCount, $"{outputFile} should have 3 entries.");
            }
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }
Пример #3
0
        public void CheckForInvalidVersionCode(string versionCode, string versionCodePattern, string errorCode)
        {
            var path = Path.Combine(Root, "temp", TestName);

            Directory.CreateDirectory(path);
            var referencePath = CreateFauxReferencesDirectory(Path.Combine(path, "references"), new [] {
                new ApiInfo {
                    Id = "27", Level = 27, Name = "Oreo", FrameworkVersion = "v8.1", Stable = true
                },
                new ApiInfo {
                    Id = "28", Level = 28, Name = "Pie", FrameworkVersion = "v9.0", Stable = true
                },
            });

            MonoAndroidHelper.RefreshSupportedVersions(new [] {
                Path.Combine(referencePath, "MonoAndroid"),
            });
            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' />");
            File.WriteAllText(Path.Combine(path, "AndroidManifest.xml"), $@"<?xml version='1.0' ?><manifest xmlns:android='http://schemas.android.com/apk/res/android' package='Foo.Foo' android:versionCode='{versionCode}' />");
            File.WriteAllText(Path.Combine(path, "foo.map"), @"a\nb");
            var          errors   = new List <BuildErrorEventArgs> ();
            IBuildEngine engine   = new MockBuildEngine(TestContext.Out, errors);
            var          archives = new List <ITaskItem> ();

            CallAapt2Compile(engine, resPath, archivePath, flatFilePath);
            var outputFile   = Path.Combine(path, "resources.apk");
            var manifestFile = Path.Combine(path, "AndroidManifest.xml");
            var task         = new Aapt2Link {
                BuildEngine                 = engine,
                ToolPath                    = GetPathToAapt2(),
                ResourceDirectories         = new ITaskItem [] { new TaskItem(resPath) },
                ManifestFiles               = new ITaskItem [] { new TaskItem(manifestFile) },
                CompiledResourceFlatArchive = new TaskItem(Path.Combine(path, "compiled.flata")),
                OutputFile                  = outputFile,
                AssemblyIdentityMapFile     = Path.Combine(path, "foo.map"),
                VersionCodePattern          = versionCodePattern,
            };

            Assert.False(task.Execute(), "task should have failed.");
            Assert.AreEqual(1, errors.Count, $"One error should have been raised. {string.Join (" ", errors.Select (e => e.Message))}");
            Assert.AreEqual(errorCode, errors [0].Code, $"Error Code should have been {errorCode}");
            Assert.AreEqual(manifestFile, errors [0].File, $"Error File should have been {manifestFile}");
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }
Пример #4
0
        public void Aapt2AndroidResgenExtraArgsAreSplit()
        {
            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(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' />");
            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> ();
            var          messages = new List <BuildMessageEventArgs> ();
            IBuildEngine engine   = new MockBuildEngine(TestContext.Out, errors, warnings, messages);
            var          archives = new List <ITaskItem>();

            CallAapt2Compile(engine, resPath, archivePath, flatFilePath);
            var    outputFile = Path.Combine(path, "resources.apk");
            int    platform   = 0;
            string emitids    = Path.Combine(path, "emitids.txt");
            string Rtxt       = Path.Combine(path, "R.txt");

            using (var b = new Builder()) {
                platform = b.GetMaxInstalledPlatform();
            }
            var task = new Aapt2Link {
                BuildEngine                 = engine,
                ToolPath                    = GetPathToAapt2(),
                ResourceDirectories         = new ITaskItem [] { new TaskItem(resPath) },
                ManifestFiles               = new ITaskItem [] { new TaskItem(Path.Combine(path, "AndroidManifest.xml")) },
                CompiledResourceFlatArchive = new TaskItem(Path.Combine(archivePath, "compiled.flata")),
                OutputFile                  = outputFile,
                AssemblyIdentityMapFile     = Path.Combine(path, "foo.map"),
                JavaPlatformJarPath         = Path.Combine(AndroidSdkPath, "platforms", $"android-{platform}", "android.jar"),
                ExtraArgs                   = $@"--no-version-vectors -v --emit-ids ""{emitids}"" --output-text-symbols '{Rtxt}'"
            };

            Assert.True(task.Execute(), $"task should have succeeded. {string.Join (" ", errors.Select (e => e.Message))}");
            Assert.AreEqual(0, errors.Count, $"No errors should have been raised. {string.Join (" ", errors.Select (e => e.Message))}");
            Assert.True(File.Exists(emitids), $"{emitids} should have been created.");
            Assert.True(File.Exists(Rtxt), $"{Rtxt} should have been created.");
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }
Пример #5
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);
        }
Пример #6
0
        public void Aapt2Link([Values(true, false)] bool compilePerFile)
        {
            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>");
            File.WriteAllText(Path.Combine(resPath, "layout", "main.xml"), @"<?xml version='1.0' ?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' />");
            var libPath = Path.Combine(path, "lp");

            Directory.CreateDirectory(libPath);
            Directory.CreateDirectory(Path.Combine(libPath, "0", "res", "values"));
            Directory.CreateDirectory(Path.Combine(libPath, "1", "res", "values"));
            File.WriteAllText(Path.Combine(libPath, "0", "res", "values", "strings.xml"), @"<?xml version='1.0' ?><resources><string name='foo1'>foo1</string></resources>");
            File.WriteAllText(Path.Combine(libPath, "1", "res", "values", "strings.xml"), @"<?xml version='1.0' ?><resources><string name='foo2'>foo2</string></resources>");
            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> ();
            IBuildEngine engine   = new MockBuildEngine(TestContext.Out, errors, warnings);
            var          archives = new List <ITaskItem>();

            if (compilePerFile)
            {
                foreach (var file in Directory.EnumerateFiles(resPath, "*.*", SearchOption.AllDirectories))
                {
                    CallAapt2Compile(engine, file, archivePath, flatFilePath);
                }
            }
            else
            {
                CallAapt2Compile(engine, resPath, archivePath, flatFilePath);
            }
            CallAapt2Compile(engine, Path.Combine(libPath, "0", "res"), archivePath, flatFilePath, archives);
            CallAapt2Compile(engine, Path.Combine(libPath, "1", "res"), archivePath, flatFilePath, archives);
            List <ITaskItem> items = new List <ITaskItem> ();

            if (compilePerFile)
            {
                // collect all the flat archives
                foreach (var file in Directory.EnumerateFiles(flatFilePath, "*.flat", SearchOption.AllDirectories))
                {
                    items.Add(new TaskItem(file));
                }
            }
            int platform   = AndroidSdkResolver.GetMaxInstalledPlatform();
            var outputFile = Path.Combine(path, "resources.apk");
            var task       = new Aapt2Link {
                BuildEngine                = engine,
                ToolPath                   = GetPathToAapt2(),
                ResourceDirectories        = new ITaskItem [] { new TaskItem(resPath) },
                ManifestFiles              = new ITaskItem [] { new TaskItem(Path.Combine(path, "AndroidManifest.xml")) },
                AdditionalResourceArchives = !compilePerFile?archives.ToArray() : null,
                                                 CompiledResourceFlatArchive = !compilePerFile ? new TaskItem(Path.Combine(archivePath, "compiled.flata")) : null,
                                                 CompiledResourceFlatFiles   = compilePerFile ? items.ToArray() : null,
                                                 OutputFile = outputFile,
                                                 AssemblyIdentityMapFile = Path.Combine(path, "foo.map"),
                                                 JavaPlatformJarPath     = Path.Combine(AndroidSdkPath, "platforms", $"android-{platform}", "android.jar"),
            };

            Assert.True(task.Execute(), $"task should have succeeded. {string.Join (";", errors.Select (x => x.Message))}");
            Assert.AreEqual(0, errors.Count, "There should be no errors.");
            Assert.LessOrEqual(0, warnings.Count, "There should be 0 warnings.");
            Assert.True(File.Exists(outputFile), $"{outputFile} should have been created.");
            using (var apk = ZipHelper.OpenZip(outputFile)) {
                Assert.AreEqual(3, apk.EntryCount, $"{outputFile} should have 3 entries.");
            }
            Directory.Delete(Path.Combine(Root, path), recursive: true);
        }