5.0 KiB
Moshi Companion
Companion to Moshi with more practical features.
English | 简体中文
![]() |
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 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 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 to view the documentation
Changelog
- Click here 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
Third-Party Open Source Usage Statement
License
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