# Moshi Companion [![GitHub license](https://img.shields.io/github/license/HighCapable/moshi-companion?color=blue&style=flat-square)](https://github.com/HighCapable/moshi-companion/blob/main/LICENSE) [![Telegram](https://img.shields.io/badge/discussion-Telegram-blue.svg?logo=telegram&style=flat-square)](https://t.me/HighCapable) [![Telegram](https://img.shields.io/badge/discussion%20dev-Telegram-blue.svg?logo=telegram&style=flat-square)](https://t.me/HighCapable_Dev) [![QQ](https://img.shields.io/badge/discussion%20dev-QQ-blue.svg?logo=tencent-qq&logoColor=red&style=flat-square)](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) | 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

Hey, please stay! 👋

Here are related projects such as Android development tools, UI design, Gradle plugins, Xposed Modules and practical software.

If the project below can help you, please give me a star!

All projects are free, open source, and follow the corresponding open source license agreement.

→ To see more about my projects, please click here ←

## Star History ![Star History Chart](https://api.star-history.com/svg?repos=HighCapable/moshi-companion&type=Date) ## 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