Пример #1
0
        public void BuildApplicationWithMonoEnvironment([Values("", "Normal", "Offline")] string sequencePointsMode)
        {
            const string supportedAbis = "armeabi-v7a;x86";

            var lib = new XamarinAndroidLibraryProject {
                ProjectName     = "Library1",
                IsRelease       = true,
                OtherBuildItems = { new AndroidItem.AndroidEnvironment("Mono.env")
                                    {
                                        TextContent = () => "MONO_DEBUG=soft-breakpoints"
                                    }, },
            };
            var app = new XamarinFormsAndroidApplicationProject()
            {
                IsRelease = true,
                AndroidLinkModeRelease = AndroidLinkMode.Full,
                References             =
                {
                    new BuildItem("ProjectReference", "..\\Library1\\Library1.csproj"),
                },
            };
            //LinkSkip one assembly that contains __AndroidLibraryProjects__.zip
            string linkSkip = KnownPackages.SupportV7AppCompat_27_0_2_1.Id;

            app.SetProperty("AndroidLinkSkip", linkSkip);
            app.SetProperty("_AndroidSequencePointsMode", sequencePointsMode);
            app.SetProperty(app.ReleaseProperties, KnownProperties.AndroidSupportedAbis, supportedAbis);
            using (var libb = CreateDllBuilder(Path.Combine("temp", TestName, lib.ProjectName)))
                using (var appb = CreateApkBuilder(Path.Combine("temp", TestName, app.ProjectName))) {
                    Assert.IsTrue(libb.Build(lib), "Library build should have succeeded.");
                    Assert.IsTrue(appb.Build(app), "App should have succeeded.");
                    Assert.IsTrue(StringAssertEx.ContainsText(appb.LastBuildOutput, $"Save assembly: {linkSkip}"), $"{linkSkip} should be saved, and not linked!");

                    string        intermediateOutputDir = Path.Combine(Root, appb.ProjectDirectory, app.IntermediateOutputPath);
                    List <string> envFiles = EnvironmentHelper.GatherEnvironmentFiles(intermediateOutputDir, supportedAbis, true);
                    Dictionary <string, string> envvars = EnvironmentHelper.ReadEnvironmentVariables(envFiles);
                    Assert.IsTrue(envvars.Count > 0, $"No environment variables defined");

                    string monoDebugVar;
                    Assert.IsTrue(envvars.TryGetValue("MONO_DEBUG", out monoDebugVar), "Environment should contain MONO_DEBUG");
                    Assert.IsFalse(String.IsNullOrEmpty(monoDebugVar), "Environment must contain MONO_DEBUG with a value");
                    Assert.IsTrue(monoDebugVar.IndexOf("soft-breakpoints") >= 0, "Environment must contain MONO_DEBUG with 'soft-breakpoints' in its value");

                    if (!String.IsNullOrEmpty(sequencePointsMode))
                    {
                        Assert.IsTrue(monoDebugVar.IndexOf("gen-compact-seq-points") >= 0, "The values from Mono.env should have been merged into environment");
                    }

                    EnvironmentHelper.AssertValidEnvironmentSharedLibrary(intermediateOutputDir, AndroidSdkPath, AndroidNdkPath, supportedAbis);

                    var assemblyDir = Path.Combine(Root, appb.ProjectDirectory, app.IntermediateOutputPath, "android", "assets");
                    var rp          = new ReaderParameters {
                        ReadSymbols = false
                    };
                    foreach (var assemblyFile in Directory.EnumerateFiles(assemblyDir, "*.dll"))
                    {
                        using (var assembly = AssemblyDefinition.ReadAssembly(assemblyFile)) {
                            foreach (var module in assembly.Modules)
                            {
                                var resources = module.Resources.Select(r => r.Name).ToArray();
                                Assert.IsFalse(StringAssertEx.ContainsText(resources, "__AndroidEnvironment__"), "AndroidEnvironment EmbeddedResource should be stripped!");
                                Assert.IsFalse(StringAssertEx.ContainsText(resources, "__AndroidLibraryProjects__.zip"), "__AndroidLibraryProjects__.zip should be stripped!");
                                Assert.IsFalse(StringAssertEx.ContainsText(resources, "__AndroidNativeLibraries__.zip"), "__AndroidNativeLibraries__.zip should be stripped!");
                            }
                        }
                    }
                }
        }
Пример #2
0
        public void DotNetBuildLibrary(bool isRelease, bool duplicateAar)
        {
            var path    = Path.Combine("temp", TestName);
            var env_var = "MY_ENVIRONMENT_VAR";
            var env_val = "MY_VALUE";

            // Setup dependencies App A -> Lib B -> Lib C

            var libC = new XASdkProject(outputType: "Library")
            {
                ProjectName = "LibraryC",
                IsRelease   = isRelease,
                Sources     =
                {
                    new BuildItem.Source("Bar.cs")
                    {
                        TextContent = () => "public class Bar { }",
                    }
                }
            };

            libC.OtherBuildItems.Add(new AndroidItem.AndroidAsset("Assets\\bar\\bar.txt")
            {
                BinaryContent = () => Array.Empty <byte> (),
            });
            var activity = libC.Sources.FirstOrDefault(s => s.Include() == "MainActivity.cs");

            if (activity != null)
            {
                libC.Sources.Remove(activity);
            }
            var libCBuilder = CreateDotNetBuilder(libC, Path.Combine(path, libC.ProjectName));

            Assert.IsTrue(libCBuilder.Build(), $"{libC.ProjectName} should succeed");

            var libB = new XASdkProject(outputType: "Library")
            {
                ProjectName = "LibraryB",
                IsRelease   = isRelease,
                Sources     =
                {
                    new BuildItem.Source("Foo.cs")
                    {
                        TextContent = () => "public class Foo : Bar { }",
                    }
                }
            };

            libB.OtherBuildItems.Add(new AndroidItem.AndroidAsset("Assets\\foo\\foo.txt")
            {
                BinaryContent = () => Array.Empty <byte> (),
            });
            libB.OtherBuildItems.Add(new AndroidItem.AndroidResource("Resources\\raw\\bar.txt")
            {
                BinaryContent = () => Array.Empty <byte> (),
            });
            libB.OtherBuildItems.Add(new AndroidItem.AndroidEnvironment("env.txt")
            {
                TextContent = () => $"{env_var}={env_val}",
            });
            libB.OtherBuildItems.Add(new AndroidItem.AndroidEnvironment("sub\\directory\\env.txt")
            {
                TextContent = () => $"{env_var}={env_val}",
            });
            libB.OtherBuildItems.Add(new AndroidItem.AndroidLibrary("sub\\directory\\foo.jar")
            {
                BinaryContent = () => Convert.FromBase64String(InlineData.JavaClassesJarBase64),
            });
            libB.OtherBuildItems.Add(new AndroidItem.AndroidLibrary("sub\\directory\\arm64-v8a\\libfoo.so")
            {
                BinaryContent = () => Array.Empty <byte> (),
            });
            libB.OtherBuildItems.Add(new AndroidItem.AndroidLibrary("libfoo.so")
            {
                MetadataValues = "Link=x86\\libfoo.so",
                BinaryContent  = () => Array.Empty <byte> (),
            });
            libB.AddReference(libC);

            activity = libB.Sources.FirstOrDefault(s => s.Include() == "MainActivity.cs");
            if (activity != null)
            {
                libB.Sources.Remove(activity);
            }
            var libBBuilder = CreateDotNetBuilder(libB, Path.Combine(path, libB.ProjectName));

            Assert.IsTrue(libBBuilder.Build(), $"{libB.ProjectName} should succeed");

            // Check .aar file for class library
            var aarPath = Path.Combine(FullProjectDirectory, libB.OutputPath, $"{libB.ProjectName}.aar");

            FileAssert.Exists(aarPath);
            using (var aar = ZipHelper.OpenZip(aarPath)) {
                aar.AssertContainsEntry(aarPath, "assets/foo/foo.txt");
                aar.AssertContainsEntry(aarPath, "res/raw/bar.txt");
                aar.AssertContainsEntry(aarPath, ".netenv/190E30B3D205731E.env");
                aar.AssertContainsEntry(aarPath, ".netenv/2CBDAB7FEEA94B19.env");
                aar.AssertContainsEntry(aarPath, "libs/A1AFA985571E728E.jar");
                aar.AssertContainsEntry(aarPath, "jni/arm64-v8a/libfoo.so");
                aar.AssertContainsEntry(aarPath, "jni/x86/libfoo.so");
            }

            // Check EmbeddedResource files do not exist
            var assemblyPath = Path.Combine(FullProjectDirectory, libB.OutputPath, $"{libB.ProjectName}.dll");

            FileAssert.Exists(assemblyPath);
            using (var assembly = AssemblyDefinition.ReadAssembly(assemblyPath)) {
                Assert.AreEqual(0, assembly.MainModule.Resources.Count);
            }

            var appA = new XASdkProject {
                ProjectName = "AppA",
                IsRelease   = isRelease,
                Sources     =
                {
                    new BuildItem.Source("Bar.cs")
                    {
                        TextContent = () => "public class Bar : Foo { }",
                    }
                }
            };

            appA.AddReference(libB);
            if (duplicateAar)
            {
                // Test a duplicate @(AndroidLibrary) item with the same path of LibraryB.aar
                appA.OtherBuildItems.Add(new AndroidItem.AndroidLibrary(aarPath));
            }
            var appBuilder = CreateDotNetBuilder(appA, Path.Combine(path, appA.ProjectName));

            Assert.IsTrue(appBuilder.Build(), $"{appA.ProjectName} should succeed");

            // Check .apk for assets, res, and native libraries
            var apkPath = Path.Combine(FullProjectDirectory, appA.OutputPath, $"{appA.PackageName}.apk");

            FileAssert.Exists(apkPath);
            using (var apk = ZipHelper.OpenZip(apkPath)) {
                apk.AssertContainsEntry(apkPath, "assets/foo/foo.txt");
                apk.AssertContainsEntry(apkPath, "assets/bar/bar.txt");
                apk.AssertContainsEntry(apkPath, "res/raw/bar.txt");
                apk.AssertContainsEntry(apkPath, "lib/arm64-v8a/libfoo.so");
                apk.AssertContainsEntry(apkPath, "lib/x86/libfoo.so");
            }

            // Check classes.dex contains foo.jar
            var intermediate = Path.Combine(FullProjectDirectory, appA.IntermediateOutputPath);
            var dexFile      = Path.Combine(intermediate, "android", "bin", "classes.dex");

            FileAssert.Exists(dexFile);
            string className = "Lcom/xamarin/android/test/msbuildtest/JavaSourceJarTest;";

            Assert.IsTrue(DexUtils.ContainsClass(className, dexFile, AndroidSdkPath), $"`{dexFile}` should include `{className}`!");

            // Check environment variable
            var environmentFiles     = EnvironmentHelper.GatherEnvironmentFiles(intermediate, "x86", required: true);
            var environmentVariables = EnvironmentHelper.ReadEnvironmentVariables(environmentFiles);

            Assert.IsTrue(environmentVariables.TryGetValue(env_var, out string actual), $"Environment should contain {env_var}");
            Assert.AreEqual(env_val, actual, $"{env_var} should be {env_val}");
        }