dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 525 forks source link

Error: Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'Microsoft.Azure.Services.AppAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Perhaps it doesn't exist in the Mono for Android profile? #1532

Closed ghuntley closed 6 years ago

ghuntley commented 6 years ago

Visual Studio for Mac 7.4.2

Bootstrap a new application using File -> New -> Xamarin.Forms application -> iOS & Android application using defaults (which creates shared project architecture)

iOS App

Install-Package Microsoft.Azure.EventHubs

Compiles and works fine.

Android

Install-Package Microsoft.Azure.EventHubs

Does not compile, left with 💣

/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(2,2): Error: Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'Microsoft.Azure.Services.AppAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Perhaps it doesn't exist in the Mono for Android profile?
File name: 'Microsoft.Azure.Services.AppAuthentication.dll'
  at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference reference, Mono.Cecil.ReaderParameters parameters) [0x00099] in /Users/builder/data/lanes/5809/a7829590/source/monodroid/external/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:229 
  at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference reference) [0x00000] in /Users/builder/data/lanes/5809/a7829590/source/monodroid/external/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:179 
  at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences (Java.Interop.Tools.Cecil.DirectoryAssemblyResolver resolver, System.Collections.Generic.ICollection`1[T] assemblies, Mono.Cecil.AssemblyDefinition assembly, System.Boolean topLevel) [0x0015c] in <bae064cadfb64dccb729d7cb7dab7861>:0 
  at Xamarin.Android.Tasks.ResolveAssemblies.Execute (Java.Interop.Tools.Cecil.DirectoryAssemblyResolver resolver) [0x001c7] in <bae064cadfb64dccb729d7cb7dab7861>:0  (BrandNewApp.Droid)
ghuntley commented 6 years ago
=== Visual Studio Enterprise 2017 for Mac ===

Version 7.4.2 (build 12)
Installation UUID: a32bd9d1-8328-4615-8629-3d85d971cc40
Runtime:
    Mono 5.8.1.0 (2017-10/6bf3922f3fd) (64-bit)
    GTK+ 2.24.23 (Raleigh theme)

    Package version: 508010000

=== NuGet ===

Version: 4.3.1.4445

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Version: 2.0.5
SDK: /usr/local/share/dotnet/sdk/2.1.4/Sdks
SDK Version: 2.1.4
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.8.1/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.6.1
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 9.3 (14154)
Build 9E145

=== Xamarin.Mac ===

Version: 4.2.1.28 (Visual Studio Enterprise)

=== Xamarin.iOS ===

Version: 11.9.1.24 (Visual Studio Enterprise)
Hash: f62de472
Branch: xcode9.3
Build date: 2018-03-29 19:30:53-0400

=== Xamarin.Android ===

Version: 8.2.0.16 (Visual Studio Enterprise)
Android SDK: /Users/ghuntley/Library/Developer/Xamarin/android-sdk-macosx
    Supported Android versions:
        4.4 (API level 19)
        5.0 (API level 21)
        5.1 (API level 22)
        6.0 (API level 23)
        7.0 (API level 24)
        7.1 (API level 25)
        8.0 (API level 26)
        8.1 (API level 27)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 27.0.1
SDK Build Tools Version: 27.0.3

Java SDK: /usr
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Inspector ===

Version: 1.4.0
Hash: b3f92f9
Branch: master
Build date: Fri, 19 Jan 2018 22:00:34 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 704020012
Git revision: 0d8e3f0a4d683771f17959739956fa09c7ba21e3
Build date: 2018-03-30 10:45:17-04
Xamarin addins: 958839ea56ab1e331caf7c92b6ad50fb9e6ee9d2
Build lane: monodevelop-lion-d15-6

=== Operating System ===

Mac OS X 10.13.4
Darwin 17.5.0 Darwin Kernel Version 17.5.0
    Mon Mar  5 22:24:32 PST 2018
    root:xnu-4570.51.1~1/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

MFractor 3.04.15
Internet of Things (IoT) development (Preview) 7.1
ghuntley commented 6 years ago

The workaround is to not install the package into Xamarin.Android directly and ignore defaults supplied by the file -> new project wizard.

IT WORKS. Kinda annoyed tho - 40mins wasted. I can see newbies wasting days/weeks on this tho or giving up on xamarin altogether.

ghuntley commented 6 years ago

Ultimate end goal is to do this https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-send

ghuntley commented 6 years ago

Repro test case at issue1532.zip

ghuntley commented 6 years ago
screen shot 2018-04-10 at 2 27 13 pm
jonathanpeppers commented 6 years ago

@ghuntley I think the issue1532.zip file is missing the Droid and iOS directories.

I'll try to repro with a new project in the meantime, it looks pretty simple to setup.

jonathanpeppers commented 6 years ago

I could repro with Visual Studio 2017 15.6.6 on Windows:

image

I am also getting the same results with xamarin-android/master.

jonathanpeppers commented 6 years ago

Microsoft.Azure.EventHubs.dll indeed has a reference to Microsoft.Azure.Services.AppAuthentication.dll:

image

It appears that installing https://www.nuget.org/packages/Microsoft.Azure.Services.AppAuthentication/1.1.0-preview fixes the issue. I believe there is an issue with this NuGet package where it should actually have a dependency on this other package, here is the nuspec (no dependency):

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>Microsoft.Azure.EventHubs</id>
    <version>2.0.0</version>
    <authors>Microsoft</authors>
    <owners>Microsoft</owners>
    <requireLicenseAcceptance>true</requireLicenseAcceptance>
    <licenseUrl>https://raw.githubusercontent.com/Azure/azure-event-hubs-dotnet/master/LICENSE</licenseUrl>
    <projectUrl>https://github.com/Azure/azure-event-hubs-dotnet</projectUrl>
    <iconUrl>https://raw.githubusercontent.com/Azure/azure-event-hubs-dotnet/master/event-hubs.png</iconUrl>
    <description>This is the next generation Azure Event Hubs .NET Standard client library. For more information about Event Hubs, see https://azure.microsoft.com/en-us/services/event-hubs/</description>
    <releaseNotes>https://github.com/Azure/azure-event-hubs-dotnet/releases</releaseNotes>
    <tags>Azure Event Hubs EventHubs .NET AMQP IoT</tags>
    <dependencies>
      <group targetFramework=".NETFramework4.6.1">
        <dependency id="Microsoft.Azure.Amqp" version="2.2.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.1" exclude="Build,Analyzers" />
        <dependency id="System.Diagnostics.DiagnosticSource" version="4.4.1" exclude="Build,Analyzers" />
        <dependency id="System.IdentityModel.Tokens.Jwt" version="5.2.1" exclude="Build,Analyzers" />
        <dependency id="System.Net.Http" version="4.3.3" exclude="Build,Analyzers" />
        <dependency id="System.Reflection.TypeExtensions" version="4.4.0" exclude="Build,Analyzers" />
      </group>
      <group targetFramework="UAP10.0">
        <dependency id="Microsoft.Azure.Amqp" version="2.2.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.1" exclude="Build,Analyzers" />
        <dependency id="Microsoft.NETCore.UniversalWindowsPlatform" version="5.2.3" exclude="Build,Analyzers" />
        <dependency id="System.Diagnostics.DiagnosticSource" version="4.4.1" exclude="Build,Analyzers" />
        <dependency id="System.IdentityModel.Tokens.Jwt" version="5.2.1" exclude="Build,Analyzers" />
        <dependency id="System.Net.Http" version="4.3.3" exclude="Build,Analyzers" />
        <dependency id="System.Reflection.TypeExtensions" version="4.4.0" exclude="Build,Analyzers" />
      </group>
      <group targetFramework=".NETStandard2.0">
        <dependency id="Microsoft.Azure.Amqp" version="2.2.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.1" exclude="Build,Analyzers" />
        <dependency id="System.Diagnostics.DiagnosticSource" version="4.4.1" exclude="Build,Analyzers" />
        <dependency id="System.IdentityModel.Tokens.Jwt" version="5.2.1" exclude="Build,Analyzers" />
        <dependency id="System.Net.Http" version="4.3.3" exclude="Build,Analyzers" />
        <dependency id="System.Reflection.TypeExtensions" version="4.4.0" exclude="Build,Analyzers" />
        <dependency id="System.Runtime.Serialization.Primitives" version="4.3.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>

Maybe they just took out the dependency so they could ship 🚢 to stable, as the dependency is still in preview?

That being said, it is the <ResolveAssemblies /> MSBuild task from Xamarin.Android failing if an assembly can't be found. It is very likely you would still get a runtime exception in a case like this if we let the build go on and the app was deployed and launched.

It is a bit weird something different is happening on Android vs iOS, so let me discuss with my team and we can see what needs to be done here. @ghuntley let me know if you see Microsoft.Azure.Services.AppAuthentication.dll somewhere and I missed it.

dellis1972 commented 6 years ago

The iOS team do not have <ResolveAssemblies /> in their build system. They make use of the msbuild variable @(ReferenceCopyLocalPaths) to figure out what assemblies need to be shipped. The down side of this is it does not always pick up ALL the required assemblies.

The <ResolveAssemblies /> task will pick up all the required dependencies regardless of if they are referenced by the main app or not. It does this by using Cecil to load the dll's and then walk its dependencies. In this case the Nuget for Microsoft.Azure.Services.AppAuthentication.dll was not installed but it appears Microsoft.Azure.EventHubs.dll has it as a reference. Which is why <ResolveAssemblies /> is complaining. This will explain why iOS behaves differently.

jonathanpeppers commented 6 years ago

Maybe if we improved the error message it would help? The existing message makes it sound like it's a missing assembly in Xamarin.Android:

Could not load assembly XYZ. Perhaps it doesn't exist in the Mono for Android profile?

This message makes sense if the assembly name started with System.*. But for other assemblies maybe it should say something like:

Could not load assembly 'Microsoft.Azure.Services.AppAuthentication'. It was referenced by assembly 'Microsoft.Azure.EventHubs', but was not found. 

Or maybe that is a better message, in general?

dellis1972 commented 6 years ago

I'm all for better error messages :) We have a helper to check if something is a Framework Assembly I think, so it that fails we can provide a newer error message.

Could not load assembly 'Microsoft.Azure.Services.AppAuthentication'. It was referenced by assembly 'Microsoft.Azure.EventHubs', but was not found.

Are you missing a reference or nuget package?

On 11 April 2018 at 14:22, Jonathan Peppers notifications@github.com wrote:

Maybe if we improved the error message it would help? The existing message makes it sound like it's a missing assembly in Xamarin.Android:

Could not load assembly XYZ. Perhaps it doesn't exist in the Mono for Android profile?

This message makes sense if the assembly name started with System.*. But for other assemblies maybe it should say something like:

Could not load assembly 'Microsoft.Azure.Services.AppAuthentication'. It was referenced by assembly 'Microsoft.Azure.EventHubs', but was not found.

Or maybe that is a better message, in general?

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/xamarin/xamarin-android/issues/1532#issuecomment-380449788, or mute the thread https://github.com/notifications/unsubscribe-auth/AAxeeaziaGVJpPOpwirK-EsKe1U4LMAmks5tngOigaJpZM4TNk6a .

ghuntley commented 6 years ago

Thanks for looking at this so quickly Jonathan Peppers. Here's the exact solution that includes the iOS and Android applications.

issue1532-rezipped.zip

screen shot 2018-04-13 at 2 12 14 pm screen shot 2018-04-13 at 2 12 48 pm

Typically I use netstandard20 style project structure. When this issue was created I was doing a quick proof of concept infront of a client and accepted the defaults shipped by the Xamarin toolchain - shared projects.

edit: problem is reproducing with netstandard20 library as well. Agreed that this is an upstream issue with the package. I'm sorry for any confusion. Either way sending in a couple PR's to improve the developer experience/error messages is highly valuable.

ghuntley commented 6 years ago

Confirming that manually installing https://www.nuget.org/packages/Microsoft.Azure.Services.AppAuthentication/1.1.0-preview into the Android project resolves the compilation issue. This isn't needed on iOS.

kairok commented 5 years ago

I added new Page. After Then I got the same error. In Xaml file I added next line

xmlns:local="clr-namespace:PageNavigation;assembly:PageNavigation" The error is gone