예제 #1
0
        void AddNativeLibrariesFromAssemblies(ZipArchiveEx apk, string supportedAbis)
        {
            int count = 0;
            var abis  = supportedAbis.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);

            using (var res = new DirectoryAssemblyResolver(this.CreateTaskLogger(), loadDebugSymbols: false)) {
                foreach (var assembly in EmbeddedNativeLibraryAssemblies)
                {
                    res.Load(assembly.ItemSpec);
                }
                foreach (var assemblyPath in EmbeddedNativeLibraryAssemblies)
                {
                    var assembly = res.GetAssembly(assemblyPath.ItemSpec);
                    foreach (var mod in assembly.Modules)
                    {
                        var ressozip = mod.Resources.FirstOrDefault(r => r.Name == "__AndroidNativeLibraries__.zip") as EmbeddedResource;
                        if (ressozip == null)
                        {
                            continue;
                        }
                        var data = ressozip.GetResourceData();
                        using (var ms = new MemoryStream(data)) {
                            using (var zip = ZipArchive.Open(ms)) {
                                foreach (var e in zip.Where(x => abis.Any(a => x.FullName.Contains($"/{a}/"))))
                                {
                                    if (e.IsDirectory)
                                    {
                                        continue;
                                    }
                                    var key = e.FullName.Replace("native_library_imports", "lib");
                                    if (apk.Archive.Any(k => k.FullName == key))
                                    {
                                        Log.LogCodedWarning("4301", "Apk already contains the item {0}; ignoring.", key);
                                        continue;
                                    }
                                    using (var s = new MemoryStream()) {
                                        e.Extract(s);
                                        s.Position = 0;
                                        apk.Archive.AddEntry(s.ToArray(), key);
                                    }
                                }
                            }
                        }
                    }
                    count++;
                    if (count == ZipArchiveEx.ZipFlushLimit)
                    {
                        apk.Flush();
                        count = 0;
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// aapt2 is putting AndroidManifest.xml in the root of the archive instead of at manifest/AndroidManifest.xml that bundletool expects.
        /// I see no way to change this behavior, so we can move the file for now:
        /// https://github.com/aosp-mirror/platform_frameworks_base/blob/e80b45506501815061b079dcb10bf87443bd385d/tools/aapt2/LoadedApk.h#L34
        /// </summary>
        protected override void FixupArchive(ZipArchiveEx zip)
        {
            var entry = zip.Archive.ReadEntry("AndroidManifest.xml");

            using (var stream = new MemoryStream()) {
                entry.Extract(stream);
                stream.Position = 0;
                zip.Archive.AddEntry("manifest/AndroidManifest.xml", stream);
                zip.Archive.DeleteEntry(entry);
                zip.Flush();
            }
        }
예제 #3
0
        /// <summary>
        /// aapt2 is putting AndroidManifest.xml in the root of the archive instead of at manifest/AndroidManifest.xml that bundletool expects.
        /// I see no way to change this behavior, so we can move the file for now:
        /// https://github.com/aosp-mirror/platform_frameworks_base/blob/e80b45506501815061b079dcb10bf87443bd385d/tools/aapt2/LoadedApk.h#L34
        /// </summary>
        protected override void FixupArchive(ZipArchiveEx zip)
        {
            if (!zip.Archive.ContainsEntry("AndroidManifest.xml"))
            {
                Log.LogDebugMessage($"No AndroidManifest.xml. Skipping Fixup");
                return;
            }
            var entry = zip.Archive.ReadEntry("AndroidManifest.xml");

            Log.LogDebugMessage($"Fixing up AndroidManifest.xml to be manifest/AndroidManifest.xml.");
            using (var stream = new MemoryStream()) {
                entry.Extract(stream);
                stream.Position = 0;
                zip.Archive.AddEntry("manifest/AndroidManifest.xml", stream);
                zip.Archive.DeleteEntry(entry);
                zip.Flush();
            }
        }
예제 #4
0
        private void AddGdbservers(ZipArchiveEx apk, ArchiveFileList files, string supportedAbis, AndroidDebugServer debugServer)
        {
            if (string.IsNullOrEmpty(AndroidNdkDirectory))
            {
                return;
            }

            var sdkBinDirectory = MonoAndroidHelper.GetOSBinPath();
            int count           = 0;

            foreach (var sabi in supportedAbis.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var arch = GdbPaths.GetArchFromAbi(sabi);
                var abi  = GdbPaths.GetAbiFromArch(arch);
                if (abi == null)
                {
                    continue;
                }
                var debugServerFile = GdbPaths.GetDebugServerFileName(debugServer);
                if (files.Any(f => string.Equals(Path.GetFileName(f.Item1), debugServerFile, StringComparison.Ordinal) &&
                              string.Equals(f.Item2, "lib/" + sabi, StringComparison.Ordinal)))
                {
                    continue;
                }
                var entryName       = string.Format("lib/{0}/{1}", sabi, debugServerFile);
                var debugServerPath = GdbPaths.GetDebugServerPath(debugServer, arch, AndroidNdkDirectory, sdkBinDirectory);
                if (!File.Exists(debugServerPath))
                {
                    continue;
                }
                Log.LogDebugMessage("Adding {0} debug server '{1}' to the APK as '{2}'", sabi, debugServerPath, entryName);
                apk.Archive.AddEntry(entryName, File.OpenRead(debugServerPath));
                count++;
                if (count == ZipArchiveEx.ZipFlushLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }
        }
예제 #5
0
        void AddAssemblies(ZipArchiveEx apk, bool debug, bool compress, IDictionary <string, CompressedAssemblyInfo> compressedAssembliesInfo)
        {
            string sourcePath;

            AssemblyCompression.AssemblyData compressedAssembly = null;
            string compressedOutputDir = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(ApkOutputPath), "..", "lz4"));

            int count = 0;

            foreach (ITaskItem assembly in ResolvedUserAssemblies)
            {
                if (bool.TryParse(assembly.GetMetadata("AndroidSkipAddToPackage"), out bool value) && value)
                {
                    Log.LogDebugMessage($"Skipping {assembly.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' ");
                    continue;
                }
                if (MonoAndroidHelper.IsReferenceAssembly(assembly.ItemSpec))
                {
                    Log.LogCodedWarning("XA0107", assembly.ItemSpec, 0, Properties.Resources.XA0107, assembly.ItemSpec);
                }

                sourcePath = CompressAssembly(assembly);

                // Add assembly
                var assemblyPath = GetAssemblyPath(assembly, frameworkAssembly: false);
                AddFileToArchiveIfNewer(apk, sourcePath, assemblyPath + Path.GetFileName(assembly.ItemSpec), compressionMethod: UncompressedMethod);

                // Try to add config if exists
                var config = Path.ChangeExtension(assembly.ItemSpec, "dll.config");
                AddAssemblyConfigEntry(apk, assemblyPath, config);

                // Try to add symbols if Debug
                if (debug)
                {
                    var symbols = Path.ChangeExtension(assembly.ItemSpec, "dll.mdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, assemblyPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }

                    symbols = Path.ChangeExtension(assembly.ItemSpec, "pdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, assemblyPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }
                }
                count++;
                if (count >= ZipArchiveEx.ZipFlushFilesLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }

            count = 0;
            // Add framework assemblies
            foreach (ITaskItem assembly in ResolvedFrameworkAssemblies)
            {
                if (bool.TryParse(assembly.GetMetadata("AndroidSkipAddToPackage"), out bool value) && value)
                {
                    Log.LogDebugMessage($"Skipping {assembly.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' ");
                    continue;
                }

                if (MonoAndroidHelper.IsReferenceAssembly(assembly.ItemSpec))
                {
                    Log.LogCodedWarning("XA0107", assembly.ItemSpec, 0, Properties.Resources.XA0107, assembly.ItemSpec);
                }

                sourcePath = CompressAssembly(assembly);
                var assemblyPath = GetAssemblyPath(assembly, frameworkAssembly: true);
                AddFileToArchiveIfNewer(apk, sourcePath, assemblyPath + Path.GetFileName(assembly.ItemSpec), compressionMethod: UncompressedMethod);
                var config = Path.ChangeExtension(assembly.ItemSpec, "dll.config");
                AddAssemblyConfigEntry(apk, assemblyPath, config);
                // Try to add symbols if Debug
                if (debug)
                {
                    var symbols = Path.ChangeExtension(assembly.ItemSpec, "dll.mdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, assemblyPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }

                    symbols = Path.ChangeExtension(assembly.ItemSpec, "pdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, assemblyPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }
                }
                count++;
                if (count >= ZipArchiveEx.ZipFlushFilesLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }

            void EnsureCompressedAssemblyData(string sourcePath, uint descriptorIndex)
            {
                if (compressedAssembly == null)
                {
                    compressedAssembly = new AssemblyCompression.AssemblyData(sourcePath, descriptorIndex);
                }
                else
                {
                    compressedAssembly.SetData(sourcePath, descriptorIndex);
                }
            }

            string CompressAssembly(ITaskItem assembly)
            {
                if (!compress)
                {
                    return(assembly.ItemSpec);
                }

                if (bool.TryParse(assembly.GetMetadata("AndroidSkipCompression"), out bool value) && value)
                {
                    Log.LogDebugMessage($"Skipping compression of {assembly.ItemSpec} due to 'AndroidSkipCompression' == 'true' ");
                    return(assembly.ItemSpec);
                }

                var key = CompressedAssemblyInfo.GetDictionaryKey(assembly);

                if (compressedAssembliesInfo.TryGetValue(key, out CompressedAssemblyInfo info) && info != null)
                {
                    EnsureCompressedAssemblyData(assembly.ItemSpec, info.DescriptorIndex);
                    string assemblyOutputDir;
                    string subDirectory = assembly.GetMetadata("DestinationSubDirectory");
                    if (!String.IsNullOrEmpty(subDirectory))
                    {
                        assemblyOutputDir = Path.Combine(compressedOutputDir, subDirectory);
                    }
                    else
                    {
                        assemblyOutputDir = compressedOutputDir;
                    }
                    AssemblyCompression.CompressionResult result = AssemblyCompression.Compress(compressedAssembly, assemblyOutputDir);
                    if (result != AssemblyCompression.CompressionResult.Success)
                    {
                        switch (result)
                        {
                        case AssemblyCompression.CompressionResult.EncodingFailed:
                            Log.LogMessage($"Failed to compress {assembly.ItemSpec}");
                            break;

                        case AssemblyCompression.CompressionResult.InputTooBig:
                            Log.LogMessage($"Input assembly {assembly.ItemSpec} exceeds maximum input size");
                            break;

                        default:
                            Log.LogMessage($"Unknown error compressing {assembly.ItemSpec}");
                            break;
                        }
                        return(assembly.ItemSpec);
                    }
                    return(compressedAssembly.DestinationPath);
                }
                else
                {
                    Log.LogDebugMessage($"Assembly missing from {nameof (CompressedAssemblyInfo)}: {key}");
                }

                return(assembly.ItemSpec);
            }
        }
예제 #6
0
        void ExecuteWithAbi(string [] supportedAbis, string apkInputPath, string apkOutputPath, bool debug, bool compress, IDictionary <string, CompressedAssemblyInfo> compressedAssembliesInfo)
        {
            if (InterpreterEnabled)
            {
                foreach (string abi in supportedAbis)
                {
                    if (String.Compare("x86", abi, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        Log.LogCodedError("XA0124", Properties.Resources.XA0124);
                        return;
                    }
                }
            }

            ArchiveFileList files   = new ArchiveFileList();
            bool            refresh = true;

            if (apkInputPath != null && File.Exists(apkInputPath) && !File.Exists(apkOutputPath))
            {
                Log.LogDebugMessage($"Copying {apkInputPath} to {apkInputPath}");
                File.Copy(apkInputPath, apkOutputPath, overwrite: true);
                refresh = false;
            }
            using (var apk = new ZipArchiveEx(apkOutputPath, File.Exists(apkOutputPath) ? FileMode.Open : FileMode.Create)) {
                if (refresh)
                {
                    for (long i = 0; i < apk.Archive.EntryCount; i++)
                    {
                        ZipEntry e = apk.Archive.ReadEntry((ulong)i);
                        Log.LogDebugMessage($"Registering item {e.FullName}");
                        existingEntries.Add(e.FullName);
                    }
                }
                if (apkInputPath != null && File.Exists(apkInputPath) && refresh)
                {
                    var lastWriteOutput = File.Exists(apkOutputPath) ? File.GetLastWriteTimeUtc(apkOutputPath) : DateTime.MinValue;
                    var lastWriteInput  = File.GetLastWriteTimeUtc(apkInputPath);
                    using (var packaged = new ZipArchiveEx(apkInputPath, FileMode.Open)) {
                        foreach (var entry in packaged.Archive)
                        {
                            // NOTE: aapt2 is creating zip entries on Windows such as `assets\subfolder/asset2.txt`
                            var entryName = entry.FullName;
                            if (entryName.Contains("\\"))
                            {
                                entryName = entryName.Replace('\\', '/');
                                Log.LogDebugMessage($"Fixing up malformed entry `{entry.FullName}` -> `{entryName}`");
                            }
                            Log.LogDebugMessage($"Deregistering item {entryName}");
                            existingEntries.Remove(entryName);
                            if (lastWriteInput <= lastWriteOutput)
                            {
                                continue;
                            }
                            if (apk.Archive.ContainsEntry(entryName))
                            {
                                ZipEntry e = apk.Archive.ReadEntry(entryName);
                                // check the CRC values as the ModifiedDate is always 01/01/1980 in the aapt generated file.
                                if (entry.CRC == e.CRC && entry.CompressedSize == e.CompressedSize)
                                {
                                    Log.LogDebugMessage($"Skipping {entryName} from {apkInputPath} as its up to date.");
                                    continue;
                                }
                            }
                            var ms = new MemoryStream();
                            entry.Extract(ms);
                            Log.LogDebugMessage($"Refreshing {entryName} from {apkInputPath}");
                            apk.Archive.AddStream(ms, entryName, compressionMethod: entry.CompressionMethod);
                        }
                    }
                }
                apk.FixupWindowsPathSeparators((a, b) => Log.LogDebugMessage($"Fixing up malformed entry `{a}` -> `{b}`"));

                // Add classes.dx
                foreach (var dex in DalvikClasses)
                {
                    string apkName = dex.GetMetadata("ApkName");
                    string dexPath = string.IsNullOrWhiteSpace(apkName) ? Path.GetFileName(dex.ItemSpec) : apkName;
                    AddFileToArchiveIfNewer(apk, dex.ItemSpec, DalvikPath + dexPath);
                }

                if (EmbedAssemblies && !BundleAssemblies)
                {
                    AddAssemblies(apk, debug, compress, compressedAssembliesInfo);
                }

                AddRuntimeLibraries(apk, supportedAbis);
                apk.Flush();
                AddNativeLibraries(files, supportedAbis);
                AddAdditionalNativeLibraries(files, supportedAbis);

                if (TypeMappings != null)
                {
                    foreach (ITaskItem typemap in TypeMappings)
                    {
                        AddFileToArchiveIfNewer(apk, typemap.ItemSpec, RootPath + Path.GetFileName(typemap.ItemSpec), compressionMethod: UncompressedMethod);
                    }
                }

                int count = 0;
                foreach (var file in files)
                {
                    var item = Path.Combine(file.archivePath.Replace(Path.DirectorySeparatorChar, '/'));
                    existingEntries.Remove(item);
                    CompressionMethod compressionMethod = GetCompressionMethod(file.filePath);
                    if (apk.SkipExistingFile(file.filePath, item, compressionMethod))
                    {
                        Log.LogDebugMessage($"Skipping {file.filePath} as the archive file is up to date.");
                        continue;
                    }
                    Log.LogDebugMessage("\tAdding {0}", file.filePath);
                    apk.Archive.AddFile(file.filePath, item, compressionMethod: compressionMethod);
                    count++;
                    if (count >= ZipArchiveEx.ZipFlushFilesLimit)
                    {
                        apk.Flush();
                        count = 0;
                    }
                }

                var jarFiles = (JavaSourceFiles != null) ? JavaSourceFiles.Where(f => f.ItemSpec.EndsWith(".jar")) : null;
                if (jarFiles != null && JavaLibraries != null)
                {
                    jarFiles = jarFiles.Concat(JavaLibraries);
                }
                else if (JavaLibraries != null)
                {
                    jarFiles = JavaLibraries;
                }

                var libraryProjectJars = MonoAndroidHelper.ExpandFiles(LibraryProjectJars)
                                         .Where(jar => !MonoAndroidHelper.IsEmbeddedReferenceJar(jar));

                var jarFilePaths = libraryProjectJars.Concat(jarFiles != null ? jarFiles.Select(j => j.ItemSpec) : Enumerable.Empty <string> ());
                jarFilePaths = MonoAndroidHelper.DistinctFilesByContent(jarFilePaths);

                count = 0;
                foreach (var jarFile in jarFilePaths)
                {
                    using (var stream = File.OpenRead(jarFile))
                        using (var jar = ZipArchive.Open(stream)) {
                            foreach (var jarItem in jar)
                            {
                                if (jarItem.IsDirectory)
                                {
                                    continue;
                                }
                                var name = jarItem.FullName;
                                if (!PackagingUtils.CheckEntryForPackaging(name))
                                {
                                    continue;
                                }
                                var path = RootPath + name;
                                existingEntries.Remove(path);
                                if (apk.SkipExistingEntry(jarItem, path))
                                {
                                    Log.LogDebugMessage($"Skipping {path} as the archive file is up to date.");
                                    continue;
                                }
                                if (apk.Archive.Any(e => e.FullName == path))
                                {
                                    Log.LogDebugMessage("Failed to add jar entry {0} from {1}: the same file already exists in the apk", name, Path.GetFileName(jarFile));
                                    continue;
                                }
                                byte [] data;
                                using (var d = new MemoryStream()) {
                                    jarItem.Extract(d);
                                    data = d.ToArray();
                                }
                                Log.LogDebugMessage($"Adding {path} as the archive file is out of date.");
                                apk.Archive.AddEntry(data, path);
                            }
                        }
                    count++;
                    if (count >= ZipArchiveEx.ZipFlushFilesLimit)
                    {
                        apk.Flush();
                        count = 0;
                    }
                }
                // Clean up Removed files.
                foreach (var entry in existingEntries)
                {
                    Log.LogDebugMessage($"Removing {entry} as it is not longer required.");
                    apk.Archive.DeleteEntry(entry);
                }
                apk.Flush();
                FixupArchive(apk);
            }
        }
예제 #7
0
        private void AddAssemblies(ZipArchiveEx apk)
        {
            bool debug = _Debug;
            bool use_shared_runtime = String.Equals(UseSharedRuntime, "true", StringComparison.OrdinalIgnoreCase);

            int count = 0;

            foreach (ITaskItem assembly in ResolvedUserAssemblies)
            {
                if (MonoAndroidHelper.IsReferenceAssembly(assembly.ItemSpec))
                {
                    Log.LogWarning($"{assembly.ItemSpec} is a reference assembly!");
                }
                // Add assembly
                apk.Archive.AddFile(assembly.ItemSpec, GetTargetDirectory(assembly.ItemSpec) + "/" + Path.GetFileName(assembly.ItemSpec), compressionMethod: CompressionMethod.Store);

                // Try to add config if exists
                var config = Path.ChangeExtension(assembly.ItemSpec, "dll.config");
                AddAssemblyConfigEntry(apk, config);

                // Try to add symbols if Debug
                if (debug)
                {
                    var symbols = Path.ChangeExtension(assembly.ItemSpec, "dll.mdb");

                    if (File.Exists(symbols))
                    {
                        apk.Archive.AddFile(symbols, "assemblies/" + Path.GetFileName(symbols), compressionMethod: CompressionMethod.Store);
                    }

                    symbols = Path.ChangeExtension(assembly.ItemSpec, "pdb");

                    if (File.Exists(symbols))
                    {
                        apk.Archive.AddFile(symbols, "assemblies/" + Path.GetFileName(symbols), compressionMethod: CompressionMethod.Store);
                    }
                }
                count++;
                if (count == ZipArchiveEx.ZipFlushLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }

            if (use_shared_runtime)
            {
                return;
            }

            count = 0;
            // Add framework assemblies
            foreach (ITaskItem assembly in ResolvedFrameworkAssemblies)
            {
                if (MonoAndroidHelper.IsReferenceAssembly(assembly.ItemSpec))
                {
                    Log.LogWarning($"{assembly.ItemSpec} is a reference assembly!");
                }
                apk.Archive.AddFile(assembly.ItemSpec, "assemblies/" + Path.GetFileName(assembly.ItemSpec), compressionMethod: CompressionMethod.Store);
                var config = Path.ChangeExtension(assembly.ItemSpec, "dll.config");
                AddAssemblyConfigEntry(apk, config);
                // Try to add symbols if Debug
                if (debug)
                {
                    var symbols = Path.ChangeExtension(assembly.ItemSpec, "dll.mdb");

                    if (File.Exists(symbols))
                    {
                        apk.Archive.AddFile(symbols, "assemblies/" + Path.GetFileName(symbols), compressionMethod: CompressionMethod.Store);
                    }

                    symbols = Path.ChangeExtension(assembly.ItemSpec, "pdb");

                    if (File.Exists(symbols))
                    {
                        apk.Archive.AddFile(symbols, "assemblies/" + Path.GetFileName(symbols), compressionMethod: CompressionMethod.Store);
                    }
                }
                count++;
                if (count == ZipArchiveEx.ZipFlushLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }
        }
예제 #8
0
        void ExecuteWithAbi(string supportedAbis, string apkInputPath, string apkOutputPath)
        {
            ArchiveFileList files = new ArchiveFileList();

            if (apkInputPath != null)
            {
                File.Copy(apkInputPath, apkOutputPath + "new", overwrite: true);
            }
            using (var apk = new ZipArchiveEx(apkOutputPath + "new", apkInputPath != null ? FileMode.Open : FileMode.Create)) {
                apk.Archive.AddEntry("NOTICE",
                                     Assembly.GetExecutingAssembly().GetManifestResourceStream("NOTICE.txt"));

                // Add classes.dx
                apk.Archive.AddFiles(DalvikClasses, useFileDirectories: false);

                if (EmbedAssemblies && !BundleAssemblies)
                {
                    AddAssemblies(apk);
                }

                AddEnvironment(apk);
                AddRuntimeLibraries(apk, supportedAbis);
                apk.Flush();
                AddNativeLibraries(files, supportedAbis);
                apk.Flush();
                AddAdditionalNativeLibraries(files, supportedAbis);
                apk.Flush();
                AddNativeLibrariesFromAssemblies(apk, supportedAbis);
                apk.Flush();

                foreach (ITaskItem typemap in TypeMappings)
                {
                    apk.Archive.AddFile(typemap.ItemSpec, Path.GetFileName(typemap.ItemSpec), compressionMethod: CompressionMethod.Store);
                }

                int count = 0;
                foreach (var file in files)
                {
                    var item = Path.Combine(file.Item2, Path.GetFileName(file.Item1))
                               .Replace(Path.DirectorySeparatorChar, '/');
                    if (apk.Archive.ContainsEntry(item))
                    {
                        Log.LogWarning(null, "XA4301", null, file.Item1, 0, 0, 0, 0, "Apk already contains the item {0}; ignoring.", item);
                        continue;
                    }
                    apk.Archive.AddFile(file.Item1, item);
                    count++;
                    if (count == ZipArchiveEx.ZipFlushLimit)
                    {
                        apk.Flush();
                        count = 0;
                    }
                }
                if (_Debug)
                {
                    AddGdbservers(apk, files, supportedAbis, debugServer);
                }

                var jarFiles = (JavaSourceFiles != null) ? JavaSourceFiles.Where(f => f.ItemSpec.EndsWith(".jar")) : null;
                if (jarFiles != null && JavaLibraries != null)
                {
                    jarFiles = jarFiles.Concat(JavaLibraries);
                }
                else if (JavaLibraries != null)
                {
                    jarFiles = JavaLibraries;
                }

                var libraryProjectJars = MonoAndroidHelper.ExpandFiles(LibraryProjectJars)
                                         .Where(jar => !MonoAndroidHelper.IsEmbeddedReferenceJar(jar));

                var jarFilePaths = libraryProjectJars.Concat(jarFiles != null ? jarFiles.Select(j => j.ItemSpec) : Enumerable.Empty <string> ());
                jarFilePaths = MonoAndroidHelper.DistinctFilesByContent(jarFilePaths);

                count = 0;
                foreach (var jarFile in jarFilePaths)
                {
                    using (var jar = ZipArchive.Open(File.OpenRead(jarFile))) {
                        foreach (var jarItem in jar.Where(ze => !ze.IsDirectory && !ze.FullName.StartsWith("META-INF") && !ze.FullName.EndsWith(".class") && !ze.FullName.EndsWith(".java") && !ze.FullName.EndsWith("MANIFEST.MF")))
                        {
                            byte [] data;
                            using (var d = new System.IO.MemoryStream()) {
                                jarItem.Extract(d);
                                data = d.ToArray();
                            }
                            if (apk.Archive.Any(e => e.FullName == jarItem.FullName))
                            {
                                Log.LogMessage("Warning: failed to add jar entry {0} from {1}: the same file already exists in the apk", jarItem.FullName, Path.GetFileName(jarFile));
                            }
                            else
                            {
                                apk.Archive.AddEntry(data, jarItem.FullName);
                            }
                        }
                    }
                    count++;
                    if (count == ZipArchiveEx.ZipFlushLimit)
                    {
                        apk.Flush();
                        count = 0;
                    }
                }
                if (StubApplicationDataFile != null && File.Exists(StubApplicationDataFile))
                {
                    apk.Archive.AddFile(StubApplicationDataFile, Path.GetFileName(StubApplicationDataFile));
                }
            }
            MonoAndroidHelper.CopyIfZipChanged(apkOutputPath + "new", apkOutputPath);
            File.Delete(apkOutputPath + "new");
        }
예제 #9
0
        private void AddAssemblies(ZipArchiveEx apk)
        {
            bool debug = _Debug;
            bool use_shared_runtime = String.Equals(UseSharedRuntime, "true", StringComparison.OrdinalIgnoreCase);

            int count = 0;

            foreach (ITaskItem assembly in ResolvedUserAssemblies)
            {
                if (bool.TryParse(assembly.GetMetadata("AndroidSkipAddToPackage"), out bool value) && value)
                {
                    Log.LogDebugMessage($"Skipping {assembly.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' ");
                    continue;
                }
                if (MonoAndroidHelper.IsReferenceAssembly(assembly.ItemSpec))
                {
                    Log.LogCodedWarning("XA0107", assembly.ItemSpec, 0, "{0} is a Reference Assembly!", assembly.ItemSpec);
                }
                // Add assembly
                AddFileToArchiveIfNewer(apk, assembly.ItemSpec, GetTargetDirectory(assembly.ItemSpec) + "/" + Path.GetFileName(assembly.ItemSpec), compressionMethod: UncompressedMethod);

                // Try to add config if exists
                var config = Path.ChangeExtension(assembly.ItemSpec, "dll.config");
                AddAssemblyConfigEntry(apk, config);

                // Try to add symbols if Debug
                if (debug)
                {
                    var symbols = Path.ChangeExtension(assembly.ItemSpec, "dll.mdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, AssembliesPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }

                    symbols = Path.ChangeExtension(assembly.ItemSpec, "pdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, AssembliesPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }
                }
                count++;
                if (count == ZipArchiveEx.ZipFlushLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }

            if (use_shared_runtime)
            {
                return;
            }

            count = 0;
            // Add framework assemblies
            foreach (ITaskItem assembly in ResolvedFrameworkAssemblies)
            {
                if (bool.TryParse(assembly.GetMetadata("AndroidSkipAddToPackage"), out bool value) && value)
                {
                    Log.LogDebugMessage($"Skipping {assembly.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' ");
                    continue;
                }
                if (MonoAndroidHelper.IsReferenceAssembly(assembly.ItemSpec))
                {
                    Log.LogCodedWarning("XA0107", assembly.ItemSpec, 0, "{0} is a Reference Assembly!", assembly.ItemSpec);
                }
                AddFileToArchiveIfNewer(apk, assembly.ItemSpec, AssembliesPath + Path.GetFileName(assembly.ItemSpec), compressionMethod: UncompressedMethod);
                var config = Path.ChangeExtension(assembly.ItemSpec, "dll.config");
                AddAssemblyConfigEntry(apk, config);
                // Try to add symbols if Debug
                if (debug)
                {
                    var symbols = Path.ChangeExtension(assembly.ItemSpec, "dll.mdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, AssembliesPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }

                    symbols = Path.ChangeExtension(assembly.ItemSpec, "pdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, AssembliesPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }
                }
                count++;
                if (count == ZipArchiveEx.ZipFlushLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }
        }
예제 #10
0
        void ExecuteWithAbi(string [] supportedAbis, string apkInputPath, string apkOutputPath)
        {
            var             temp  = apkOutputPath + "new";
            ArchiveFileList files = new ArchiveFileList();

            if (apkInputPath != null)
            {
                File.Copy(apkInputPath, temp, overwrite: true);
            }
            using (var notice = Assembly.GetExecutingAssembly().GetManifestResourceStream("NOTICE.txt"))
                using (var apk = new ZipArchiveEx(temp, apkInputPath != null ? FileMode.Open : FileMode.Create)) {
                    apk.FixupWindowsPathSeparators((a, b) => Log.LogDebugMessage($"Fixing up malformed entry `{a}` -> `{b}`"));
                    apk.Archive.AddEntry(RootPath + "NOTICE", notice);

                    // Add classes.dx
                    foreach (var dex in DalvikClasses)
                    {
                        string apkName = dex.GetMetadata("ApkName");
                        string dexPath = string.IsNullOrWhiteSpace(apkName) ? Path.GetFileName(dex.ItemSpec) : apkName;
                        apk.Archive.AddFile(dex.ItemSpec, DalvikPath + dexPath);
                    }

                    if (EmbedAssemblies && !BundleAssemblies)
                    {
                        AddAssemblies(apk);
                    }

                    AddRuntimeLibraries(apk, supportedAbis);
                    apk.Flush();
                    AddNativeLibraries(files, supportedAbis);
                    apk.Flush();
                    AddAdditionalNativeLibraries(files, supportedAbis);
                    apk.Flush();

                    if (TypeMappings != null)
                    {
                        foreach (ITaskItem typemap in TypeMappings)
                        {
                            apk.Archive.AddFile(typemap.ItemSpec, RootPath + Path.GetFileName(typemap.ItemSpec), compressionMethod: UncompressedMethod);
                        }
                    }

                    int count = 0;
                    foreach (var file in files)
                    {
                        var item = Path.Combine(file.Item2, Path.GetFileName(file.Item1))
                                   .Replace(Path.DirectorySeparatorChar, '/');
                        if (apk.Archive.ContainsEntry(item))
                        {
                            Log.LogCodedWarning("XA4301", file.Item1, 0, "Apk already contains the item {0}; ignoring.", item);
                            continue;
                        }
                        apk.Archive.AddFile(file.Item1, item, compressionMethod: GetCompressionMethod(file.Item1));
                        count++;
                        if (count == ZipArchiveEx.ZipFlushLimit)
                        {
                            apk.Flush();
                            count = 0;
                        }
                    }

                    var jarFiles = (JavaSourceFiles != null) ? JavaSourceFiles.Where(f => f.ItemSpec.EndsWith(".jar")) : null;
                    if (jarFiles != null && JavaLibraries != null)
                    {
                        jarFiles = jarFiles.Concat(JavaLibraries);
                    }
                    else if (JavaLibraries != null)
                    {
                        jarFiles = JavaLibraries;
                    }

                    var libraryProjectJars = MonoAndroidHelper.ExpandFiles(LibraryProjectJars)
                                             .Where(jar => !MonoAndroidHelper.IsEmbeddedReferenceJar(jar));

                    var jarFilePaths = libraryProjectJars.Concat(jarFiles != null ? jarFiles.Select(j => j.ItemSpec) : Enumerable.Empty <string> ());
                    jarFilePaths = MonoAndroidHelper.DistinctFilesByContent(jarFilePaths);

                    count = 0;
                    foreach (var jarFile in jarFilePaths)
                    {
                        using (var jar = ZipArchive.Open(File.OpenRead(jarFile))) {
                            foreach (var jarItem in jar.Where(ze => !ze.IsDirectory && !ze.FullName.StartsWith("META-INF") && !ze.FullName.EndsWith(".class") && !ze.FullName.EndsWith(".java") && !ze.FullName.EndsWith("MANIFEST.MF")))
                            {
                                byte [] data;
                                using (var d = new System.IO.MemoryStream()) {
                                    jarItem.Extract(d);
                                    data = d.ToArray();
                                }
                                var path = RootPath + jarItem.FullName;
                                if (apk.Archive.Any(e => e.FullName == path))
                                {
                                    Log.LogMessage("Warning: failed to add jar entry {0} from {1}: the same file already exists in the apk", jarItem.FullName, Path.GetFileName(jarFile));
                                }
                                else
                                {
                                    apk.Archive.AddEntry(data, path);
                                }
                            }
                        }
                        count++;
                        if (count == ZipArchiveEx.ZipFlushLimit)
                        {
                            apk.Flush();
                            count = 0;
                        }
                    }
                    FixupArchive(apk);
                }
            if (MonoAndroidHelper.CopyIfZipChanged(temp, apkOutputPath))
            {
                Log.LogDebugMessage($"Copied {temp} to {apkOutputPath}");
            }
            else
            {
                Log.LogDebugMessage($"Skipped {apkOutputPath}: up to date");
            }
            File.Delete(temp);
        }
예제 #11
0
        void AddAssemblies(ZipArchiveEx apk, bool debug, bool compress, IDictionary <string, CompressedAssemblyInfo> compressedAssembliesInfo)
        {
            bool   use_shared_runtime = String.Equals(UseSharedRuntime, "true", StringComparison.OrdinalIgnoreCase);
            string sourcePath;

            AssemblyCompression.AssemblyData compressedAssembly = null;

            int count = 0;

            foreach (ITaskItem assembly in ResolvedUserAssemblies)
            {
                if (bool.TryParse(assembly.GetMetadata("AndroidSkipAddToPackage"), out bool value) && value)
                {
                    Log.LogDebugMessage($"Skipping {assembly.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' ");
                    continue;
                }
                if (MonoAndroidHelper.IsReferenceAssembly(assembly.ItemSpec))
                {
                    Log.LogCodedWarning("XA0107", assembly.ItemSpec, 0, Properties.Resources.XA0107, assembly.ItemSpec);
                }

                sourcePath = CompressAssembly(assembly);

                // Add assembly
                AddFileToArchiveIfNewer(apk, sourcePath, GetTargetDirectory(assembly.ItemSpec) + "/" + Path.GetFileName(assembly.ItemSpec), compressionMethod: UncompressedMethod);

                // Try to add config if exists
                var config = Path.ChangeExtension(assembly.ItemSpec, "dll.config");
                AddAssemblyConfigEntry(apk, config);

                // Try to add symbols if Debug
                if (debug)
                {
                    var symbols = Path.ChangeExtension(assembly.ItemSpec, "dll.mdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, AssembliesPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }

                    symbols = Path.ChangeExtension(assembly.ItemSpec, "pdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, AssembliesPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }
                }
                count++;
                if (count == ZipArchiveEx.ZipFlushLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }

            if (use_shared_runtime)
            {
                return;
            }

            count = 0;
            // Add framework assemblies
            foreach (ITaskItem assembly in ResolvedFrameworkAssemblies)
            {
                if (bool.TryParse(assembly.GetMetadata("AndroidSkipAddToPackage"), out bool value) && value)
                {
                    Log.LogDebugMessage($"Skipping {assembly.ItemSpec} due to 'AndroidSkipAddToPackage' == 'true' ");
                    continue;
                }

                if (MonoAndroidHelper.IsReferenceAssembly(assembly.ItemSpec))
                {
                    Log.LogCodedWarning("XA0107", assembly.ItemSpec, 0, Properties.Resources.XA0107, assembly.ItemSpec);
                }

                sourcePath = CompressAssembly(assembly);
                AddFileToArchiveIfNewer(apk, sourcePath, AssembliesPath + Path.GetFileName(assembly.ItemSpec), compressionMethod: UncompressedMethod);
                var config = Path.ChangeExtension(assembly.ItemSpec, "dll.config");
                AddAssemblyConfigEntry(apk, config);
                // Try to add symbols if Debug
                if (debug)
                {
                    var symbols = Path.ChangeExtension(assembly.ItemSpec, "dll.mdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, AssembliesPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }

                    symbols = Path.ChangeExtension(assembly.ItemSpec, "pdb");

                    if (File.Exists(symbols))
                    {
                        AddFileToArchiveIfNewer(apk, symbols, AssembliesPath + Path.GetFileName(symbols), compressionMethod: UncompressedMethod);
                    }
                }
                count++;
                if (count == ZipArchiveEx.ZipFlushLimit)
                {
                    apk.Flush();
                    count = 0;
                }
            }

            void EnsureCompressedAssemblyData(string sourcePath, uint descriptorIndex)
            {
                if (compressedAssembly == null)
                {
                    compressedAssembly = new AssemblyCompression.AssemblyData(sourcePath, descriptorIndex);
                }
                else
                {
                    compressedAssembly.SetData(sourcePath, descriptorIndex);
                }
            }

            string CompressAssembly(ITaskItem assembly)
            {
                if (!compress)
                {
                    return(assembly.ItemSpec);
                }

                if (bool.TryParse(assembly.GetMetadata("AndroidSkipCompression"), out bool value) && value)
                {
                    Log.LogDebugMessage($"Skipping compression of {assembly.ItemSpec} due to 'AndroidSkipCompression' == 'true' ");
                    return(assembly.ItemSpec);
                }

                if (compressedAssembliesInfo.TryGetValue(Path.GetFileName(assembly.ItemSpec), out CompressedAssemblyInfo info) && info != null)
                {
                    EnsureCompressedAssemblyData(assembly.ItemSpec, info.DescriptorIndex);
                    AssemblyCompression.CompressionResult result = AssemblyCompression.Compress(compressedAssembly);
                    if (result != AssemblyCompression.CompressionResult.Success)
                    {
                        switch (result)
                        {
                        case AssemblyCompression.CompressionResult.EncodingFailed:
                            Log.LogMessage($"Failed to compress {assembly.ItemSpec}");
                            break;

                        case AssemblyCompression.CompressionResult.InputTooBig:
                            Log.LogMessage($"Input assembly {assembly.ItemSpec} exceeds maximum input size");
                            break;

                        default:
                            Log.LogMessage($"Unknown error compressing {assembly.ItemSpec}");
                            break;
                        }
                        return(assembly.ItemSpec);
                    }
                    return(compressedAssembly.DestinationPath);
                }

                return(assembly.ItemSpec);
            }
        }