javakotlinmauiandroidxmobile-development

MAUI Java class resolution fails even with dependencie added


I want to launch from dotnet maui an Android activity implemented in Kotlin, that uses Jetpack Compose elements.

For that, I have my Kotlin class

open class ExampleActivity : ComponentActivity() {

    @Composable
    fun Greeting(name: String) {
        Text(text = "Hello $name!")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(
            ComposeView(this).apply {
                setContent {
                    MaterialTheme {
                        Greeting(name = "compose")
                    }
                }
            }
        )
    }
}

Then I built an Android Library from it, and I put the .AAR in an Android Binding Library project in Visual Studio. I then added dependencies for androidx, and compose stuff, and notably the one causing problems, androidx.compose.ui:

...
<PackageReference Include="Xamarin.AndroidX.Activity.Compose" Version="1.10.0" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Android" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Geometry" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Geometry.Android" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Graphics" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Graphics.Android" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Text" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Text.Android" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Unit" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Unit.Android" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Util" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Util.Android" Version="1.7.6.1" />
...

Building the Android Binding Library gives small warnings:

The method '[Method] void Greeting(java.lang.String name, androidx.compose.runtime.Composer $composer, int $changed)' was removed because the Java parameter type 'androidx.compose.runtime.Composer' could not be found.
The field '[Field] com.example.ExampleActivity.$stable' was removed because its name contains a dollar sign.

But doesn't block the build and the result is usable in a MAUI app:

public static void StartComposeActivity(Context context, string param)
{
    var intent = new Intent(
        context,
        typeof(Com.Example.ExampleActivity));
    //intent.PutExtra("param", param);

    Android.Util.Log.Debug("DBG", "----------------------------");
    Android.Util.Log.Debug("DBG", "Launching intent");

    context.StartActivity(intent);
}

But when clicking on the button I binded the launch to, the activity launches but results an error:

Java.Lang.NoClassDefFoundError: 'Failed resolution of: Landroidx/compose/ui/platform/ComposeView;'

But in my Kotlin code:

import androidx.compose.ui.platform.ComposeView

ComposeView comes from androidx.compose.ui.platform, and according to Android Studio the gradle dependencie reference for androidx.compose.ui.platform is androidx.compose.ui:ui-android.

Then, Xamarin.Androidx.Compose.UI.Android should contain this package.
But it is not the case.
And androidx.compose.ui.platform seems to be inexistant, and seems to be an alias for .android

I've also tried to add myself .aar and .jar copies from the first build into the Android Binding Library, but the result is worse then with nuget packages.

Does anyone know why the application, at runtime, is not able to find ComposeView anywhere ?


Solution

  • For anyone wondering, the answer is that "Xamarin.Androidx.Compose.UI" doesn't implement bindings for what it is supposed to, at the moment, so it's absolutely normal that dependencies can't be found.

    See also: https://github.com/dotnet/android-libraries/issues/1090#issuecomment-2646201588