Skip to content

Commit 8bf82bd

Browse files
committed
[Xamarin.Android.Build.Tasks] Crash - NetStandard library - could not load assembly during startup registration.
Context https://bugzilla.xamarin.com/show_bug.cgi?id=57342 The investigation in the bug suggests that we are pickin up a reference assembly rather than a the actual implementation. This is to do with the way netstandard nuget packages work, they include both 'ref' and 'lib' folders. In this case 'ref' was being included in the package rather than 'lib'. This commit alters the `_ResolveAssemblies` to use `@(ReferenceCopyLocalPaths)` This ItemGroup is populated with the correct items.
1 parent 8688832 commit 8bf82bd

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ private void AddAssemblies (ZipArchiveEx apk)
273273

274274
int count = 0;
275275
foreach (ITaskItem assembly in ResolvedUserAssemblies) {
276+
277+
if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {
278+
Log.LogWarning ($"{assembly.ItemSpec} is a reference assembly!");
279+
}
276280
// Add assembly
277281
apk.Archive.AddFile (assembly.ItemSpec, GetTargetDirectory (assembly.ItemSpec) + "/" + Path.GetFileName (assembly.ItemSpec), compressionMethod: CompressionMethod.Store);
278282

@@ -305,6 +309,9 @@ private void AddAssemblies (ZipArchiveEx apk)
305309
count = 0;
306310
// Add framework assemblies
307311
foreach (ITaskItem assembly in ResolvedFrameworkAssemblies) {
312+
if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {
313+
Log.LogWarning ($"{assembly.ItemSpec} is a reference assembly!");
314+
}
308315
apk.Archive.AddFile (assembly.ItemSpec, "assemblies/" + Path.GetFileName (assembly.ItemSpec), compressionMethod: CompressionMethod.Store);
309316
var config = Path.ChangeExtension (assembly.ItemSpec, "dll.config");
310317
AddAssemblyConfigEntry (apk, config);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Mono.Security.Cryptography;
99
using Xamarin.Android.Build.Utilities;
1010
using Xamarin.Tools.Zip;
11+
using Mono.Cecil;
1112

1213
#if MSBUILD
1314
using Microsoft.Build.Framework;
@@ -286,6 +287,14 @@ public static bool IsFrameworkAssembly (string assembly, bool checkSdkPath)
286287
return TargetFrameworkDirectories == null || !checkSdkPath ? false : ExistsInFrameworkPath (assembly);
287288
}
288289

290+
public static bool IsReferenceAssembly (string assembly)
291+
{
292+
var a = AssemblyDefinition.ReadAssembly (assembly, new ReaderParameters() { InMemory = true, ReadSymbols = false, });
293+
if (!a.HasCustomAttributes)
294+
return false;
295+
return a.CustomAttributes.Any (t => t.AttributeType.FullName == "System.Runtime.CompilerServices.ReferenceAssemblyAttribute");
296+
}
297+
289298
public static bool ExistsInFrameworkPath (string assembly)
290299
{
291300
return TargetFrameworkDirectories

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,11 @@ because xbuild doesn't support framework reference assemblies.
14111411
<Target Name="_ResolveAssemblies">
14121412
<!--- Remove the ImplicitlyExpandDesignTimeFacades assemblies. We have already build the app there are not required for packaging -->
14131413
<ItemGroup>
1414-
<FilteredAssemblies Include="@(ReferencePath)" Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades'" />
1414+
<FilteredAssemblies Include="%(ReferenceCopyLocalPaths.Identity)"
1415+
Condition="'%(ReferenceCopyLocalPaths.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And '%(ReferenceCopyLocalPaths.Extension)' == '.dll' "/>
1416+
<!-- Fallback to @(ReferencePath) if @(ReferenceCopyLocalPaths) is empty. This is for xbuild support -->
1417+
<FilteredAssemblies Include="%(ReferencePath.Identity)"
1418+
Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And '@(ReferenceCopyLocalPaths)' == '' "/>
14151419
</ItemGroup>
14161420
<!-- Find all the assemblies this app requires -->
14171421
<ResolveAssemblies

0 commit comments

Comments
 (0)