MAUI app cannot resolve androidx.compose.ui.platform.ComposeView from Android Binding Library
Android framework version
net9.0-android
Affected platform version
VS Enterprise 2022 17.12.4, .NET 9.0.102,
Description
I have kotlin code that needs androidx.compose.ui to work. I build an android library from this kotlin code, then import it in an Android Binding Library in VS, and I add the dependencies in the .csproj, with notably
<PackageReference Include="Xamarin.AndroidX.Compose.UI.Android" Version="1.7.6.1" />
among a lot of other packages
I then import this library in a MAUI application, and when launching a ComponentActivity, there is a runtime error
Java.Lang.NoClassDefFoundError: 'Failed resolution of: Landroidx/compose/ui/platform/ComposeView;'
see also https://stackoverflow.com/questions/79421004/maui-java-class-resolution-fails-even-with-dependencie-added
Steps to Reproduce
- create a new android studio project
- add an android library module
- create a custom class extending ComponentActivity()
- in setContentView, add a ComposeView
- add a composable in the content
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")
}
}
}
)
}
}
- build the library
- create a new Android Binding Library in VS
- add the .AAR
- add all xamarin.androix dependencies
<PackageReference Include="Xamarin.AndroidX.Activity" Version="1.10.0" />
<PackageReference Include="Xamarin.AndroidX.Activity.Compose" Version="1.10.0" />
<PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.10.0" />
<PackageReference Include="Xamarin.AndroidX.Annotation" Version="1.9.1.2" />
<PackageReference Include="Xamarin.AndroidX.Annotation.Experimental" Version="1.4.1.8" />
<PackageReference Include="Xamarin.AndroidX.Annotation.Jvm" Version="1.9.1.2" />
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.7.0.5" />
<PackageReference Include="Xamarin.AndroidX.Collection.Jvm" Version="1.4.5.2" />
<PackageReference Include="Xamarin.AndroidX.Compose.Foundation" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.Material3" Version="1.3.1.2" />
<PackageReference Include="Xamarin.AndroidX.Compose.Material3Android" Version="1.3.1.2" />
<PackageReference Include="Xamarin.AndroidX.Compose.Runtime" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.Runtime.Android" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.Runtime.LiveData" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.Runtime.RxJava2" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.Runtime.RxJava3" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.Runtime.Saveable" Version="1.7.6.1" />
<PackageReference Include="Xamarin.AndroidX.Compose.Runtime.Saveable.Android" Version="1.7.6.1" />
<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" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.Runtime.Compose" Version="2.8.7.2" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.Runtime.Compose.Android" Version="2.8.7.2" />
<PackageReference Include="Xamarin.Jetbrains.Annotations" Version="26.0.1.2" />
<PackageReference Include="Xamarin.Kotlin.StdLib" Version="2.0.21.2" />
<PackageReference Include="Xamarin.Kotlin.StdLib.Common" Version="2.0.21.2" />
<PackageReference Include="Xamarin.Kotlin.StdLib.Jdk7" Version="2.0.21.2" />
<PackageReference Include="Xamarin.Kotlin.StdLib.Jdk8" Version="2.0.21.2" />
<PackageReference Include="Xamarin.KotlinX.Coroutines.Android" Version="1.9.0.2" />
<PackageReference Include="Xamarin.KotlinX.Coroutines.Core" Version="1.9.0.2" />
<PackageReference Include="Xamarin.KotlinX.Coroutines.Core.Jvm" Version="1.9.0.2" />
<PackageReference Include="Xamarin.AndroidX.Compose.UI" Version="1.7.6.1" />
- build the library
- create a new MAUI application
- add a reference to the Binding Library
- launch the custom activity, with for example a button
public static void StartComposeActivity(Context context, string param)
{
var intent = new Intent(
context,
typeof(Com.Example.Composebind.ExampleActivity));
//intent.PutExtra("param", param);
context.StartActivity(intent);
}
and
private void OpenCompose_Clicked(object sender, EventArgs e)
{
#if ANDROID
if (Platform.CurrentActivity is MainActivity mainActivity)
{
MainActivity.StartComposeActivity(mainActivity, "test");
}
#endif
}
and
<Button
Text="Open Compose"
Clicked="OpenCompose_Clicked"/>
- admire the crash
Did you find any workaround?
no :(
Relevant log output
@TheFoxan12 Thanks for the feedback
AndroidX.Compose is not officialy supported in .NET Android. Thus not supported in MAUI too.
please see
https://github.com/dotnet/android-libraries/issues/984#issuecomment-2379804524
OK. I am sorry - did not read everything and carefully.
So you have
- Kotlin library using Compose
- C# bindings library that binds #1
- MAUI app using #2
Is that right?
I need to check bindings, but did you check proguard?
Checked:
yup. Back then (in 2021) I didn't have time to tackle huge amount of errors so I removed managed code
https://github.com/dotnet/android-libraries/blob/main/source/androidx.compose.ui/ui-android/Transforms/Metadata.xml
Hey, thank you for answering.
I didn't really understand your answer with the links even if you seem to have understood, I will summarize it again clearly:
I have
- Kotlin library using Compose
- Android Binding Library, in C#, binding the kotlin library, and referencing the package Xamarin.Androidx.Compose.UI
- MAUI app using the Android Binding Library
I'm pretty beginner in Kotlin and Android libs, so what do you mean by checking proguard ? Assuming you're talking about proguard rules in my android library, what should I look for ?
Regarding your last check, what does it mean ? xd Will I be able to use Compose ? Or will it really be impossible ? Is there maybe a workaround I could use ?
I didn't really understand your answer with the links even if you seem to have understood, I will summarize it again clearly:
I have
- Kotlin library using Compose
- Android Binding Library, in C#, binding the kotlin library, and referencing the package Xamarin.Androidx.Compose.UI
- MAUI app using the Android Binding Library
OK. I got it right.
I'm pretty beginner in Kotlin and Android libs, so what do you mean by checking proguard ? Assuming you're talking about proguard rules in my android library, what should I look for ?
Today it is R8. proguard is predecessor of R8. It is optimizer.
https://developer.android.com/build/shrink-code
Regarding your last check, what does it mean ? xd
I will have to redo the bindings.
Will I be able to use Compose ?
I hope so.
Or will it really be impossible ?
Nothing is impossible, just limited right now.
Is there maybe a workaround I could use ?
Not that I know of right now.
Thanks, So, do you think it will be available soon ? Like a few weeks ? Or maybe a few month ?
Thanks, So, do you think it will be available soon ? Like a few weeks ? Or maybe a few month ?
Hard to tell. I need to check 1st. I would say - weeks.
@TheFoxan12
WIP
https://github.com/dotnet/android-libraries/pull/1094
There are few subborn artifacts with lot of errors, but I think it will be few weeks (this is not our highest pirority)
In the meantime would you be so kind and create minimal repro sample, so I can test against something, please?
Thanks for working on it,
I'll create a sample project as soon as I can.
I am also happy to inform you that I found and alternative temporary solution, which is adding every dependencie file in .aar/.jar downloaded directly from the maven repository of androidx, in the android binding library But having nuget packages would be really better for maintenability