maui icon indicating copy to clipboard operation
maui copied to clipboard

Android Java Library Binding breaks intellisense, despite build/run working

Open dan6518 opened this issue 2 years ago • 3 comments

Description

This issue happens in Visual Studio 17.9.6, and also JetBrains Rider 2023.3.4 for what it is worth, with the latest MAUI project template as of the time of writing this bug report.

I'm following this documentation to port a Xamarin project to MAUI, but I have ran into an issue where intellisense insists that there are errors in the project, despite the project building and running. The errors appear on any line of code that uses native Android Java libraries. I have attached a project that exhibits this behavior, and in the reproduction steps I have outlined how I created that project. To see that the project is in fact able to run you can set a breakpoint in Platforms/Android/MainApplication.cs and see that LangKo from the native library can be accessed.

In the reproduction project I have included sample .jar files, but this issue does not seem to be specific to only these .jar files. It happens (maybe sporadically?) with other Android Java libraries as well.

Here are the specific intellisense errors in my example reproduction, but do note that the issue is not specific only to these errors. Any attempt at using code from the native library gives intellisense errors.

The type or namespace name 'Com' could not be found (are you missing a using directive or an assembly reference?)
The name 'Sam4sBuilder' does not exist in the current context

I had to manually add this to the MAUI project .csproj file to exclude the Android-specific library from the non-Android projects:

	<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
		<ProjectReference Include="..\Sam4sAndroidBinding\Sam4sAndroidBinding.csproj" />
	</ItemGroup>

Please let me know if you need more information, or if this is a known bug with more information about it elsewhere. It is also entirely possible that I have missed something with regards to how you are supposed to include native Android libraries, so I would love to know if there is something wrong with my approach as well.

Steps to Reproduce

  1. Create a new .NET 8 Maui App.
  2. Add a new "Android Java Library Binding" project.
  3. Add an Android .jar library into the binding project, confirm the build action is "AndroidLibrary".
  4. Reference the binding project from the main Maui project.
  5. Exclude the binding project from non-android compilations, see description above.
  6. Try to use anything from the library in (Android-specific) C# code and notice how intellisense claims everything is an error, namespaces can't be found for example.
  7. Build and run the application on an Android emulator, use breakpoints and the debugger to confirm that the native code is actually called, in spite of the intellisense errors saying it should be impossible. For example, see the code in Platforms/Android/MainApplication.cs in the included reproduction project.

Link to public reproduction project repository

https://github.com/dan6518/MauiIntellisenseBug

Version with bug

8.0.3 GA

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

I did not find any workarounds yet.

Relevant log output

No response

dan6518 avatar Apr 11 '24 14:04 dan6518

@jonathanpeppers ?

PureWeen avatar Apr 11 '24 18:04 PureWeen

@mattleibow I think this is a symptom of moving MAUI to NuGet packages....

The design-time build for the project above fails with:

C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.79\tools\Xamarin.Android.Common.targets(613,3):
error XA1018: Specified AndroidManifest file does not exist: D:\src\MauiIntellisenseBug\MauiIntellisenseBug\AndroidManifest.xml.
[D:\src\MauiIntellisenseBug\MauiIntellisenseBug\MauiIntellisenseBug.csproj]

Because MAUI is not yet restored at this point, this value is blank: https://github.com/dotnet/maui/blob/0a562dcd1de72db1d52426fdd89c29e02fb00f1b/src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.SingleProject.targets#L11

The Android workload only looks for the AndroidManifest.xml in the root directory, or in Properties/AndroidManifest.xml for Xamarin compatibility.

Unfortunately, the Android design-time build requires AndroidManifest.xml to exist as we have to pass it to aapt2.

Can we move some of this logic inside the workload?

jonathanpeppers avatar Apr 12 '24 21:04 jonathanpeppers

@jonathanpeppers Thank you so much for looking into this issue!

Is there anything in particular with the way we have set up our project that causes this, and/or are there any workarounds we can use for the time being? Maybe an older version of MAUI could work for us until the root cause has been addressed?

dan6518 avatar Apr 12 '24 21:04 dan6518