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

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

Closed
xpfr opened this issue May 28, 2023 · 7 comments · Fixed by #8088
Closed

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

xpfr opened this issue May 28, 2023 · 7 comments · Fixed by #8088
Assignees
Labels
Area: Mono.Android Issues with the Android API binding (Mono.Android.dll).
Milestone

Comments

@xpfr
Copy link

xpfr commented May 28, 2023

          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.

@ghost ghost added the needs-triage Issues that need to be assigned. label May 28, 2023
@xpfr
Copy link
Author

xpfr commented May 28, 2023

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

@jpobst
Copy link
Contributor

jpobst commented May 29, 2023

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

@jpobst jpobst added Area: Mono.Android Issues with the Android API binding (Mono.Android.dll). and removed needs-triage Issues that need to be assigned. labels May 29, 2023
@jpobst jpobst added this to the .NET 8 milestone May 29, 2023
@jpobst jpobst self-assigned this May 29, 2023
@jpobst jpobst changed the title Bind library that uses Java 8+ API desugaring support - not fixed java.time package not bound in Mono.Android May 29, 2023
@Kikanye
Copy link

Kikanye commented Jun 23, 2023

@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.

@jpobst
Copy link
Contributor

jpobst commented Jun 23, 2023

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);
}

@Kikanye
Copy link

Kikanye commented Jun 23, 2023

@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! :)

@jpobst
Copy link
Contributor

jpobst commented Jun 23, 2023

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

@Kikanye
Copy link

Kikanye commented Jun 23, 2023

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.

jonpryor pushed a commit that referenced this issue Aug 10, 2023
)

Fixes: #8081

Context: 571546a

When binding API-26 we chose not to bind the `java.time` and
`java.time.chrono` Android packages (571546a), likely due to the
amount of metadata required to build them correctly.
(It also requires the use of `managedOverride`, which didn't exist
at the time.)

However, they are pretty useful packages, so let's invest the effort
to get them bound.

Additionally, this results in now producing bindings for many methods
in unrelated types that consume `java.time` types.
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: Mono.Android Issues with the Android API binding (Mono.Android.dll).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants