moxy-community / Moxy

Moxy is MVP library for Android with incremental annotation processor and ktx features
MIT License
324 stars 33 forks source link

Add proguard rules to support R8 full mode #142

Closed dzmpr closed 3 months ago

dzmpr commented 4 months ago

Resolves #141

aasitnikov commented 4 months ago

Thanks for the PR!

The problem from the linked issue is caused by R8 performing class merging optimization (similar to what is described in this article). However, AddToEndSingleStrategy relies on commands to have different Classes. Therefore, we need to disable the optimization of classes that extend ViewCommand. This can achieved by using the following proguard rule:

-keep,allowobfuscation,allowshrinking class * extends moxy.viewstate.ViewCommand { *; }

I believe this rule is enough to fix the issue.


Regarding the issue, where R8 removes custom strategies. Moxy uses Class.newInstance() to reflectively create an instance of custom strategy given a strategy Class. However, in full mode, R8 removes the constructor, as stated in R8 FAQ:

The default constructor (\()) is not implicitly kept when a class is kept.

So we need to write a rule to keeps this constructor:

-keepclassmembers class * extends moxy.viewstate.strategy.StateStrategy {
    <init>();
}
dzmpr commented 3 months ago

@aasitnikov Can confirm that suggested rules are enough to fix problem. I updated PR.