# Moshi Companion
[](https://github.com/HighCapable/moshi-companion/blob/main/LICENSE)
[](https://t.me/HighCapable)
[](https://t.me/HighCapable_Dev)
[](https://qm.qq.com/cgi-bin/qm/qr?k=Pnsc5RY6N2mBKFjOLPiYldbAbprAU3V7&jump_from=webapi&authKey=X5EsOVzLXt1dRunge8ryTxDRrh9/IiW1Pua75eDLh9RE3KXE+bwXIYF5cWri/9lf)
Companion to Moshi with more practical features.
English | [简体中文](README-zh-CN.md)
| | [HighCapable](https://github.com/HighCapable) |
|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
This project belongs to the above-mentioned organization, **click the link above to follow this organization** and discover more good projects.
## What's this
This is a functional optimization tool for the [Moshi](https://github.com/square/moshi) project. It enables Moshi to
work seamlessly with R8 in Android projects without relying on `Class.forName` to locate adapter classes generated by `moshi-kotlin-codegen`. This
allows for
complete obfuscation of entity class names and fields, enhancing code security and reducing APK size.
## Motivation
When using Moshi for JSON serialization and deserialization in Android projects, entity classes annotated with `@JsonClass(generateAdapter = true)`
cause Moshi to dynamically load adapter classes generated by `moshi-kotlin-codegen` through the `Class.forName` method. If R8 code obfuscation and
optimization are enabled in the project, Moshi's default obfuscation rules preserve the names of entity classes and adapter classes from being
obfuscated to ensure proper reflection. However, this leads to reduced code security and increased APK size.
I explored Moshi's adapter generation principles and greatly appreciated this high-performance solution of generating handwritten code. Through
research, I believed that manually registering adapters generated by `moshi-codegen` into Moshi to allow class name obfuscation was a viable solution.
I once proposed this idea as a [PR](https://github.com/square/moshi/pull/2002) to the Moshi project. However, modifying the project itself could cause
unnecessary maintenance issues and might not meet everyone's needs. Additionally, there are some code generation performance issues that need to be
addressed. Following the principle of non-intrusive project modification, I have now chosen to extract this idea as an independent project for
maintenance.
Therefore, as proposed in my PR, the current implementation reads `@JsonClass(generateAdapter = true)` annotations throughout the entire project,
obtains all entity classes that need adapter generation, creates an `AdapterRegistry`, and then manually registers these adapters through
`Moshi.Builder` at runtime, while also improving the generation of obfuscation rules.
## Get Started
- [Click here](docs/guide.md) to view the documentation
## Changelog
- [Click here](docs/changelog.md) to view the historical changelog
## Promotion