Skip to content

Commit 8f2ae24

Browse files
dellis1972jonpryor
authored andcommitted
[Xamarin.Android.Build.Tasks] Don't package reference assemblies (dotnet#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 ac05b8b commit 8f2ae24

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
@@ -149,7 +149,7 @@ TEST_APK_PROJECTS = \
149149
# Syntax: $(call BUILD_TEST_APK,path/to/project.csproj)
150150
define BUILD_TEST_APK
151151
# Must use xabuild to ensure correct assemblies are resolved
152-
MSBUILD="$(MSBUILD)" tools/scripts/xabuild /t:SignAndroidPackage $(1)
152+
MSBUILD="$(MSBUILD)" tools/scripts/xabuild $(MSBUILD_FLAGS) /t:SignAndroidPackage $(1)
153153
endef # BUILD_TEST_APK
154154

155155
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
@@ -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
@@ -1417,7 +1417,11 @@ because xbuild doesn't support framework reference assemblies.
14171417
<Target Name="_ResolveAssemblies">
14181418
<!--- Remove the ImplicitlyExpandDesignTimeFacades assemblies. We have already build the app there are not required for packaging -->
14191419
<ItemGroup>
1420-
<FilteredAssemblies Include="@(ReferencePath)" Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades'" />
1420+
<FilteredAssemblies Include="%(ReferenceCopyLocalPaths.Identity)"
1421+
Condition="'%(ReferenceCopyLocalPaths.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And '%(ReferenceCopyLocalPaths.Extension)' == '.dll' And '%(ReferenceCopyLocalPaths.DestinationSubDirectory)' == '' "/>
1422+
<!-- Fallback to @(ReferencePath) if @(ReferenceCopyLocalPaths) is empty. This is for xbuild support -->
1423+
<FilteredAssemblies Include="%(ReferencePath.Identity)"
1424+
Condition="'%(ReferencePath.ResolvedFrom)' != 'ImplicitlyExpandDesignTimeFacades' And '@(ReferenceCopyLocalPaths)' == '' "/>
14211425
</ItemGroup>
14221426
<!-- Find all the assemblies this app requires -->
14231427
<ResolveAssemblies

0 commit comments

Comments
 (0)