Skip to content

java.time package not bound in Mono.Android #8081

Closed

Description

@xpfr
          Since https://github.com/xamarin/XamarinComponents/pull/1180 got merged, I think this can now be closed.

Originally posted by @4brunu in #5523 (comment)

Still facing the same issue.

Activity

  1. ghost added
    needs-triageIssues that need to be assigned.
    on May 28, 2023
  2. xpfr commented on May 28, 2023

    @xpfr
    Author

    To reproduce:

    Create a binding library with TargetFramework=net6-android over a .aar having desugaring support (https://developer.android.com/studio/write/java8-support) using:

    "Microsoft Visual Studio Community 2022
    Version 17.6.2
    VisualStudio.17.Release/17.6.2+33723.286
    Microsoft .NET Framework
    Version 4.8.09032
    Gestionnaire de package NuGet 6.6.0
    Xamarin 17.6.0.251 (d17-6@318364c)
    Xamarin Designer 17.6.6.0 (remotes/origin/d17-6@cb430751d1)
    Xamarin Templates 17.6.32 (98c12a2)
    Xamarin.Android SDK 13.2.0.6 (d17-5/a200af1)
    Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: 6dd9def
    Java.Interop: xamarin/java.interop/d17-5@149d70fe
    SQLite: xamarin/sqlite@68c69d8
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@9f02d77"

    Building the binding library gives below error:

    warning BG8605: The Java type 'java.time.LocalDate' could not be found (are you missing a Java reference jar/aar or a Java binding library NuGet?)
    Related methods are then removed from the bindings:
    The method '[Method] <package>.CalendarDay copy(java.time.LocalDate date, com.kizitonwose.calendar.core.DayPosition position)' was removed because the Java parameter type 'java.time.LocalDate' could not be found.
    

    Tried a few things without success:

    Thank you for the support

  3. jpobst commented on May 29, 2023

    @jpobst
    Contributor

    The java.time package is not bound, and thus no API that relies on it can be bound:
    https://github.com/xamarin/xamarin-android/blob/main/src/Mono.Android/metadata#L1340-L1342

    The commit from 6 years ago doesn't say why it wasn't bound. I guess we could try it and see what the issues are.

    Note that this issue is:

    • Unrelated to Java desugaring
    • Just a warning, it removes the API that cannot be bound, but does not prevent the rest of the library from being bound
  4. added
    Area: Mono.AndroidIssues with the Android API binding (Mono.Android.dll).
    and removed
    needs-triageIssues that need to be assigned.
    on May 29, 2023
  5. added this to the .NET 8 milestone on May 29, 2023
  6. self-assigned this
    on May 29, 2023
  7. changed the title [-]Bind library that uses Java 8+ API desugaring support - not fixed[/-] [+]`java.time` package not bound in `Mono.Android`[/+] on May 29, 2023
  8. Kikanye commented on Jun 23, 2023

    @Kikanye

    @jpobst I'm facing a similar issue, I wonder if there is a work around for this for the time being? Is there some way to bind methods that take Instant or LocalDateTime (which both come from java.time) as arguments? In my case these API's are necessary for use of the library.

  9. jpobst commented on Jun 23, 2023

    @jpobst
    Contributor

    I think the only thing you can do is write a new Java API that wraps the existing API without exposing Instant or LocalDateTime, and then bind that API.

    Something like:

    public void doThing (string localDateTime)
    {
      var dt = LocalDateTime.parse (localDateTime);
      doRealThing (dt);
    }
  10. Kikanye commented on Jun 23, 2023

    @Kikanye

    @jpobst Thanks for your response on this, I was thinking something similar, but I was hoping I would not have to rewite the Java API. Do you know of a way I can add a method to the Additions that will do something like this, but on the C# level.

    So something like:

    pubic void doThing(string localDateTime)
    {
        // Make a call to LocalDateTime.parse here, but do it with C# (not have to write Java Code)
        var dt = LocalDateTime.parse(string)
        
        // Make a call to Java code here to do the real thing but from C#
        JavaCall.doRealThing(dt);
    }
    
    

    I don't know if the above makes sense? Do you think this is possible?

    I don't have access to edit the java source code for the AAR I am trying to bind, that's why I'd prefer a C# based solution.

    Let me know what you think, and thanks for your respose! :)

  11. jpobst commented on Jun 23, 2023

    @jpobst
    Contributor

    There is no way to do it in C#, unless you try to bind it by hand yourself. (I can't help with that.)

    If you are using .NET 7+, you can add a loose .java file to your application that wraps the API as a AndroidJavaSource file and it will automatically get compiled and bound:

    https://learn.microsoft.com/en-us/xamarin/android/deploy-test/building-apps/build-items#androidjavasource

    Note that it will not show up in Intellisense which is quite annoying, but it does work when you run your application:
    #7692

  12. Kikanye commented on Jun 23, 2023

    @Kikanye

    Thanks for the response @jpobst, I'm trying to do this now but it's not too clear to he how. Would I add the java file to my Xamarin Android Binding project? Or would I add it to the Xamarin-Android application?

    If there is a way to do this is in the Android Binding project, do I just add the java file to the Additions folder in that project? it doesn't seem too clear to me what the best way to do it.

  13. locked and limited conversation to collaborators on Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: Mono.AndroidIssues with the Android API binding (Mono.Android.dll).

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions