ansman / kotshi

An annotation processor that generates Moshi adapters from immutable Kotlin data classes.
https://kotshi.ansman.se
Apache License 2.0
755 stars 42 forks source link

Best practices in multi-modular projects #309

Open charlee-dev opened 1 month ago

charlee-dev commented 1 month ago

Hi. I just have a question about a Kotlin best practices in multi-modular projects.

In the current setup I have multiple sub-modules with its own models annotated with @JsonSerializable and Factories. So each of them have the factory generated in the build directory, Then in the top module which is a lambda function module in my case I create moshi instance and I add all factories from submodules to it.

Is this a right approach? Will it be possible for Kotshi to automatically gather those factories and create one wrapper factory?

ansman commented 1 month ago

Unfortunately Kotshi doesn't have support for generating aggregate factories across modules. This mostly stems from a limitation in KSP/KAPT as it can only find annotated classes in a single module. There are ways around this that Hilt uses for example but they aren't ideal here.

I've been considering adding support for Moshi's codegen which would let you skip adding a factory all together but it does require reflection and some proguard rules to be generated.

For now your options are either to create one factory per module like you do now or to switch to Moshi's official codegen.