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 527 forks source link

WearOS app fails to compile when targeting net6.0-android #7003

Closed beeradmoore closed 2 years ago

beeradmoore commented 2 years ago

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

VS4Mac 2022 Preview (17.0 build 8904), .NET 6.0.300, Xamarin.Android 12.3.0.3

Description

I have been trying to use net6.0-android as the target framework to build a WearOS wearable app after it was mentioned in an (unofficial) .NET MAUI Platforms Support gist that it was supported.

The warnings below seem to indicate android-31/optional/com.google.android.wearable.jar is missing when in actual fact com.google.android.wearable.jar does not exist in android-30/optional/ and android-28/optional/ which are the other folders I have there.

I am unsure if those warnings relate to the R8 compilation or not.

Repro: WearOSMonoTest.zip WearOSNet6Test.zip

Extra info: I did update target framework on the first project to MonoAndroid12.0 I also updated TargetFramework to net6.0-android31 because that is what was listed as the required framework on nuget.org for the nugets I added but I don't believe this is required as it has the same issues if I use net6.0-android.

Steps to Reproduce

  1. Create new wear app using VS4Mac stable (in this case I called it WearOSMonoTest)
  2. Update all nuget files.
  3. Build and deploy app to confirm it is working.
  4. (see WearOSMonoTest.zip above)
  5. Create new net6.0-android app, dotnet new android --name WearOSNet6Test
  6. Update nugets/resources/code to match the WearOSMonoTest project.
  7. Build and deploy app to see .NET 6 in a WearOS app.
  8. Get compile issues (listed below)
  9. (see WearOSNet6Test.zip above)

Did you find any workaround?

Nope.

Relevant log output

obj/Debug/net6.0-android31/android/AndroidManifest.xml : warning XA4218: Unable to find //manifest/application/uses-library at path: /Users/bradmoore/Library/Developer/Xamarin/android-sdk-macosx/platforms/android-31/optional/com.google.android.wearable.jar
obj/Debug/net6.0-android31/android/AndroidManifest.xml : warning XA4218: Unable to find //manifest/application/uses-library at path: /Users/bradmoore/Library/Developer/Xamarin/android-sdk-macosx/platforms/android-31/optional/androidx.window.extensions.jar
obj/Debug/net6.0-android31/android/AndroidManifest.xml : warning XA4218: Unable to find //manifest/application/uses-library at path: /Users/bradmoore/Library/Developer/Xamarin/android-sdk-macosx/platforms/android-31/optional/androidx.window.sidecar.jar
/usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/32.0.300-rc.3.45/tools/Xamarin.Android.D8.targets(79,5): error : java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: obj/Debug/net6.0-android31/lp/59/jl/classes.jar : android/support/v4/app/INotificationSideChannel$Stub.class
vhugogarcia commented 2 years ago

Hello @beeradmoore , I think I found a temporary workaround to develop and run Android Wear OS while the official fix is provided.

I'm on using .NET 6.0.300, Visual Studio for Mac RC2 and MAUI RC3

  1. Created a new MAUI application
  2. Added a new Android Wear App to the solution
  3. Updated the packages that were added automatically by the VS tooling (wizard).
  4. Then added some missing packages (I found) related to Wear support (use those exact versions) see screenshot here
  5. Close Visual Studio for Mac and re-open it again (I'm not sure why but it appears to work)
  6. Rebuild the project it may compile or maybe throw some errors like Error: java.nio.file.NoSuchFileException: /Users/demo/Personal/demo/App/src/demo/Acceso.Wear/Acceso.Wear/obj/Debug/lp/48/jl/res/values/values.xml just ignore them and even with errors, just click on RUN.

Note: I created a Wear OS emulator using Android 11, it is the one you can see on the screenshot I shared above. Note 2: When closing and opening VS for Mac, I get this error on package console, just for your information:

Detected package version outside of dependency constraint: Xamarin.Android.Support.Percent 26.1.0.1 requires Xamarin.Android.Support.Compat (= 26.1.0.1) but version Xamarin.Android.Support.Compat 28.0.0.3 was resolved.

I hope this helps with some insights to solve the root cause of the issue with the tooling on VS for Mac or the update for nugets packages officially from Microsoft to support WearOS.

beeradmoore commented 2 years ago

Oh, neato! Followed your directions and I found a few things.

Unsure if this is what the java.nio.file.NoSuchFileException warning you were getting is relating to but I found adding xml files the build type was set to none by default and not AndroidResource.

The

Xamarin.Android.Support.Percent 26.1.0.1 requires Xamarin.Android.Support.Compat (= 26.1.0.1)

warning appears to be because Xamarin.Android.Wear has it as a dependency like that. Screen Shot 2022-05-17 at 9 43 13 am

At this point I have not installed Xamarin.AndroidX.ConstraintLayout or Xamarin.AndroidX.Lifecycle.LiveData. But now get different errors building.

/Users/bradmoore/.nuget/packages/xamarin.android.support.annotations/28.0.0.3/build/monoandroid90/Xamarin.Android.Support.Annotations.targets(20,3): error MSB4062: The "Xamarin.Android.Support.BuildTasks.VerifyVersionsTask" task could not be loaded from the assembly /Users/bradmoore/.nuget/packages/xamarin.android.support.annotations/28.0.0.3/build/monoandroid90/Xamarin.Android.Support.BuildTasks.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
/Users/bradmoore/.nuget/packages/xamarin.android.support.annotations/28.0.0.3/build/monoandroid90/Xamarin.Android.Support.Annotations.targets(20,3): error MSB4062:  Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

VS gave me the option to enable building MSBuild on Mono, and a link to this site for more information.

But even enabling that I don't get past this error. Even attempted restarting VS but it was a big fat nope.

Here is my current project without Xamarin.AndroidX.ConstraintLayout and Xamarin.AndroidX.Lifecycle.LiveData (although adding them didn't change this error)

WearOSNet6Test-v1.1.zip

vhugogarcia commented 2 years ago

Hello @beeradmoore , I was able to simplify the packages dependencies and compile without any errors and no more warnings on the packages.

Please find below the project zipped and a screenshot of the configuration. One thing, I forgot to mention on my previous comment was that I had to increase the Minimum Android Version Target to 6.0 API level 23.

Acceso.Wear.zip

Screen Shot 2022-05-17 at 7 40 41

However, I believe, I have been using the MonoCompiler instead of the MS Build new project structure, because I noticed that your .csproj is different from the one the VS for Mac 2022 RC2 created automatically for me.

vhugogarcia commented 2 years ago

Hello again @beeradmoore, After being in touch with the community on Discord, we were able to solve the issue.

The following project has been updated by removing the old dependencies and just leave the AndroidX ones. Also updated the MainActivity.cs to reflect the new APIs provided on AndroidX.

@maddymontaquila do you think the MS can give us a hand to update the VS for Mac and Windows 2022 tooling for creating Android WearOS Apps for .NET 6 new csproj styles and also include the new packages and MainActivity.cs layout, please.

Reminder: Set the Minimum Android Version Target to 6.0 API level 23 (it never compiled on version 21 or 22)

WearOSNet6Test.zip

Screen Shot 2022-05-17 at 8 32 24
jonpryor commented 2 years ago

See also:

The big takeaways:

  1. Stop using the old Xamarin.Android.Support packages, and all other Xamarin.Android.Support packages for that matter.
  2. WearableActivity is dead, just use Activity and the new AmbientModeSupport fragment

The really big takeaway: we need to update the Wear template in VSMac.

beeradmoore commented 2 years ago

@vhugogarcia awesome job! I was able to replicate the steps you had done (and with what @jonpryor had said) but to my own project and got it working. With my new project and the two you posted above I still have the same warnings I had before but everything still works. Unsure if I am missing something from being installed.

obj/Debug/net6.0-android31/android/AndroidManifest.xml : warning XA4218: Unable to find //manifest/application/uses-library at path: /Users/bradmoore/Library/Developer/Xamarin/android-sdk-macosx/platforms/android-31/optional/com.google.android.wearable.jar
obj/Debug/net6.0-android31/android/AndroidManifest.xml : warning XA4218: Unable to find //manifest/application/uses-library at path: /Users/bradmoore/Library/Developer/Xamarin/android-sdk-macosx/platforms/android-31/optional/androidx.window.extensions.jar
obj/Debug/net6.0-android31/android/AndroidManifest.xml : warning XA4218: Unable to find //manifest/application/uses-library at path: /Users/bradmoore/Library/Developer/Xamarin/android-sdk-macosx/platforms/android-31/optional/androidx.window.sidecar.jar

@jonpryor what server are those discord channels on? When I try go to them it just says no text channel here, probably because I am not on that server.

What's the process for submitting/requesting new templates for VS4Mac (and VS for Windows as well I guess) so we can get those updated?

jonathanpeppers commented 2 years ago

In .NET 6, template live inside optional workloads, and what was offer right now is here:

https://github.com/xamarin/xamarin-android/tree/main/src/Microsoft.Android.Templates

Was the mentioned template above for Xamarin.Android?

We could add an android-wear template for .NET 6. Is this common enough to be needed? If someone is really interested in this, send a PR, thanks!

dellis1972 commented 2 years ago

We could add an android-wear template for .NET 6. Is this common enough to be needed? If someone is really interested in this, send a PR, thanks!

I think they created an old Wear template then converted it. So we should probably do one for .NET 6.

beeradmoore commented 2 years ago

The ones I was making was with 'dotnet new android' and the copying over things from one templates in VS4mac 2019 to make files match (eg. dimens.xml, activity_main.xml)

EDIT: @jonathanpeppers , do you know if workload templates show up in Visual Studio on both Windows and Mac (or at least the preview versions of them both)?

jonathanpeppers commented 2 years ago

The templates in VS Mac do not yet support finding templates inside .NET optional workloads -- when that feature is available, it should display the latest .NET 6+ or MAUI templates.

So I think we are good to close this? Anyone looking to target Android Wear should try the template in #7013 thanks!