androidfacebook

Android crash: BadParcelableException for com.facebook.iabadscontext.IABAdsContext when opening app from Facebook


I’m encountering a crash in my Android app. The crash log is:

android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.facebook.iabadscontext.IABAdsContext
        at android.os.Parcel.readParcelableCreatorInternal(Parcel.java:5247)
        at android.os.Parcel.readParcelableInternal(Parcel.java:5096)
        at android.os.Parcel.readValue(Parcel.java:4856)
        at android.os.Parcel.readValue(Parcel.java:4625)
        at android.os.Parcel.-$$Nest$mreadValue(Unknown)
        at android.os.Parcel$LazyValue.apply(Parcel.java:4723)
        at android.os.Parcel$LazyValue.apply(Parcel.java:4682)
        at android.os.BaseBundle.unwrapLazyValueFromMapLocked(BaseBundle.java:416)
        at android.os.BaseBundle.getValueAt(BaseBundle.java:402)
        at android.os.BaseBundle.getValue(BaseBundle.java:382)
        at android.os.BaseBundle.getValue(BaseBundle.java:365)
        at android.os.BaseBundle.getValue(BaseBundle.java:358)
        at android.os.BaseBundle.get(BaseBundle.java:715)
        at androidx.lifecycle.SavedStateHandle$Companion.createHandle(SavedStateHandle:413)
        at androidx.lifecycle.SavedStateHandleSupport.createSavedStateHandle(SavedStateHandleSupport:68)
        at androidx.lifecycle.SavedStateHandleSupport.createSavedStateHandle(SavedStateHandleSupport:101)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory$2.create(HiltViewModelFactory:100)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory:170)

I do not use or reference com.facebook.iabadscontext.IABAdsContext anywhere in my code, and I can’t find it in any Facebook SDK.

Has anyone else run into this issue? Is this class coming from Facebook’s in-app browser? Any way to prevent this crash or safely handle this case?

Environment: • Android 15 • Facebook SDK 18.0.2

Any insight would be greatly appreciated.


Solution

  • This issue is similar to historical bugs from Facebook’s in-app browser incorrectly passing internal SDK classes in Intent extras. Because Your app doesn’t reference the class com.facebook.iabadscontext.IABAdsContext, so when Android tries to unmarshal the Intent extras on your app’s side, it can’t find the class, leading to:

    Fix :
    1. Use setExtrasClassLoader(null) on the intent bundle

    use try..catch with your intent as below before redirection

      try {
            intent.extras?.classLoader = null  // Prevent crash from unknown Parcelable
        } catch (e: Exception) {
            Log.w("IntentFix", "Failed to sanitize intent extras", e)
        }
    

    If you use Hilt, this may need to happen very early, possibly in a BaseActivity that all your Activitys extend.

    2. Manually sanitize Intent.getExtras() before accessing values

    Only access safe primitive types (String, Int, etc.) and avoid auto-unpacking Parcelable.

    val bundle = intent.extras
    bundle?.keySet()?.forEach {
        val value = try {
            bundle.get(it)
        } catch (e: Exception) {
            null  // skip unknown types
        }
    }
    

    Reason of this crash :