Skip to content

Commit 5a09742

Browse files
committed
[XABT] Break BuildApk into individual tasks for each content type.
1 parent 894ecea commit 5a09742

File tree

6 files changed

+290
-154
lines changed

6 files changed

+290
-154
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,15 @@ public class BuildApk : AndroidTask
6060

6161
public ITaskItem[] BundleNativeLibraries { get; set; }
6262

63-
public ITaskItem[] TypeMappings { get; set; }
64-
65-
[Required]
66-
public ITaskItem [] DalvikClasses { get; set; }
67-
6863
[Required]
6964
public string [] SupportedAbis { get; set; }
7065

7166
public bool EmbedAssemblies { get; set; }
7267

7368
public bool BundleAssemblies { get; set; }
7469

75-
public ITaskItem[] JavaSourceFiles { get; set; }
76-
77-
public ITaskItem[] JavaLibraries { get; set; }
78-
7970
public string[] DoNotPackageJavaLibraries { get; set; }
8071

81-
public string [] ExcludeFiles { get; set; }
82-
83-
public string [] IncludeFiles { get; set; }
84-
8572
public string Debug { get; set; }
8673

8774
public string AndroidSequencePointsMode { get; set; }
@@ -101,10 +88,6 @@ public class BuildApk : AndroidTask
10188

10289
public bool UseAssemblyStore { get; set; }
10390

104-
public string ZipFlushFilesLimit { get; set; }
105-
106-
public string ZipFlushSizeLimit { get; set; }
107-
10891
public int ZipAlignmentPages { get; set; } = AndroidZipAlign.DefaultZipAlignment64Bit;
10992

11093
[Required]
@@ -134,39 +117,20 @@ bool _Debug {
134117

135118
SequencePointsMode sequencePointsMode = SequencePointsMode.None;
136119

137-
public ITaskItem[] LibraryProjectJars { get; set; }
138120
HashSet<string> uncompressedFileExtensions;
139121

140-
// Do not use trailing / in the path
141-
public string RootPath { get; set; } = "";
142-
143-
public string DalvikPath { get; set; } = "";
144-
145122
protected virtual CompressionMethod UncompressedMethod => CompressionMethod.Store;
146123

147124
protected virtual void FixupArchive (ZipArchiveFileListBuilder zip) { }
148125

149126
List<string> existingEntries = new List<string> ();
150127

151-
List<Regex> excludePatterns = new List<Regex> ();
152-
153-
List<Regex> includePatterns = new List<Regex> ();
154-
155128
void ExecuteWithAbi (DSOWrapperGenerator.Config dsoWrapperConfig, string [] supportedAbis, string apkInputPath, string apkOutputPath, bool debug, bool compress, IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>> compressedAssembliesInfo, string assemblyStoreApkName)
156129
{
157130
ArchiveFileList files = new ArchiveFileList ();
158131

159132
using (var apk = new ZipArchiveFileListBuilder (apkOutputPath, File.Exists (apkOutputPath) ? FileMode.Open : FileMode.Create)) {
160133

161-
// Add classes.dx
162-
CompressionMethod dexCompressionMethod = GetCompressionMethod (".dex");
163-
foreach (var dex in DalvikClasses) {
164-
string apkName = dex.GetMetadata ("ApkName");
165-
string dexPath = string.IsNullOrWhiteSpace (apkName) ? Path.GetFileName (dex.ItemSpec) : apkName;
166-
AddFileToArchiveIfNewer (apk, dex.ItemSpec, DalvikPath + dexPath, compressionMethod: dexCompressionMethod);
167-
apk.Flush ();
168-
}
169-
170134
if (EmbedAssemblies) {
171135
AddAssemblies (dsoWrapperConfig, apk, debug, compress, compressedAssembliesInfo, assemblyStoreApkName);
172136
apk.Flush ();
@@ -178,12 +142,6 @@ void ExecuteWithAbi (DSOWrapperGenerator.Config dsoWrapperConfig, string [] supp
178142
AddNativeLibraries (files, supportedAbis);
179143
AddAdditionalNativeLibraries (files, supportedAbis);
180144

181-
if (TypeMappings != null) {
182-
foreach (ITaskItem typemap in TypeMappings) {
183-
AddFileToArchiveIfNewer (apk, typemap.ItemSpec, RootPath + Path.GetFileName(typemap.ItemSpec), compressionMethod: UncompressedMethod);
184-
}
185-
}
186-
187145
foreach (var file in files) {
188146
var item = Path.Combine (file.archivePath.Replace (Path.DirectorySeparatorChar, '/'));
189147
existingEntries.Remove (item);
@@ -196,63 +154,6 @@ void ExecuteWithAbi (DSOWrapperGenerator.Config dsoWrapperConfig, string [] supp
196154
apk.AddFileAndFlush (file.filePath, item, compressionMethod: compressionMethod);
197155
}
198156

199-
var jarFiles = (JavaSourceFiles != null) ? JavaSourceFiles.Where (f => f.ItemSpec.EndsWith (".jar", StringComparison.OrdinalIgnoreCase)) : null;
200-
if (jarFiles != null && JavaLibraries != null)
201-
jarFiles = jarFiles.Concat (JavaLibraries);
202-
else if (JavaLibraries != null)
203-
jarFiles = JavaLibraries;
204-
205-
var libraryProjectJars = MonoAndroidHelper.ExpandFiles (LibraryProjectJars)
206-
.Where (jar => !MonoAndroidHelper.IsEmbeddedReferenceJar (jar));
207-
208-
var jarFilePaths = libraryProjectJars.Concat (jarFiles != null ? jarFiles.Select (j => j.ItemSpec) : Enumerable.Empty<string> ());
209-
jarFilePaths = MonoAndroidHelper.DistinctFilesByContent (jarFilePaths);
210-
211-
foreach (var jarFile in jarFilePaths) {
212-
using (var stream = File.OpenRead (jarFile))
213-
using (var jar = ZipArchive.Open (stream)) {
214-
foreach (var jarItem in jar) {
215-
if (jarItem.IsDirectory)
216-
continue;
217-
var name = jarItem.FullName;
218-
if (!PackagingUtils.CheckEntryForPackaging (name)) {
219-
continue;
220-
}
221-
var path = RootPath + name;
222-
existingEntries.Remove (path);
223-
if (apk.SkipExistingEntry (jarItem, path)) {
224-
Log.LogDebugMessage ($"Skipping {path} as the archive file is up to date.");
225-
continue;
226-
}
227-
// check for ignored items
228-
bool exclude = false;
229-
bool forceInclude = false;
230-
foreach (var include in includePatterns) {
231-
if (include.IsMatch (path)) {
232-
forceInclude = true;
233-
break;
234-
}
235-
}
236-
if (!forceInclude) {
237-
foreach (var pattern in excludePatterns) {
238-
if (pattern.IsMatch (path)) {
239-
Log.LogDebugMessage ($"Ignoring jar entry '{name}' from '{Path.GetFileName (jarFile)}'. Filename matched the exclude pattern '{pattern}'.");
240-
exclude = true;
241-
break;
242-
}
243-
}
244-
}
245-
if (exclude)
246-
continue;
247-
if (string.Compare (Path.GetFileName (name), "AndroidManifest.xml", StringComparison.OrdinalIgnoreCase) == 0) {
248-
Log.LogDebugMessage ("Ignoring jar entry {0} from {1}: the same file already exists in the apk", name, Path.GetFileName (jarFile));
249-
continue;
250-
}
251-
252-
apk.AddJavaEntryAndFlush (jarFile, jarItem.FullName, path);
253-
}
254-
}
255-
}
256157
FixupArchive (apk);
257158

258159
OutputApkFiles = apk.ApkFiles.ToArray ();
@@ -280,13 +181,6 @@ public override bool RunTask ()
280181

281182
existingEntries.Clear ();
282183

283-
foreach (var pattern in ExcludeFiles ?? Array.Empty<string> ()) {
284-
excludePatterns.Add (FileGlobToRegEx (pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled));
285-
}
286-
foreach (var pattern in IncludeFiles ?? Array.Empty<string> ()) {
287-
includePatterns.Add (FileGlobToRegEx (pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled));
288-
}
289-
290184
bool debug = _Debug;
291185
bool compress = !debug && EnableCompression;
292186
IDictionary<AndroidTargetArch, Dictionary<string, CompressedAssemblyInfo>> compressedAssembliesInfo = null;
@@ -311,24 +205,6 @@ public override bool RunTask ()
311205
return !Log.HasLoggedErrors;
312206
}
313207

314-
static Regex FileGlobToRegEx (string fileGlob, RegexOptions options)
315-
{
316-
StringBuilder sb = new StringBuilder ();
317-
foreach (char c in fileGlob) {
318-
switch (c) {
319-
case '*': sb.Append (".*");
320-
break;
321-
case '?': sb.Append (".");
322-
break;
323-
case '.': sb.Append (@"\.");
324-
break;
325-
default: sb.Append (c);
326-
break;
327-
}
328-
}
329-
return new Regex (sb.ToString (), options);
330-
}
331-
332208
void AddRuntimeConfigBlob (DSOWrapperGenerator.Config dsoWrapperConfig, ZipArchiveFileListBuilder apk)
333209
{
334210
// We will place rc.bin in the `lib` directory next to the blob, to make startup slightly faster, as we will find the config file right after we encounter

src/Xamarin.Android.Build.Tasks/Tasks/BuildArchive.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ namespace Xamarin.Android.Tasks;
1212

1313
/// <summary>
1414
/// Takes a list of files and adds them to an APK archive. If the APK archive already
15-
/// exists, files are only added if they were changed.
15+
/// exists, files are only added if they were changed. Note *ALL* files to be in the final
16+
/// APK must be passed in via @(FilesToAddToArchive). This task will determine any unchanged files
17+
/// and skip them, as well as remove any existing files in the APK that are no longer required.
1618
/// </summary>
1719
public class BuildArchive : AndroidTask
1820
{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#nullable enable
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using Microsoft.Android.Build.Tasks;
7+
using Microsoft.Build.Framework;
8+
using Microsoft.Build.Utilities;
9+
10+
namespace Xamarin.Android.Tasks;
11+
12+
/// <summary>
13+
/// Collects Dalvik classes to be added to the final archive.
14+
/// </summary>
15+
public class CollectDalvikFilesForArchive : AndroidTask
16+
{
17+
public override string TaskPrefix => "CDF";
18+
19+
public string AndroidPackageFormat { get; set; } = "";
20+
21+
[Required]
22+
public ITaskItem [] DalvikClasses { get; set; } = [];
23+
24+
[Output]
25+
public ITaskItem [] FilesToAddToArchive { get; set; } = [];
26+
27+
public override bool RunTask ()
28+
{
29+
var dalvikPath = AndroidPackageFormat.Equals ("aab", StringComparison.InvariantCultureIgnoreCase) ? "dex/" : "";
30+
var files = new List<ITaskItem> ();
31+
32+
foreach (var dex in DalvikClasses) {
33+
var apkName = dex.GetMetadata ("ApkName");
34+
var dexPath = string.IsNullOrWhiteSpace (apkName) ? Path.GetFileName (dex.ItemSpec) : apkName;
35+
36+
var item = new TaskItem (dex.ItemSpec);
37+
item.SetMetadata ("ArchivePath", dalvikPath + dexPath);
38+
39+
files.Add (item);
40+
}
41+
42+
FilesToAddToArchive = files.ToArray ();
43+
44+
return !Log.HasLoggedErrors;
45+
}
46+
}

0 commit comments

Comments
 (0)