Skip to content

Commit

Permalink
Sometimes the classloader on fragment.arguments is java.lang.BootClas…
Browse files Browse the repository at this point in the history
…sLoader, which cannot be used to load any app classes, which will then lead to a ClassNotFoundException. (#688)

This ensures that the classloader used for the arguments bundle is the same that was used to load the Fragment itself.

Co-authored-by: Andreas Rossbacher <[email protected]>
  • Loading branch information
rossbacher and Andreas Rossbacher authored Jul 11, 2023
1 parent 09bc9f8 commit fc70e45
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mvrx/src/main/kotlin/com/airbnb/mvrx/MavericksExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ fun <V : Any> args() = object : ReadOnlyProperty<Fragment, V> {
if (value == null) {
val args = thisRef.arguments
?: throw IllegalArgumentException("There are no fragment arguments!")
// Sometimes the class loader of the arguments is java.lang.BootClassLoader and thus the args bundle
// cannot be loaded. Ensure that it has the same classloader as the Fragment.
args.classLoader = thisRef::class.java.classLoader
val argUntyped = args.get(Mavericks.KEY_ARG)
argUntyped
?: throw IllegalArgumentException("Mavericks arguments not found at key _root_ide_package_.com.airbnb.mvrx.Mavericks.KEY_ARG!")
Expand All @@ -262,6 +265,9 @@ fun <V> argsOrNull() = object : ReadOnlyProperty<Fragment, V?> {
override fun getValue(thisRef: Fragment, property: KProperty<*>): V? {
if (!read) {
val args = thisRef.arguments
// Sometimes the class loader of the arguments is java.lang.BootClassLoader and thus the args bundle
// cannot be loaded. Ensure that it has the same classloader as the Fragment.
args?.classLoader = thisRef::class.java.classLoader
val argUntyped = args?.get(Mavericks.KEY_ARG)
@Suppress("UNCHECKED_CAST")
value = argUntyped as? V
Expand Down

0 comments on commit fc70e45

Please sign in to comment.