bool ExecuteForAbi(string cmd, string currentResourceOutputFile)
        {
            var output  = new List <OutputLine> ();
            var ret     = RunAapt(cmd, output);
            var success = !string.IsNullOrEmpty(currentResourceOutputFile)
                                ? File.Exists(Path.Combine(currentResourceOutputFile + ".bk"))
                                : ret;

            foreach (var line in output)
            {
                if (!LogAapt2EventsFromOutput(line.Line, MessageImportance.Normal, success))
                {
                    break;
                }
            }
            if (ret && !string.IsNullOrEmpty(currentResourceOutputFile))
            {
                var tmpfile = currentResourceOutputFile + ".bk";
                // aapt2 might not produce an archive and we must provide
                // and -o foo even if we don't want one.
                if (File.Exists(tmpfile))
                {
                    MonoAndroidHelper.CopyIfZipChanged(tmpfile, currentResourceOutputFile);
                    File.Delete(tmpfile);
                }
            }
            return(ret);
        }
Beispiel #2
0
        bool ExecuteForAbi(string cmd, string currentResourceOutputFile)
        {
            var output  = new List <OutputLine> ();
            var ret     = RunAapt(cmd, output);
            var success = !string.IsNullOrEmpty(currentResourceOutputFile)
                                ? File.Exists(Path.Combine(currentResourceOutputFile + ".bk"))
                                : ret;

            foreach (var line in output)
            {
                if (line.StdError)
                {
                    LogEventsFromTextOutput(line.Line, MessageImportance.Normal, success);
                }
                else
                {
                    LogMessage(line.Line, MessageImportance.Normal);
                }
            }
            if (ret && !string.IsNullOrEmpty(currentResourceOutputFile))
            {
                var tmpfile = currentResourceOutputFile + ".bk";
                MonoAndroidHelper.CopyIfZipChanged(tmpfile, currentResourceOutputFile);
                File.Delete(tmpfile);
            }
            return(ret);
        }
        public async override System.Threading.Tasks.Task RunTaskAsync()
        {
            try {
                LoadResourceCaseMap();

                assemblyMap.Load(Path.Combine(WorkingDirectory, AssemblyIdentityMapFile));

                proguardRuleOutputTemp = GetTempFile();

                await this.WhenAll(ManifestFiles, ProcessManifest);

                ProcessOutput();
                // now check for
                foreach (var kvp in apks)
                {
                    string currentResourceOutputFile = kvp.Key;
                    bool   aaptResult = Daemon.JobSucceded(kvp.Value);
                    LogDebugMessage($"Processing {currentResourceOutputFile} JobId: {kvp.Value} Exists: {File.Exists (currentResourceOutputFile)} JobWorked: {aaptResult}");
                    if (!string.IsNullOrEmpty(currentResourceOutputFile))
                    {
                        var tmpfile = currentResourceOutputFile + ".bk";
                        // aapt2 might not produce an archive and we must provide
                        // and -o foo even if we don't want one.
                        if (File.Exists(tmpfile))
                        {
                            if (aaptResult)
                            {
                                LogDebugMessage($"Copying {tmpfile} to {currentResourceOutputFile}");
                                MonoAndroidHelper.CopyIfZipChanged(tmpfile, currentResourceOutputFile);
                            }
                            File.Delete(tmpfile);
                        }
                        // Delete the archive on failure
                        if (!aaptResult && File.Exists(currentResourceOutputFile))
                        {
                            LogDebugMessage($"Link did not succeed. Deleting {currentResourceOutputFile}");
                            File.Delete(currentResourceOutputFile);
                        }
                    }
                }
                if (!string.IsNullOrEmpty(ProguardRuleOutput))
                {
                    MonoAndroidHelper.CopyIfChanged(proguardRuleOutputTemp, ProguardRuleOutput);
                }
            } finally {
                lock (tempFiles) {
                    foreach (var temp in tempFiles)
                    {
                        File.Delete(temp);
                    }
                    tempFiles.Clear();
                }
            }
        }
Beispiel #4
0
        bool ExecuteForAbi(string cmd, string currentResourceOutputFile)
        {
            var ret = RunAapt(cmd);

            if (ret && !string.IsNullOrEmpty(currentResourceOutputFile))
            {
                var tmpfile = currentResourceOutputFile + ".bk";
                MonoAndroidHelper.CopyIfZipChanged(tmpfile, currentResourceOutputFile);
                File.Delete(tmpfile);
            }
            return(ret);
        }
Beispiel #5
0
        bool ExecuteForAbi(string abi)
        {
            currentAbi = abi;
            var ret = base.Execute();

            if (ret && !string.IsNullOrEmpty(currentResourceOutputFile))
            {
                var tmpfile = currentResourceOutputFile + ".bk";
                MonoAndroidHelper.CopyIfZipChanged(tmpfile, currentResourceOutputFile);
                File.Delete(tmpfile);
            }
            return(ret);
        }
Beispiel #6
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 = ZipFile.Open(apkOutputPath + "new", apkInputPath != null ? ZipArchiveMode.Update : ZipArchiveMode.Create)) {
                apk.AddEntry("NOTICE",
                             Assembly.GetExecutingAssembly().GetManifestResourceStream("NOTICE.txt"));

                // Add classes.dx
                apk.AddFiles(DalvikClasses, string.Empty);

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

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

                foreach (ITaskItem typemap in TypeMappings)
                {
                    apk.AddFile(typemap.ItemSpec, directoryPathInZip: "", compressionLevel: CompressionLevel.NoCompression);
                }

                foreach (var file in files)
                {
                    var item = Path.Combine(file.Item2, Path.GetFileName(file.Item1))
                               .Replace(Path.DirectorySeparatorChar, '/');
                    if (apk.ContainsEntry(item))
                    {
                        Log.LogWarning(null, "XA4301", null, file.Item1, 0, 0, 0, 0, "Apk already contains the item {0}; ignoring.", item);
                        continue;
                    }
                    apk.AddFile(file.Item1, file.Item2);
                }
                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);

                foreach (var jarFile in jarFilePaths)
                {
                    using (var jar = new ZipArchive(File.Open(jarFile, FileMode.Open), ZipArchiveMode.Read)) {
                        foreach (var jarItem in jar.Entries.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())
                                using (var i = jarItem.Open()) {
                                    i.CopyTo(d);
                                    data = d.ToArray();
                                }
                            if (apk.Entries.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.AddEntry(jarItem.FullName, data);
                            }
                        }
                    }
                }
                if (StubApplicationDataFile != null && File.Exists(StubApplicationDataFile))
                {
                    AddZipEntry(apk, StubApplicationDataFile, string.Empty);
                }
            }
            MonoAndroidHelper.CopyIfZipChanged(apkOutputPath + "new", apkOutputPath);
            File.Delete(apkOutputPath + "new");
        }
Beispiel #7
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();

                if (TypeMappings != null)
                {
                    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.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();
                            }
                            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;
                    }
                }
            }
            MonoAndroidHelper.CopyIfZipChanged(apkOutputPath + "new", apkOutputPath);
            File.Delete(apkOutputPath + "new");
        }
Beispiel #8
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);
        }