[Xamarin.Android.Build.Tasks] StripEmbeddedLibraries and ReadWrite (#239)

[Xamarin.Android.Build.Tasks] StripEmbeddedLibraries and ReadWrite

Fixes part of: https://bugzilla.xamarin.com/show_bug.cgi?id=44529

When running the `<StripEmbeddedLibraries/>` task, an `IOException`
may be thrown:

	Error executing task StripEmbeddedLibraries: System.IO.IOException: Sharing violation on path .../obj/Release/linksrc/Xamarin.Android.NUnitLite.dll
	  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0025f] in <253a3790b2c44512bbca8669ecfc1822>:0
	  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <253a3790b2c44512bbca8669ecfc1822>:0
	  at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
	  at Mono.Cecil.ModuleDefinition.GetFileStream (System.String fileName, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00007] in :0
	  at Mono.Cecil.ModuleDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00007] in :0
	  at Mono.Cecil.AssemblyDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00001] in :0
	  at Xamarin.Android.Tasks.StripEmbeddedLibraries.Execute () [0x0034a] in <3d5202a5d4874a76a99388021bf1ab1a>:0

This was caused as part of commit 1d658252, which bumped us to
Cecil 0.10.0-beta1-v2, in which `AssemblyDefinition` now holds a
`Stream` to the file that was opened, instead of being a purely
in-memory construct, which was the case in Cecil 0.9.x.

Because `AssemblyDefinition` held a `Stream` to the source assembly,
calling `AssemblyDefinition.Write(string, WeriterParameters)` would
attempt to *re-open* the file for writing, which resulteed in the
above `IOException`.

Fix this by telling `DirectoryAssemblyResolver` to open all
assemblies as read/write and using the
`AssemblyDefinition.Write(WriterParameters)` overload, so that
`AssemblyDefinition` re-uses the existing `Stream` to write the file.
This commit is contained in:
Radek Doulik 2016-09-27 21:06:47 +02:00 коммит произвёл Jonathan Pryor
Родитель 6cfe2a3ee8
Коммит 453a80da76
1 изменённых файлов: 2 добавлений и 2 удалений

Просмотреть файл

@ -28,7 +28,7 @@ namespace Xamarin.Android.Tasks
Log.LogDebugMessage ("StripEmbeddedLibraries Task");
Log.LogDebugTaskItems (" Assemblies: ", Assemblies);
using (var res = new DirectoryAssemblyResolver (Log.LogWarning, true)) {
using (var res = new DirectoryAssemblyResolver (Log.LogWarning, true, new ReaderParameters { ReadWrite = true } )) {
return Execute (res);
}
}
@ -83,7 +83,7 @@ namespace Xamarin.Android.Tasks
WriteSymbols = assembly.MainModule.HasSymbols
};
assembly.Write (assemblyPath, wp);
assembly.Write (wp);
}
}
return true;