Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Goooler
Copy link
Contributor

@Goooler Goooler commented May 18, 2025

Description

Refs #2856 (comment).

Checklist

  • New code follows the Google Java Style Guide
    This is automatically checked by mvn verify, but can also be checked on its own using mvn spotless:check.
    Style violations can be fixed using mvn spotless:apply; this can be done in a separate commit to verify that it did not cause undesired changes.
  • If necessary, new public API validates arguments, for example rejects null
  • New public API has Javadoc
    • Javadoc uses @since $next-version$
      ($next-version$ is a special placeholder which is automatically replaced during release)
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
  • mvn clean verify javadoc:jar passes without errors

README.md Outdated
> [!IMPORTANT]\
> Gson is not a recommended library for interacting with JSON on Android. The open ended reflection in the Gson runtime doesn't play nicely with shrinking/optimization/obfuscation passes that Android release apps should perform.\
> If your app or library may be running on Android, instead look at [Kotlin Serialization](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md#basics) which uses code generation instead of reflection. This avoids Gson's runtime crashes when optimizations are applied, and results in faster performance on Android devices. If you still want to use Gson and attempt to avoid these crashes, you can see how to do so [here](Troubleshooting.md#-proguard--r8).
> If your app or library may be running on Android, consider using [Kotlin Serialization](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md#basics) or [Moshi](https://github.com/square/moshi), which use code generation instead of reflection. This avoids Gson's runtime crashes when optimizations are applied, and results in faster performance on Android devices. If you still want to use Gson and attempt to avoid these crashes, you can see how to do so [here](Troubleshooting.md#-proguard--r8).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can recommend Moshi unreservedly. By default it uses reflection, which is just as problematic on Android as with Gson, for the same reasons. So at least I think the text would have to say "Moshi codegen" and link to the codegen section of the documentation.

I'd also like to have something to help readers decide which solution is more appropriate for them. What would lead someone to choose Moshi over kotlinx.serialization?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/square/moshi?tab=readme-ov-file#codegen is only for KSP for Kotlin, I didn't paste it as Moshi also supports APT for Java.

Copy link
Member

@eamonnmcmanus eamonnmcmanus May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write something like this?

consider a solution that uses code generation instead of reflection, such as [Kotlin Serialization](...) or Moshi with codegen (for Java](...) or [Kotlin](...)).

I don't see where Java codegen is documented on the Moshi site, though I do see this separate project. Is that what you're referring to? If we're talking about how people can assemble other projects in order to avoid reflection, we might also reference auto-value-gson. That could be a separate change, though.

I still think that we would need to tell people why they might consider using Moshi codegen with Kotlin instead of the solution that is built in to the Kotlin platform.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops, I misremembered that...

Moshi's official code generation has only been available for Kotlin from the start, initially via KAPT (which is based on APT). I used it in my projects through KAPT instead of APT.

Copy link
Contributor Author

@Goooler Goooler May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the doc!

I still think that we would need to tell people why they might consider using Moshi codegen with Kotlin instead of the solution that is built in to the Kotlin platform.

Also amended the reason for it,

... when optimizations are applied (usually due to the fields missing or obfuscating)

Goooler and others added 2 commits May 21, 2025 09:30
Co-authored-by: Marcono1234 <[email protected]>
Co-authored-by: Marcono1234 <[email protected]>
Copy link
Member

@eamonnmcmanus eamonnmcmanus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still reluctant to make this change unless we can say why someone would want to use Moshi-codegen with Kotlin instead of the solution that is proposed and supported by JetBrains. I can believe there would be reasons, but can we say what they are?

@Goooler
Copy link
Contributor Author

Goooler commented May 22, 2025

Moshi's APIs share similarities with Gson, making it easier and more seamless for users to migrate between the two.

@eamonnmcmanus
Copy link
Member

Moshi's APIs share similarities with Gson, making it easier and more seamless for users to migrate between the two.

OK, do you want to add a sentence like this after the "This avoids..." sentence?

The Moshi API may be more familiar to users who already know Gson.

@Goooler
Copy link
Contributor Author

Goooler commented May 22, 2025

Done.

@eamonnmcmanus eamonnmcmanus merged commit de140ed into google:main May 22, 2025
11 checks passed
@Goooler Goooler deleted the also-moshi branch May 22, 2025 16:15
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.

3 participants