Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MediaSessionCompat exists in multiple assemblies #71

Closed
markmccaigue opened this issue Mar 5, 2020 · 10 comments
Closed

MediaSessionCompat exists in multiple assemblies #71

markmccaigue opened this issue Mar 5, 2020 · 10 comments
Assignees

Comments

@markmccaigue
Copy link

Xamarin Android Version: 10.1.3.7

Operating System & Version: Mac OS 10.14.6

Support Libraries Version: 28.0.0.1

Describe your Issue:

I have a project with transitive dependencies which reference the Xamarin.Android.Support.Media.Compat package, and some which reference the newer Xamarin.AndroidX.Media package.

I have installed the Xamarin.AndroidX.Migration package.

When I attempt to reference the type MediaSessionCompat, I recieve the following error message:

Error CS0433: The type 'MediaSessionCompat' exists in both 'Xamarin.Android.Support.Media.Compat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Xamarin.AndroidX.Media, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' (CS0433)

I'm not sure if this scenario is supposed to be handled by the tooling, or if there is some other change I should have made to the project as part of the migration.

Many thanks for your help.

@moljac
Copy link
Member

moljac commented Mar 7, 2020

Why do you have both Android.Support and AnroidX?

if you reference both - of course you will have the same class twice.

1st solution - use FQClassNames (Fully Qualified), do not add both namespaces in using statements.

2nd solution - If some libraries that you use reference Android.Support - contact library authors/vendors and ask THEM to update their libs to AndroidX dependencies.

Under the hood is Jettifier tool used by google too. it should work on dependencies that are not migrated to AndroidX, but there are several exceptional cases (some libraries are known issue for Android java/kotlin developers).

BTW. Can you post here your source file (usings), please?

@moljac moljac self-assigned this Mar 7, 2020
@markmccaigue
Copy link
Author

markmccaigue commented Mar 9, 2020

Hi, thanks for getting back to me.

As you say, it's not that we are taking a direct dependency on both Android.Support and AndroidX, but that some of our dependencies require AndroidX and some only have versions available with dependencies on Android.Support. I will get in touch with the vendors and request updates, but I'm hoping there is a way to have this project compile in the meantime.

I tried using fully qualified class names, but unfortunately this does not resolve things, I believe in this case that the class names exist in the same namespaces in both the Android.Support libraries and the AndroidX libraries. For example, I've checked in ILSpy and the MediaMetadataCompat type, for example, is in the Android.Support.V4.Media namespace in both assemblies.

It was my understanding that including the Xamarin.AndroidX.Migration package should allow us to proceed in this scenario, in the same manner as the Jettifier tool should.

Sure, I'll add an some example code below, thanks:

using Android.Support.V4.Media;
using Android.Support.V4.Media.Session;
using Com.Google.Android.Exoplayer2;
using Com.Google.Android.Exoplayer2.Ext.Mediasession;

namespace TheAppBuilder.Droid.BackgroundMedia
{
    public class MyQueueNavigator : TimelineQueueNavigator
    {
        public MyQueueNavigator(MediaSessionCompat mediaSession) : base(mediaSession)
        {
        }

        public override MediaDescriptionCompat GetMediaDescription(IPlayer player, int windowIndex)
        {
            var metadata = BackgroundAudioPlayerService.CurrentTrack;

            var builder = new MediaDescriptionCompat.Builder()
                     .SetMediaId(metadata.GetString(MediaMetadataCompat.MetadataKeyMediaId))
                     .SetTitle(metadata.GetString(MediaMetadataCompat.MetadataKeyDisplayTitle))
                     .SetSubtitle(metadata.GetString(MediaMetadataCompat.MetadataKeyDisplaySubtitle))
                     .SetDescription(metadata.GetString(MediaMetadataCompat.MetadataKeyDisplayDescription));

            var displayIconUri = metadata.GetString(MediaMetadataCompat.MetadataKeyDisplayIconUri);

            if (displayIconUri != null)
            {
                builder.SetIconUri(UriUtility.FromString(displayIconUri));
            }

            return builder.Build();
        }
    }
}

In this file, we have issues with the MediaDescriptionCompat, MediaMetadataCompat, and MediaSessionCompat.

@moljac
Copy link
Member

moljac commented Mar 9, 2020

@markmccaigue

thanks for getting back to me.

You're welcome. BTW no reason to thank me. Helping is the reason why we are here for.

It was my understanding that including the Xamarin.AndroidX.Migration package should allow
us to proceed in this scenario, in the same manner as the Jettifier tool should.

Not even google can solve all problems with jettifier. There are libraries which cause problems in java/kotlin world with AndroidX. And we add substantial amount of complexity on top of java/kotlin world.
Xamarin.AndroidX.Migration package works in most cases, but there could be problems for sure.

Is your code publicly open so I/we could check?

And: forget ExoPlayer and Player packages for now. They are quite complex to bind and I simply cannot gat some time to finish them. (I dislike to do <remove-node> during bindings).

@moljac
Copy link
Member

moljac commented Mar 9, 2020

Related (possible duplicate):

#62

@moljac
Copy link
Member

moljac commented Mar 9, 2020

Related (possible duplicate):

#62

@markmccaigue

Why not trying tips described here?

This is an issue we know about, but there may be a workaround:

Whole discussion:

#55

@markmccaigue
Copy link
Author

markmccaigue commented Mar 9, 2020

@moljac

Cool, that all makes sense. The code isn't open source, but I could probably put together a small reproduction app if you would like one?

I went through the workarounds in the issueyou linked. Removing and re-adding the assembly (first suggestion) resulted in different error messages:

'MyQueueNavigator.GetMediaDescription(IPlayer, int)': return type must be 'MediaDescriptionCompat' to match overridden member 'TimelineQueueNavigator.GetMediaDescription(IPlayer, int)'

The type 'MediaDescriptionCompat' is defined in an assembly that is not referenced. You must add a reference to assembly 'Xamarin.Android.Support.Media.Compat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

The type 'MediaSessionCompat' is defined in an assembly that is not referenced. You must add a reference to assembly 'Xamarin.Android.Support.Media.Compat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

I suspect these might be due to the fact that I'm overriding a contract owned by a dependency that owns the references and points them at Support, but this is just a guess.

The good news is that the other workaround, of defining an extern alias, has indeed solved the issue! This seems to work fine on Visual Studio on Windows, however on Visual Studio for Mac there are visual errors highlighed, although the actual compilation completes with no problems.

In case anyone else runs into this issue the following is the target I added to the bottom of my Droid .csproj file:

<Target Name="AddCustomAliases" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'Xamarin.Android.Support.Media.Compat' AND '%(ReferencePath.NuGetPackageId)' == 'Xamarin.Android.Support.Media.Compat'">
        <Aliases>AndroidSupportMedia</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>

making the code file above:

extern alias AndroidSupportMedia;

using AndroidSupportMedia::Android.Support.V4.Media;
using AndroidSupportMedia::Android.Support.V4.Media.Session;
using Com.Google.Android.Exoplayer2;
using Com.Google.Android.Exoplayer2.Ext.Mediasession;

namespace TheAppBuilder.Droid.BackgroundMedia
{
    public class MyQueueNavigator : TimelineQueueNavigator
    {
        public MyQueueNavigator(MediaSessionCompat mediaSession) : base(mediaSession)
        {
        }

        public override MediaDescriptionCompat GetMediaDescription(IPlayer player, int windowIndex)
        {
            var metadata = BackgroundAudioPlayerService.CurrentTrack;

            var displayIconUri = metadata.GetString(MediaMetadataCompat.MetadataKeyDisplayIconUri);

            var builder = new MediaDescriptionCompat.Builder()
                     .SetMediaId(metadata.GetString(MediaMetadataCompat.MetadataKeyMediaId))
                     .SetTitle(metadata.GetString(MediaMetadataCompat.MetadataKeyDisplayTitle))
                     .SetSubtitle(metadata.GetString(MediaMetadataCompat.MetadataKeyDisplaySubtitle))
                     .SetDescription(metadata.GetString(MediaMetadataCompat.MetadataKeyDisplayDescription));

            if (displayIconUri != null)
            {
                builder.SetIconUri(UriUtility.FromString(displayIconUri));
            }

            return builder.Build();
        }
    }
}

@moljac
Copy link
Member

moljac commented Mar 18, 2020

@markmccaigue

Sorry for delay, but I wanted to release new set of AndroidX (Corona) updates, before I get back to issues and fixing 'em.

but I could probably put together a small reproduction app if you would like one?

Sure. you know the more samples we/I have - the better. We'll hit issues sooner, before they get to users (you). So,YES please.

Where did you find ExoPlayer bindings?

@markmccaigue
Copy link
Author

@moljac

Hey, also apologies for the delay. It might be some time before I can put together a repro, but I will get to it.

In the meantime, I'm using these ExoPlayer bindings :)

https://github.com/Baseflow/ExoPlayerXamarin

@moljac
Copy link
Member

moljac commented Apr 15, 2020

@markmccaigue
Thanks. I found it, but confirmation is great.
That ExoPlayer is not AndroidX version.

@moljac
Copy link
Member

moljac commented Feb 18, 2021

Closing this one, because it is stale.
Open new issue if the problem still persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants