Skip to content

Commit

Permalink
Add new keep rule for MvRxViewModelFactory without JvmStatic and init…
Browse files Browse the repository at this point in the history
…ialState (#181)

#169 Added a new initialState method and ability to not use @JvmStatic all of these need keep rules.

Closes #176

** Note: Kotlin 1.3+ requires this keep rule as well:

keep class kotlin.reflect.jvm.internal.impl.serialization.deserialization.builtins.BuiltInsLoaderImpl

Per the discussion in #138, this is not a project specific rule, so not including it in the proguard file, but updated wiki to suggest it.

Tested on sample app.

Created #182 to prevent this from the future. Would be a great first task if anyone from the community wants to jump on it :)
  • Loading branch information
Ben Schwab authored and gpeal committed Jan 14, 2019
1 parent 1ad21ef commit 037ab28
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
16 changes: 13 additions & 3 deletions mvrx/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@

# Classes extending BaseMvRxViewModel are recreated using reflection, which assumes that a one argument
# constructor accepting a data class holding the state exists. Need to make sure to keep the constructor
# around. Additionally, a static create method will be generated in the case a companion object factory
# is used. This is accessed via reflection.
# around. Additionally, a static create / inital state method will be generated in the case a
# companion object factory is used with JvmStatic. This is accessed via reflection.
-keepclassmembers class ** extends com.airbnb.mvrx.BaseMvRxViewModel {
public <init>(...);
public static *** create(...);
public static *** initialState(...);
}

# If a MvRxViewModelFactory is used without JvmStatic, keep create and initalState methods which
# are accessed via reflection.
-keepclassmembers class ** implements com.airbnb.mvrx.MvRxViewModelFactory {
public <init>(...);
public *** create(...);
public *** initialState(...);
}


# Members of the Kotlin data classes used as the state in MvRx are read via Kotlin reflection which cause trouble
# with Proguard if they are not kept.
-keepclassmembers class ** implements com.airbnb.mvrx.MvRxState {
*;
}
}
1 change: 1 addition & 0 deletions sample/proguard-project.pro
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
#

-keep class kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoaderImpl
-keep class kotlin.reflect.jvm.internal.impl.serialization.deserialization.builtins.BuiltInsLoaderImpl

-keepclassmembers class kotlin.Metadata {
public <methods>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class DadJokeDetailViewModel(
}

companion object : MvRxViewModelFactory<DadJokeDetailViewModel, DadJokeDetailState> {
override fun create(viewModelContext: ViewModelContext, state: DadJokeDetailState): DadJokeDetailViewModel {
// Intentionally leaving unnecessary JvmStatic to test Proguard rules.
@JvmStatic override fun create(viewModelContext: ViewModelContext, state: DadJokeDetailState): DadJokeDetailViewModel {
val service: DadJokeService by viewModelContext.activity.inject()
return DadJokeDetailViewModel(state, service)
}
Expand Down

0 comments on commit 037ab28

Please sign in to comment.