Skip to content

Commit d9c0dfe

Browse files
committed
Add ability to override from MSBuild
1 parent c43ed51 commit d9c0dfe

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

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

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public class BuildApk : AndroidTask
9595

9696
public bool UseAssemblyStore { get; set; }
9797

98+
public string ZipFlushFilesLimit { get; set; }
99+
100+
public string ZipFlushSizeLimit { get; set; }
101+
98102
[Required]
99103
public string ProjectFullPath { get; set; }
100104

@@ -206,7 +210,6 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut
206210
AddFileToArchiveIfNewer (apk, RuntimeConfigBinFilePath, $"{AssembliesPath}rc.bin", compressionMethod: UncompressedMethod);
207211
}
208212

209-
int count = 0;
210213
foreach (var file in files) {
211214
var item = Path.Combine (file.archivePath.Replace (Path.DirectorySeparatorChar, '/'));
212215
existingEntries.Remove (item);
@@ -216,12 +219,7 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut
216219
continue;
217220
}
218221
Log.LogDebugMessage ("\tAdding {0}", file.filePath);
219-
apk.Archive.AddFile (file.filePath, item, compressionMethod: compressionMethod);
220-
count++;
221-
if (count >= ZipArchiveEx.ZipFlushFilesLimit) {
222-
apk.Flush();
223-
count = 0;
224-
}
222+
apk.AddFileAndFlush (file.filePath, item, compressionMethod: compressionMethod);
225223
}
226224

227225
var jarFiles = (JavaSourceFiles != null) ? JavaSourceFiles.Where (f => f.ItemSpec.EndsWith (".jar", StringComparison.OrdinalIgnoreCase)) : null;
@@ -236,7 +234,6 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut
236234
var jarFilePaths = libraryProjectJars.Concat (jarFiles != null ? jarFiles.Select (j => j.ItemSpec) : Enumerable.Empty<string> ());
237235
jarFilePaths = MonoAndroidHelper.DistinctFilesByContent (jarFilePaths);
238236

239-
count = 0;
240237
foreach (var jarFile in jarFilePaths) {
241238
using (var stream = File.OpenRead (jarFile))
242239
using (var jar = ZipArchive.Open (stream)) {
@@ -278,14 +275,9 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut
278275
data = d.ToArray ();
279276
}
280277
Log.LogDebugMessage ($"Adding {path} from {jarFile} as the archive file is out of date.");
281-
apk.Archive.AddEntry (data, path);
278+
apk.AddEntryAndFlush (data, path);
282279
}
283280
}
284-
count++;
285-
if (count >= ZipArchiveEx.ZipFlushFilesLimit) {
286-
apk.Flush();
287-
count = 0;
288-
}
289281
}
290282
// Clean up Removed files.
291283
foreach (var entry in existingEntries) {
@@ -302,6 +294,12 @@ void ExecuteWithAbi (string [] supportedAbis, string apkInputPath, string apkOut
302294

303295
public override bool RunTask ()
304296
{
297+
if (int.TryParse (ZipFlushFilesLimit, out int flushFilesLimit)) {
298+
ZipArchiveEx.ZipFlushFilesLimit = flushFilesLimit;
299+
}
300+
if (int.TryParse (ZipFlushSizeLimit, out int flushSizeLimit)) {
301+
ZipArchiveEx.ZipFlushSizeLimit = flushSizeLimit;
302+
}
305303
Aot.TryGetSequencePointsMode (AndroidSequencePointsMode, out sequencePointsMode);
306304

307305
var outputFiles = new List<string> ();
@@ -377,7 +375,6 @@ void AddAssemblies (ZipArchiveEx apk, bool debug, bool compress, IDictionary<str
377375
storeGenerator = null;
378376
}
379377

380-
int count = 0;
381378
AssemblyStoreAssemblyInfo storeAssembly = null;
382379

383380
//
@@ -398,7 +395,6 @@ void AddAssemblies (ZipArchiveEx apk, bool debug, bool compress, IDictionary<str
398395
AddAssembliesFromCollection (ResolvedUserAssemblies);
399396

400397
// Add framework assemblies
401-
count = 0;
402398
AddAssembliesFromCollection (ResolvedFrameworkAssemblies);
403399

404400
if (!UseAssemblyStore) {
@@ -482,12 +478,6 @@ void AddAssembliesFromCollection (ITaskItem[] assemblies)
482478

483479
if (UseAssemblyStore) {
484480
storeGenerator.Add (assemblyStoreApkName, storeAssembly);
485-
} else {
486-
count++;
487-
if (count >= ZipArchiveEx.ZipFlushFilesLimit) {
488-
apk.Flush();
489-
count = 0;
490-
}
491481
}
492482
}
493483
}
@@ -554,7 +544,7 @@ bool AddFileToArchiveIfNewer (ZipArchiveEx apk, string file, string inArchivePat
554544
return false;
555545
}
556546
Log.LogDebugMessage ($"Adding {file} as the archive file is out of date.");
557-
apk.Archive.AddFile (file, inArchivePath, compressionMethod: compressionMethod);
547+
apk.AddFileAndFlush (file, inArchivePath, compressionMethod: compressionMethod);
558548
return true;
559549
}
560550

@@ -578,7 +568,7 @@ void AddAssemblyConfigEntry (ZipArchiveEx apk, string assemblyPath, string confi
578568
source.CopyTo (dest);
579569
dest.WriteByte (0);
580570
dest.Position = 0;
581-
apk.Archive.AddEntry (inArchivePath, dest, compressionMethod);
571+
apk.AddEntryAndFlush (inArchivePath, dest, compressionMethod);
582572
}
583573
}
584574

@@ -625,7 +615,7 @@ void AddNativeLibraryToArchive (ZipArchiveEx apk, string abi, string filesystemP
625615
return;
626616
}
627617
Log.LogDebugMessage ($"Adding native library: {filesystemPath} (APK path: {archivePath})");
628-
apk.Archive.AddEntry (archivePath, File.OpenRead (filesystemPath), compressionMethod);
618+
apk.AddEntryAndFlush (archivePath, File.OpenRead (filesystemPath), compressionMethod);
629619
}
630620

631621
void AddRuntimeLibraries (ZipArchiveEx apk, string [] supportedAbis)

src/Xamarin.Android.Build.Tasks/Utilities/ZipArchiveEx.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ void AddFileAndFlush (string filename, long fileLength, string archiveFileName,
7070
}
7171
}
7272

73+
public void AddFileAndFlush (string filename, string archiveFileName, CompressionMethod compressionMethod)
74+
{
75+
var fi = new FileInfo (filename);
76+
AddFileAndFlush (filename, fi.Length, archiveFileName, compressionMethod);
77+
}
78+
79+
public void AddEntryAndFlush (byte[] data, string archiveFileName)
80+
{
81+
filesWrittenTotalSize += data.Length;
82+
zip.AddEntry (data, archiveFileName);
83+
if ((filesWrittenTotalSize >= ZipArchiveEx.ZipFlushSizeLimit || filesWrittenTotalCount >= ZipArchiveEx.ZipFlushFilesLimit) && AutoFlush) {
84+
Flush ();
85+
}
86+
}
87+
88+
public void AddEntryAndFlush (string archiveFileName, Stream data, CompressionMethod method)
89+
{
90+
filesWrittenTotalSize += data.Length;
91+
zip.AddEntry (archiveFileName, data, method);
92+
if ((filesWrittenTotalSize >= ZipArchiveEx.ZipFlushSizeLimit || filesWrittenTotalCount >= ZipArchiveEx.ZipFlushFilesLimit) && AutoFlush) {
93+
Flush ();
94+
}
95+
}
96+
7397
void AddFiles (string folder, string folderInArchive, CompressionMethod method)
7498
{
7599
foreach (string fileName in Directory.GetFiles (folder, "*.*", SearchOption.TopDirectoryOnly)) {

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,8 @@ because xbuild doesn't support framework reference assemblies.
21522152
CheckedBuild="$(_AndroidCheckedBuild)"
21532153
RuntimeConfigBinFilePath="$(_BinaryRuntimeConfigPath)"
21542154
ExcludeFiles="@(AndroidPackagingOptionsExclude)"
2155+
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
2156+
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)"
21552157
UseAssemblyStore="$(AndroidUseAssemblyStore)">
21562158
<Output TaskParameter="OutputFiles" ItemName="ApkFiles" />
21572159
</BuildApk>
@@ -2185,6 +2187,8 @@ because xbuild doesn't support framework reference assemblies.
21852187
CheckedBuild="$(_AndroidCheckedBuild)"
21862188
RuntimeConfigBinFilePath="$(_BinaryRuntimeConfigPath)"
21872189
ExcludeFiles="@(AndroidPackagingOptionsExclude)"
2190+
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
2191+
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)"
21882192
UseAssemblyStore="$(AndroidUseAssemblyStore)">
21892193
<Output TaskParameter="OutputFiles" ItemName="BaseZipFile" />
21902194
</BuildBaseAppBundle>

0 commit comments

Comments
 (0)