Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAUI app cannot resolve androidx.compose.ui.platform.ComposeView from Android Binding Library #1090

Open
TheFoxan12 opened this issue Feb 7, 2025 · 8 comments · May be fixed by #1094
Open
Assignees
Labels
binding-issue Java error when attempting to use bound type/method missing-api Java types/members not available in binding

Comments

@TheFoxan12
Copy link

TheFoxan12 commented Feb 7, 2025

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

  1. create a new android studio project
  2. add an android library module
  3. create a custom class extending ComponentActivity()
  4. in setContentView, add a ComposeView
  5. 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")
                    }
                }
            }
        )
    }
}
  1. build the library
  2. create a new Android Binding Library in VS
  3. add the .AAR
  4. 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" />
  1. build the library
  2. create a new MAUI application
  3. add a reference to the Binding Library
  4. 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"/>
  1. admire the crash
Image

Did you find any workaround?

no :(

Relevant log output

@moljac
Copy link
Contributor

moljac commented Feb 9, 2025

@TheFoxan12
Thanks for the feedback

AndroidX.Compose is not officialy supported in .NET Android. Thus not supported in MAUI too.

please see

#984 (comment)

@moljac moljac added the wontfix This will not be worked on label Feb 9, 2025
@moljac
Copy link
Contributor

moljac commented Feb 9, 2025

OK. I am sorry - did not read everything and carefully.

So you have

  1. Kotlin library using Compose
  2. C# bindings library that binds Update the templates in .github #1
  3. MAUI app using Camera X Bindings #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

@moljac moljac added missing-api Java types/members not available in binding binding-issue Java error when attempting to use bound type/method and removed wontfix This will not be worked on labels Feb 9, 2025
@moljac moljac self-assigned this Feb 9, 2025
@TheFoxan12
Copy link
Author

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

  1. Kotlin library using Compose
  2. Android Binding Library, in C#, binding the kotlin library, and referencing the package Xamarin.Androidx.Compose.UI
  3. 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 ?

@moljac
Copy link
Contributor

moljac commented Feb 9, 2025

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

  1. Kotlin library using Compose
  2. Android Binding Library, in C#, binding the kotlin library, and referencing the package Xamarin.Androidx.Compose.UI
  3. 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.

@TheFoxan12
Copy link
Author

TheFoxan12 commented Feb 9, 2025

Thanks,
So, do you think it will be available soon ? Like a few weeks ? Or maybe a few month ?

@moljac
Copy link
Contributor

moljac commented Feb 11, 2025

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.

@moljac moljac linked a pull request Feb 12, 2025 that will close this issue
@moljac
Copy link
Contributor

moljac commented Feb 12, 2025

@TheFoxan12

WIP

#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?

@TheFoxan12
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
binding-issue Java error when attempting to use bound type/method missing-api Java types/members not available in binding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants