Skip to content

Commit 2940d2e

Browse files
committed
Add ability to override from MSBuild
1 parent 7bfc80e commit 2940d2e

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) {
@@ -488,12 +484,6 @@ void AddAssembliesFromCollection (ITaskItem[] assemblies)
488484

489485
if (UseAssemblyStore) {
490486
storeGenerator.Add (assemblyStoreApkName, storeAssembly);
491-
} else {
492-
count++;
493-
if (count >= ZipArchiveEx.ZipFlushFilesLimit) {
494-
apk.Flush();
495-
count = 0;
496-
}
497487
}
498488
}
499489
}
@@ -560,7 +550,7 @@ bool AddFileToArchiveIfNewer (ZipArchiveEx apk, string file, string inArchivePat
560550
return false;
561551
}
562552
Log.LogDebugMessage ($"Adding {file} as the archive file is out of date.");
563-
apk.Archive.AddFile (file, inArchivePath, compressionMethod: compressionMethod);
553+
apk.AddFileAndFlush (file, inArchivePath, compressionMethod: compressionMethod);
564554
return true;
565555
}
566556

@@ -584,7 +574,7 @@ void AddAssemblyConfigEntry (ZipArchiveEx apk, string assemblyPath, string confi
584574
source.CopyTo (dest);
585575
dest.WriteByte (0);
586576
dest.Position = 0;
587-
apk.Archive.AddEntry (inArchivePath, dest, compressionMethod);
577+
apk.AddEntryAndFlush (inArchivePath, dest, compressionMethod);
588578
}
589579
}
590580

@@ -631,7 +621,7 @@ void AddNativeLibraryToArchive (ZipArchiveEx apk, string abi, string filesystemP
631621
return;
632622
}
633623
Log.LogDebugMessage ($"Adding native library: {filesystemPath} (APK path: {archivePath})");
634-
apk.Archive.AddEntry (archivePath, File.OpenRead (filesystemPath), compressionMethod);
624+
apk.AddEntryAndFlush (archivePath, File.OpenRead (filesystemPath), compressionMethod);
635625
}
636626

637627
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
@@ -2162,6 +2162,8 @@ because xbuild doesn't support framework reference assemblies.
21622162
CheckedBuild="$(_AndroidCheckedBuild)"
21632163
RuntimeConfigBinFilePath="$(_BinaryRuntimeConfigPath)"
21642164
ExcludeFiles="@(AndroidPackagingOptionsExclude)"
2165+
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
2166+
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)"
21652167
UseAssemblyStore="$(AndroidUseAssemblyStore)">
21662168
<Output TaskParameter="OutputFiles" ItemName="ApkFiles" />
21672169
</BuildApk>
@@ -2195,6 +2197,8 @@ because xbuild doesn't support framework reference assemblies.
21952197
CheckedBuild="$(_AndroidCheckedBuild)"
21962198
RuntimeConfigBinFilePath="$(_BinaryRuntimeConfigPath)"
21972199
ExcludeFiles="@(AndroidPackagingOptionsExclude)"
2200+
ZipFlushFilesLimit="$(_ZipFlushFilesLimit)"
2201+
ZipFlushSizeLimit="$(_ZipFlushSizeLimit)"
21982202
UseAssemblyStore="$(AndroidUseAssemblyStore)">
21992203
<Output TaskParameter="OutputFiles" ItemName="BaseZipFile" />
22002204
</BuildBaseAppBundle>

0 commit comments

Comments
 (0)