Skip to content

Commit 0bd26fd

Browse files
dellis1972jonpryor
authored andcommitted
[Xamarin.Android.Build.Tasks] Don't package reference assemblies (#706)
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=57342 The investigation in the bug suggests that we are picking 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 c7222bc commit 0bd26fd

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ TEST_APK_PROJECTS = \
148148
# Syntax: $(call BUILD_TEST_APK,path/to/project.csproj)
149149
define BUILD_TEST_APK
150150
# Must use xabuild to ensure correct assemblies are resolved
151-
MSBUILD="$(MSBUILD)" tools/scripts/xabuild /t:SignAndroidPackage $(1)
151+
MSBUILD="$(MSBUILD)" tools/scripts/xabuild $(MSBUILD_FLAGS) /t:SignAndroidPackage $(1)
152152
endef # BUILD_TEST_APK
153153

154154
run-apk-tests:

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

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

276276
int count = 0;
277277
foreach (ITaskItem assembly in ResolvedUserAssemblies) {
278+
279+
if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {
280+
Log.LogWarning ($"{assembly.ItemSpec} is a reference assembly!");
281+
}
278282
// Add assembly
279283
apk.Archive.AddFile (assembly.ItemSpec, GetTargetDirectory (assembly.ItemSpec) + "/" + Path.GetFileName (assembly.ItemSpec), compressionMethod: CompressionMethod.Store);
280284

@@ -307,6 +311,9 @@ private void AddAssemblies (ZipArchiveEx apk)
307311
count = 0;
308312
// Add framework assemblies
309313
foreach (ITaskItem assembly in ResolvedFrameworkAssemblies) {
314+
if (MonoAndroidHelper.IsReferenceAssembly (assembly.ItemSpec)) {
315+
Log.LogWarning ($"{assembly.ItemSpec} is a reference assembly!");
316+
}
310317
apk.Archive.AddFile (assembly.ItemSpec, "assemblies/" + Path.GetFileName (assembly.ItemSpec), compressionMethod: CompressionMethod.Store);
311318
var config = Path.ChangeExtension (assembly.ItemSpec, "dll.config");
312319
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;
@@ -260,6 +261,14 @@ public static bool IsFrameworkAssembly (string assembly, bool checkSdkPath)
260261
return TargetFrameworkDirectories == null || !checkSdkPath ? false : ExistsInFrameworkPath (assembly);
261262
}
262263

264+
public static bool IsReferenceAssembly (string assembly)
265+
{
266+
var a = AssemblyDefinition.ReadAssembly (assembly, new ReaderParameters() { InMemory = true, ReadSymbols = false, });
267+
if (!a.HasCustomAttributes)
268+
return false;
269+
return a.CustomAttributes.Any (t => t.AttributeType.FullName == "System.Runtime.CompilerServices.ReferenceAssemblyAttribute");
270+
}
271+
263272
public static bool ExistsInFrameworkPath (string assembly)
264273
{
265274
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
@@ -1429,7 +1429,11 @@ because xbuild doesn't support framework reference assemblies.
14291429
<Target Name="_ResolveAssemblies">
14301430
<!--- Remove the ImplicitlyExpandDesignTimeFacades assemblies. We have already build the app there are not required for packaging -->
14311431
<ItemGroup>
1432-
<FilteredAssemblies Include="@(ReferencePath)" Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades'" />
1432+
<FilteredAssemblies Include="%(ReferenceCopyLocalPaths.Identity)"
1433+
Condition="'%(ReferenceCopyLocalPaths.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And '%(ReferenceCopyLocalPaths.Extension)' == '.dll' And '%(ReferenceCopyLocalPaths.DestinationSubDirectory)' == '' "/>
1434+
<!-- Fallback to @(ReferencePath) if @(ReferenceCopyLocalPaths) is empty. This is for xbuild support -->
1435+
<FilteredAssemblies Include="%(ReferencePath.Identity)"
1436+
Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And '@(ReferenceCopyLocalPaths)' == '' "/>
14331437
</ItemGroup>
14341438
<!-- Find all the assemblies this app requires -->
14351439
<ResolveAssemblies

0 commit comments

Comments
 (0)