Assembly References Getting Added to Every Platform in .NET MAUI

Dewangan, Khagesh 45 Reputation points
2023-04-03T05:50:40.8466667+00:00

In the MAUI application, If you add an assembly reference to one Platform, it gets added to every Platform.

Steps to Reproduce:

  1. Create a new .NET MAUI App
  2. Under same solution created an Android Class Library project(<TargetFramework>net7.0-android</TargetFramework>)
  3. Right-click on Maui main project Dependencies
  4. Add a new project reference (a local DLL, not a NuGet package)
  5. Observe the reference was also added to every Platform dependencies (Android, iOS, maccatalyst)

Even if i add Condition like : **Condition="'$(TargetFramework)' == 'net7.0-android'"**or Condition="$(TargetFramework.Contains('-android')) != false ", It did not work.

I am in the lasted Visual Studio for MAC. 

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,005 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,126 Reputation points Microsoft Vendor
    2023-04-04T07:35:54.0166667+00:00

    Hello, The condition statement you set is correct, and you can refer to the following full item group: (AndroidCustomLibrary is the name of an Android Class Library project)

    
    <ItemGroup >
          <Reference Include="AndroidCustomLibrary" Condition="'$(TargetFramework)' == 'net7.0-android'"><!--or Condition="$(TargetFramework.StartsWith('net7.0-android'))"-->
            <HintPath>..\AndroidCustomLibrary\obj\Debug\net7.0-android\AndroidCustomLibrary.dll</HintPath>
          </Reference>
        </ItemGroup>
    
    

    It's the default behavior that the reference was also added to every Platform dependency. However, it's not available when you use the namespace. When I type using AndroidCustomLibrary; in MainPage, there will be a warning button that shows it's not available on iOS and Maccatalyst. For this, you can try conditional compilation:

    #if ANDROID  
    using AndroidCustomLibrary;  
    #endif
    

    Or sending feedback with VS, please see Report a problem or suggestions for Visual Studio for Mac Best Regards, Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful