Skip to content

Conversation

@eamonnmcmanus
Copy link
Member

This is Android
8.0
, released in August 2017. Only about 2% of devices have an older version.

Requiring API Level 26 means we can use java.time APIs without reflection.

This is [Android
8.0](https://developer.android.com/tools/releases/platforms#8.0),
released in August 2017. Only about 2% of devices have an older version.
@MukjepScarlet
Copy link
Contributor

This also means we can add Optional adapters into standard. Crazy.

@eamonnmcmanus
Copy link
Member Author

This also means we can add Optional adapters into standard. Crazy.

Yes, that's a good point. Similar compatibility considerations to the java.time classes would probably lead us to copy what the (terrible) reflection-based adapter does currently, so Optional.of("foo") becomes {"value":"foo"} and Optional.empty() becomes {}, or {"value":null} if serializeNulls() is set. I think probably putting in the null value regardless of serializeNulls() would make sense. Plain {} is pretty cryptic.

Similarly OptionalInt would become {"isPresent":true,"value":23} or {"isPresent":false}. The current serialization includes "value":0 for the empty case but I don't think that matters.

This is probably not the serialization we would have chosen if there were no compatibility considerations. People can always register their own adapters (and probably often do already). I found several in Google's codebase, more than one of which serialized Optional.of(foo) the same as foo, with Optional.empty() being serialized as null.

@Marcono1234
Copy link
Contributor

Marcono1234 commented Nov 25, 2025

Can you please adjust the README as well:

gson/README.md

Lines 71 to 74 in a271f48

#### Minimum Android API level
- Gson 2.11.0 and newer: API level 21
- Gson 2.10.1 and older: API level 19

There are also (at least) these two places where the code could be improved now:

Or if you prefer I can do that in a separate PR.

@cpovirk
Copy link
Member

cpovirk commented Nov 25, 2025

I ended up leaving a comment about this on a separate PR. Based on what you've subsequently said elsewhere, I think we may end up having to maintain three sorts of compatibility:

  • Gson works under minSdkVersion x.
    • In theory, we could have different values for x for users who enable library desugaring than for users who don't.
  • Animal Sniffer's checking against minSdkVersion x passes. (Again, you get to make a decision about library desugaring.) One part of that picture could be suppressions, as I mentioned in the comment linked above.
    • Incidentally, I see the existing comment about using the "scents" from https://github.com/open-toast/gummy-bears. That's worked well for us in Guava, possibly better than net.sf.androidscents.signature in handling always-desugared ("backported?") APIs. But if it ain't broke....
  • The internal test you uncovered with your CL 836391909 passes its minSdkVersion-23 checking (since that's the current minimum for most Google foundational libraries). Again, part of the story may be suppressions, in that case using @RequiresApi.
    • Now that I think about it, I'm surprised that we don't seem to have seen problems with those tests when code in Guava refers to classes like Optional and Stream. Maybe they avoid Guava in that particular library?

When it comes to Google-internal usage of the class, I think you'll end up wanting to maintain compatibility with minSdkVersion 23, but you could get away with requiring library desugaring. I wouldn't expect you to be able to get away with minSdkVersion 26 yet, though I hope we're only a year or two away....

Then the question would be whether all other users could meet a baseline of 23+desugaring or (if you can support it easily enough) an optional alternative baseline of 26 without need for desugaring. (It's possible to have fancy Animal Sniffer checking to check both of those.)

You might prefer to keep things simple and compatible for now. That could mean supporting minSdkVersion 23 even without desugaring (as Guava does unless you use APIs with "Java 8" types in them), in which case the main work to do is to suppress the errors you're seeing, since the "Java 8" code in #2948 already won't run except under a newer Android version. But as always, nothing will be as simple as it should be.

@MukjepScarlet
Copy link
Contributor

The functional interfaces can have Java 8 annotation FunctionalInterface, like JsonSerializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants