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.93k stars 529 forks source link

`AndroidLibrary` `Pack='false'` adds `.jar` to NuGet package #8504

Open UlyssesWu opened 11 months ago

UlyssesWu commented 11 months ago

Android application type

.NET Android (net7.0-android, etc.)

Affected platform version

VS 2022 17.8, VS 2022 17.9

Description

https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/OneDotNetBindingProjects.md

With the new OneDotNetBindingProjects, it seems that I cannot create a binding C# project for a jar, then only reference it in my main android project, but do NOT pack the jar code into the final apk. My project XamarinPosed achieved this (also tricky) on Classic Xamarin.Android. But for the new XamarinPosed with .NET 8, if I apply similar tricks, VS Intellisense will stop working for members in binding project, and there are CS0246 and CS0103 errors. However, the project can still be built.

image

Steps to Reproduce

  1. git clone https://github.com/UlyssesWu/XamarinPosed.Core/tree/vs_CS0246 (do not clone the main branch because it works)
  2. Open sln with VS, build the solution, it can be built without error
  3. Navigate to XamarinPosed\Loader.cs, put your mouse on IXposedHookLoadPackage, XposedBridge.Log, or just check the error list, there are CS0246 and CS0103, and there are no auto-complete. But still, the project built without error

Did you find any workaround?

Do not reference the binding project directly, but reference the built binding dll instead. Check out the main branch of my repo.

Relevant log output

No response

jpobst commented 11 months ago

@jonathanpeppers Is there a way to do the equivalent of @(InputJar) using <ProjectReference>?

If I do <AndroidLibrary Include="auto-value-annotations-1.10.4.jar" Pack="false" /> in a binding project, and then reference that project in my app project using <ProjectReference>, the .jar appears to be picked up and compiled into the app:

image

jonathanpeppers commented 11 months ago

This would probably work:

<None Remove="auto-value-annotations-1.10.4.jar" />

https://github.com/xamarin/xamarin-android/blob/3f2b090f550d5c1fa8d63b309a64f9df05698768/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets#L105-L108

I remember thinking that @(InputJar) was somewhat useless, and so we don't have an easy replacement. We could maybe make it respect %(CopyToOutputDirectory).

jpobst commented 11 months ago

I think an issue is that it's not explicitly %(CopyToOutputDirectory), the .jar gets placed in a <project_name>.aar in the output dir:

image

Maybe we just need to prevent it from ending up in there?

jpobst commented 11 months ago

Hmm, I guess Pack=false doesn't work for NuGet either, it also includes the binding.aar file containing the .jar:

image

jonathanpeppers commented 11 months ago

Does the default wildcard pick this up, so you'd need to do Update?

<AndroidLibrary Update="auto-value-annotations-1.10.4.jar" Pack="false" />
jpobst commented 11 months ago

My test uses <EnableDefaultItems>false</EnableDefaultItems> to avoid this issue.

jonathanpeppers commented 11 months ago

It seems one of the issues is this loop should probably continue; if %(Pack) is false:

https://github.com/xamarin/xamarin-android/blob/3f2b090f550d5c1fa8d63b309a64f9df05698768/src/Xamarin.Android.Build.Tasks/Tasks/CreateAar.cs#L90