Skip to content

ClassCastException in MoxyKtxDelegate when using R8 full mode #146

@romankulykov

Description

@romankulykov

I’m experiencing a ClassCastException when starting my GlobalActivity. The error occurs during Presenter injection in the MoxyKtxDelegate (specifically in the providePresenter method). When using R8 full mode, the crash happens consistently. However, switching to R8 compatibility mode (by setting android.enableR8.fullMode=false) allows the app to run without crashing.
What I’ve Tried:
• Added the following ProGuard/R8 rules to preserve necessary classes and attributes:

-keepattributes Signature,RuntimeVisibleAnnotations,RuntimeVisibleParameterAnnotations,EnclosingMethod,InnerClasses

-keep class moxy.** { *; }
-keep class moxy.ktx.** { *; }

-keepclasseswithmembers class * {
    @moxy.InjectPresenter <fields>;
}

-keep,allowobfuscation class * extends moxy.MvpPresenter { *; }

-keep class *$$Lambda$* { *; }
# Do not obfuscate meta-information with annotations
-keepattributes *Annotation*
# Do not obfuscate classes with Injected Presenters
-keepclasseswithmembernames class * { @moxy.presenter.InjectPresenter <fields>; }
# Do not obfuscate names of classes with Injected View States
-keepnames @moxy.InjectViewState class *
# Do not obfuscate presenters names
-keepnames class * extends moxy.MvpPresenter
# Do not obfuscate PresentersBinder autogenerated classes
-keep class **$$PresentersBinder { *; }
# Do not obfuscate ViewStateProvider autogenerated classes
-keep class **$$ViewStateProvider { *; }
# Keep Moxy classes
-keep class moxy.** { *; }
# Don't warn about unresolved references of coroutines, androidx classes etc
-dontwarn moxy.**

# R8 with full mode can remove *Command classes with same signature which breaks command queue
# More info: https://github.com/moxy-community/Moxy/issues/141
-keep,allowobfuscation,allowshrinking class * extends moxy.viewstate.ViewCommand { *; }
# R8 with full mode removes custom strategy default constructor
-keepclassmembers class * extends moxy.viewstate.strategy.StateStrategy {
    <init>();
}

Despite these rules, the ClassCastException still occurs in full mode.
I suspect that R8 full mode might be aggressively optimizing or merging classes that are critical for Presenter injection, causing issues during type casting.

Any guidance on additional rules or adjustments needed to safely run in R8 full mode would be greatly appreciated.

Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions