|
| 1 | +using Microsoft.Build.Framework; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | +using Xamarin.Android.Tools; |
| 7 | +using Xamarin.Build; |
| 8 | +using ThreadingTasks = System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Xamarin.Android.Tasks |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// We used to invoke aapt/aapt2 per library (many times!), this task does the work to generate R.java for libraries without calling aapt/aapt2. |
| 14 | + /// </summary> |
| 15 | + public class GenerateLibraryResources : AsyncTask |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// The main R.txt for the app |
| 19 | + /// </summary> |
| 20 | + [Required] |
| 21 | + public string ResourceSymbolsTextFile { get; set; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The output directory for Java source code, such as: $(IntermediateOutputPath)android\src |
| 25 | + /// </summary> |
| 26 | + [Required] |
| 27 | + public string OutputDirectory { get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The list of R.txt files for each library |
| 31 | + /// </summary> |
| 32 | + public string [] LibraryTextFiles { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The accompanying manifest file for each library |
| 36 | + /// </summary> |
| 37 | + public string [] ManifestFiles { get; set; } |
| 38 | + |
| 39 | + public override bool Execute () |
| 40 | + { |
| 41 | + Yield (); |
| 42 | + try { |
| 43 | + var task = ThreadingTasks.Task.Run (() => { |
| 44 | + DoExecute (); |
| 45 | + }, CancellationToken); |
| 46 | + |
| 47 | + task.ContinueWith (Complete); |
| 48 | + |
| 49 | + base.Execute (); |
| 50 | + } finally { |
| 51 | + Reacquire (); |
| 52 | + } |
| 53 | + |
| 54 | + return !Log.HasLoggedErrors; |
| 55 | + } |
| 56 | + |
| 57 | + string output_directory; |
| 58 | + Dictionary<Tuple<string, string>, string> main_r_txt; |
| 59 | + |
| 60 | + void DoExecute () |
| 61 | + { |
| 62 | + if (LibraryTextFiles == null || LibraryTextFiles.Length == 0) |
| 63 | + return; |
| 64 | + |
| 65 | + main_r_txt = new Dictionary<Tuple<string, string>, string> (); |
| 66 | + using (var reader = File.OpenText (ResourceSymbolsTextFile)) { |
| 67 | + foreach (var line in ParseFile (reader)) { |
| 68 | + var key = new Tuple<string, string> (line [Index.Class], line [Index.Name]); |
| 69 | + main_r_txt [key] = line [Index.Value]; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Directory.CreateDirectory (OutputDirectory); |
| 74 | + //So we don't need to consider WorkingDirectory on background threads |
| 75 | + output_directory = Path.GetFullPath (OutputDirectory); |
| 76 | + |
| 77 | + ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions { |
| 78 | + CancellationToken = CancellationToken, |
| 79 | + TaskScheduler = ThreadingTasks.TaskScheduler.Default, |
| 80 | + }; |
| 81 | + |
| 82 | + ThreadingTasks.Parallel.For (0, LibraryTextFiles.Length, options, ProcessDirectory); |
| 83 | + } |
| 84 | + |
| 85 | + void ProcessDirectory (int index) |
| 86 | + { |
| 87 | + var r_txt = LibraryTextFiles [index]; |
| 88 | + if (!File.Exists (r_txt)) { |
| 89 | + LogDebugMessage ($"Skipping, R.txt does not exist: {r_txt}"); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + var manifestFile = ManifestFiles [index]; |
| 94 | + if (!File.Exists (manifestFile)) { |
| 95 | + LogDebugMessage ($"Skipping, AndroidManifest.xml does not exist: {manifestFile}"); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + var manifest = AndroidAppManifest.Load (manifestFile, MonoAndroidHelper.SupportedVersions); |
| 100 | + |
| 101 | + using (var memory = new MemoryStream ()) |
| 102 | + using (var writer = new StreamWriter (memory, Encoding)) { |
| 103 | + // This code is based on the Android gradle plugin |
| 104 | + // https://android.googlesource.com/platform/tools/base/+/908b391a9c006af569dfaff08b37f8fdd6c4da89/build-system/builder/src/main/java/com/android/builder/internal/SymbolWriter.java |
| 105 | + |
| 106 | + writer.WriteLine ("/* AUTO-GENERATED FILE. DO NOT MODIFY."); |
| 107 | + writer.WriteLine (" *"); |
| 108 | + writer.WriteLine (" * This class was automatically generated by"); |
| 109 | + writer.WriteLine (" * Xamarin.Android from the resource data it found."); |
| 110 | + writer.WriteLine (" * It should not be modified by hand."); |
| 111 | + writer.WriteLine (" */"); |
| 112 | + |
| 113 | + writer.Write ("package "); |
| 114 | + writer.Write (manifest.PackageName); |
| 115 | + writer.WriteLine (';'); |
| 116 | + writer.WriteLine (); |
| 117 | + writer.WriteLine ("public final class R {"); |
| 118 | + |
| 119 | + using (var reader = File.OpenText (r_txt)) { |
| 120 | + string currentClass = null; |
| 121 | + foreach (var line in ParseFile (reader)) { |
| 122 | + var type = line [Index.Type]; |
| 123 | + var clazz = line [Index.Class]; |
| 124 | + var name = line [Index.Name]; |
| 125 | + var key = new Tuple<string, string> (clazz, name); |
| 126 | + if (main_r_txt.TryGetValue (key, out string value)) { |
| 127 | + if (clazz != currentClass) { |
| 128 | + // If not the first inner class |
| 129 | + if (currentClass != null) { |
| 130 | + writer.WriteLine ("\t}"); |
| 131 | + } |
| 132 | + |
| 133 | + currentClass = clazz; |
| 134 | + writer.Write ("\tpublic static final class "); |
| 135 | + writer.Write (currentClass); |
| 136 | + writer.WriteLine (" {"); |
| 137 | + } |
| 138 | + |
| 139 | + writer.Write ("\t\tpublic static final "); |
| 140 | + writer.Write (type); |
| 141 | + writer.Write (' '); |
| 142 | + writer.Write (name); |
| 143 | + writer.Write (" = "); |
| 144 | + // It may be an int[] |
| 145 | + if (value.StartsWith ("{", StringComparison.Ordinal)) { |
| 146 | + writer.Write ("new "); |
| 147 | + writer.Write (type); |
| 148 | + writer.Write (' '); |
| 149 | + } |
| 150 | + writer.Write (value); |
| 151 | + writer.WriteLine (';'); |
| 152 | + } else { |
| 153 | + LogDebugMessage ($"{r_txt}: `{type} {clazz} {name}` value not found"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // If we wrote at least one inner class |
| 158 | + if (currentClass != null) { |
| 159 | + writer.WriteLine ("\t}"); |
| 160 | + } |
| 161 | + writer.WriteLine ('}'); |
| 162 | + } |
| 163 | + |
| 164 | + writer.Flush (); |
| 165 | + var r_java = Path.Combine (output_directory, manifest.PackageName.Replace ('.', Path.DirectorySeparatorChar), "R.java"); |
| 166 | + if (MonoAndroidHelper.CopyIfStreamChanged (memory, r_java)) { |
| 167 | + LogDebugMessage ($"Writing: {r_java}"); |
| 168 | + } else { |
| 169 | + LogDebugMessage ($"Up to date: {r_java}"); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + static readonly Encoding Encoding = new UTF8Encoding (encoderShouldEmitUTF8Identifier: false); |
| 175 | + static readonly char [] Delimiter = new [] { ' ' }; |
| 176 | + |
| 177 | + class Index |
| 178 | + { |
| 179 | + public const int Type = 0; |
| 180 | + public const int Class = 1; |
| 181 | + public const int Name = 2; |
| 182 | + public const int Value = 3; |
| 183 | + } |
| 184 | + |
| 185 | + /// <summary> |
| 186 | + /// R.txt is of the format: |
| 187 | + /// int id icon 0x7f0c000a |
| 188 | + /// int[] styleable ViewStubCompat { 0x010100d0, 0x010100f2, 0x010100f3 } |
| 189 | + /// This returns a 4-length string[] of the parts. |
| 190 | + /// </summary> |
| 191 | + IEnumerable<string []> ParseFile (StreamReader reader) |
| 192 | + { |
| 193 | + while (!reader.EndOfStream) { |
| 194 | + var line = reader.ReadLine (); |
| 195 | + var items = line.Split (Delimiter, 4); |
| 196 | + yield return items; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments