airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.85k stars 498 forks source link

Mavericks v2 not working in Kitkat (API level 19) #506

Open mahasadhu opened 3 years ago

mahasadhu commented 3 years ago

Hi there, I just realized mavericks v2 does not working in Kitkat Android. However it's intermittent, I'm not sure why is this happening

It returns this java.lang.reflect.InvocationTargetException error. Tested using version 2.0.0-beta3

01-22 18:35:37.578 4462-4462/id.mahasadhu.testmvrx1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: id.mahasadhu.testmvrx1, PID: 4462
    java.lang.RuntimeException: Unable to start activity ComponentInfo{id.mahasadhu.testmvrx1/id.mahasadhu.testmvrx1.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at id.mahasadhu.testmvrx1.MainActivity.onCreate(MainActivity.kt:9)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5017) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.airbnb.mvrx.MavericksFactoryKt.createDefaultViewModel(MavericksFactory.kt:80)
        at com.airbnb.mvrx.MavericksFactoryKt.createViewModel(MavericksFactory.kt:53)
        at com.airbnb.mvrx.MavericksFactoryKt.access$createViewModel(MavericksFactory.kt:1)
        at com.airbnb.mvrx.MavericksFactory.create(MavericksFactory.kt:23)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at com.airbnb.mvrx.MavericksViewModelProvider.get(MavericksViewModelProvider.kt:65)
        at com.airbnb.mvrx.MavericksViewModelProvider.get$default(MavericksViewModelProvider.kt:39)
        at id.mahasadhu.testmvrx1.MyFragment$$special$$inlined$fragmentViewModel$1.invoke(MavericksExtensions.kt:44)
        at id.mahasadhu.testmvrx1.MyFragment$$special$$inlined$fragmentViewModel$1.invoke(MavericksExtensions.kt)
        at com.airbnb.mvrx.DefaultViewModelDelegateFactory$createLazyViewModel$1.invoke(ViewModelDelegateProvider.kt:93)
        at com.airbnb.mvrx.DefaultViewModelDelegateFactory$createLazyViewModel$1.invoke(ViewModelDelegateProvider.kt:82)
        at com.airbnb.mvrx.lifecycleAwareLazy.getValue(lifecycleAwareLazy.kt:54)
        at com.airbnb.mvrx.lifecycleAwareLazy$1.onCreate(lifecycleAwareLazy.kt:32)
        at androidx.lifecycle.FullLifecycleObserverAdapter.onStateChanged(FullLifecycleObserverAdapter.java:36)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2691)
        at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
        at androidx.fragment.app.FragmentManager.moveToState(F

Code

Here is the code that I use to test

Activity:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Fragment, State, and VM:

data class MyState(val listing: Async<String> = Uninitialized) : MavericksState

class MyViewModel(initialState: MyState) : MavericksViewModel<MyState>(initialState) {

    init {
        fetchListing()
    }

    private fun fetchListing() {
        setState { copy(listing = Success("wew")) }
    }
}

class MyFragment : Fragment(R.layout.fragment_main), MavericksView {
    private val viewModel: MyViewModel by fragmentViewModel()

    override fun invalidate() = withState(viewModel) { state ->
        Toast.makeText(requireContext(), state.listing.invoke().toString(), Toast.LENGTH_LONG).show()
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentMain"
        android:name="id.mahasadhu.testmvrx1.MyFragment"/>

</androidx.appcompat.widget.LinearLayoutCompat>

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.appcompat.widget.LinearLayoutCompat>

Gradle app deps:

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation 'com.airbnb.android:mvrx:2.0.0-beta3'
}
gpeal commented 3 years ago

@mahasadhu Can you attach this as a sample project?

mahasadhu commented 3 years ago

hi @gpeal thanks for checking

Sure but I still can't find why is this happening as this is intermittent, and I've got no time to check since this crash only affect a very small portion of our users.

I'll come back here later if I got some more information to share.

Feel free to close the issue for now if needed.