mirror of
https://github.com/HighCapable/moshi-companion.git
synced 2025-10-18 16:49:25 +08:00
92 lines
5.0 KiB
Markdown
92 lines
5.0 KiB
Markdown
# 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)
|
|
|
|
| <img src="https://github.com/HighCapable/.github/blob/main/img-src/logo.jpg?raw=true" width = "30" height = "30" alt="LOGO"/> | [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
|
|
|
|
<!--suppress HtmlDeprecatedAttribute -->
|
|
<div align="center">
|
|
<h2>Hey, please stay! 👋</h2>
|
|
<h3>Here are related projects such as Android development tools, UI design, Gradle plugins, Xposed Modules and practical software. </h3>
|
|
<h3>If the project below can help you, please give me a star! </h3>
|
|
<h3>All projects are free, open source, and follow the corresponding open source license agreement. </h3>
|
|
<h1><a href="https://github.com/fankes/fankes/blob/main/project-promote/README.md">→ To see more about my projects, please click here ←</a></h1>
|
|
</div>
|
|
|
|
## Star History
|
|
|
|

|
|
|
|
## Third-Party Open Source Usage Statement
|
|
|
|
- [Moshi](https://github.com/square/moshi)
|
|
- [Gson](https://github.com/google/gson)
|
|
|
|
## License
|
|
|
|
- [Apache-2.0](https://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
```
|
|
Apache License Version 2.0
|
|
|
|
Copyright (C) 2019 HighCapable
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
```
|
|
|
|
Copyright © 2019 HighCapable |