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.89k stars 524 forks source link

Trying to bind an AppCompatActivity derived Activity results in errors #9049

Closed MihaMarkic closed 1 day ago

MihaMarkic commented 1 week ago

Android framework version

net8.0-android

Affected platform version

VS 2022 17.10.3 SDK 8.0.302

Description

I have this Kotlin guy in android studio module:

class TestActivity: AppCompatActivity() {
}

which I'm trying to bind. I've created the .aar, .net android library project, added a ton of packages and when compiling, I get these errors:

Reference to type 'IHasDefaultViewModelProviderFactory' claims it is defined in 'Xamarin.AndroidX.Lifecycle.ViewModel', but it could not be found
 error CS7069: Reference to type 'IViewModelStoreOwner' claims it is defined in 'Xamarin.AndroidX.Lifecycle.ViewModel', but it could not be found
error CS7069: Reference to type 'ILifecycleOwner' claims it is defined in 'Xamarin.AndroidX.Lifecycle.Common', but it could not be found

in line

_members.InstanceMethods.FinishCreateInstance (__id, this, null);

of generated constructor.

I have these NuGet libraries configured in .csproj

<PackageReference Include="Xamarin.AndroidX.Lifecycle.Common" Version="2.8.2" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData.Core" Version="2.8.2" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData.Core.Ktx" Version="2.8.2" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.ViewModel" Version="2.8.2" />
<PackageReference Include="Xamarin.Kotlin.StdLib" Version="2.0.0" />
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.7.0" />
<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.3.3.3" />
<PackageReference Include="Xamarin.AndroidX.Camera.Lifecycle" Version="1.3.3.3" />
<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.3.3.3" />
<PackageReference Include="Xamarin.AndroidX.ConstraintLayout" Version="2.1.4.13" />
<PackageReference Include="Xamarin.Google.MLKit.BarcodeScanning" Version="117.2.0.6" />
<PackageReference Include="Xamarin.GooglePlayServices.Tasks" Version="118.1.0.2" />

Basically it's a vanilla AppCompatActivity subtype I wish to bind.

Steps to Reproduce

Will post a sample project if required

Did you find any workaround?

No

Relevant log output

Reference to type 'IHasDefaultViewModelProviderFactory' claims it is defined in 'Xamarin.AndroidX.Lifecycle.ViewModel', but it could not be found
 error CS7069: Reference to type 'IViewModelStoreOwner' claims it is defined in 'Xamarin.AndroidX.Lifecycle.ViewModel', but it could not be found
error CS7069: Reference to type 'ILifecycleOwner' claims it is defined in 'Xamarin.AndroidX.Lifecycle.Common', but it could not be found
jpobst commented 1 week ago

It looks like Google moved all the types from these libraries to new libraries in 2.8.*:

Are you getting the errors from the binding project? Or trying to consume the binding?

Maybe the binding project and the application project are referencing different versions of these packages?

MihaMarkic commented 1 week ago

I'm getting errors in binding project, when it tries to compile. Tried replacing those libraries, but the end result is the same. Android module uses the former ones though. Let me create a sample for you.

MihaMarkic commented 1 week ago

Here is a sample consisting of Android Studio project (app and library modules) and VS project trying to bind the library module output (included in archive). Versions seems ok at first glance, but I could be missing something obvious.

BindingAppCompat.zip

jpobst commented 1 week ago

I think the actual issue is in the warnings:

warning NU1608: Detected package version outside of dependency constraint: Xamarin.AndroidX.Lifecycle.Process 2.7.0.4 requires Xamarin.AndroidX.Lifecycle.Runtime (>= 2.7.0.4 && < 2.7.1) but version Xamarin.AndroidX.Lifecycle.Runtime 2.8.2 was resolved.
warning NU1608: Detected package version outside of dependency constraint: Xamarin.AndroidX.Lifecycle.LiveData 2.7.0.4 requires Xamarin.AndroidX.Lifecycle.LiveData.Core.Ktx (>= 2.7.0.4 && < 2.7.1) but version Xamarin.AndroidX.Lifecycle.LiveData.Core.Ktx 2.8.2 was resolved.
warning NU1608: Detected package version outside of dependency constraint: Xamarin.AndroidX.Lifecycle.LiveData 2.7.0.4 requires Xamarin.AndroidX.Lifecycle.LiveData.Core (>= 2.7.0.4 && < 2.7.1) but version Xamarin.AndroidX.Lifecycle.LiveData.Core 2.8.2 was resolved.
warning NU1608: Detected package version outside of dependency constraint: Xamarin.AndroidX.Lifecycle.Process 2.7.0.4 requires Xamarin.AndroidX.Lifecycle.Runtime (>= 2.7.0.4 && < 2.7.1) but version Xamarin.AndroidX.Lifecycle.Runtime 2.8.2 was resolved.
warning NU1608: Detected package version outside of dependency constraint: Xamarin.AndroidX.Lifecycle.LiveData 2.7.0.4 requires Xamarin.AndroidX.Lifecycle.LiveData.Core.Ktx (>= 2.7.0.4 && < 2.7.1) but version Xamarin.AndroidX.Lifecycle.LiveData.Core.Ktx 2.8.2 was resolved.
warning NU1608: Detected package version outside of dependency constraint: Xamarin.AndroidX.Lifecycle.LiveData 2.7.0.4 requires Xamarin.AndroidX.Lifecycle.LiveData.Core (>= 2.7.0.4 && < 2.7.1) but version Xamarin.AndroidX.Lifecycle.LiveData.Core 2.8.2 was resolved.

Some of these packages are expecting the 2.7.* versions where the types are expected in one assembly, and some of them are expecting the 2.8.* versions where the types are expected in the other assembly.

There in theory exists a complete set of packages that all rely on the 2.7.* types, and I thought NuGet would help guide you to a consistent set of packages, but it does not seem to be doing that.

On our end, I think we can publish packages with [TypeForward] attributes that should help the compiler find the moved types. I have kicked this off in https://github.com/xamarin/AndroidX/pull/908.

MihaMarkic commented 1 week ago

Cool, realistically, when should I see the changes applied? Also, if I downgrade libraries in Android studio to 2.7, it might work as well, right (not behind the computer)?