Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash FacebookSDK Validate.kt line 129 #1151

Open
5 tasks done
michaeltiket opened this issue Jan 9, 2023 · 9 comments
Open
5 tasks done

Crash FacebookSDK Validate.kt line 129 #1151

michaeltiket opened this issue Jan 9, 2023 · 9 comments

Comments

@michaeltiket
Copy link

michaeltiket commented Jan 9, 2023

Checklist before submitting a bug report

Java version

11

Android version

variance

Android SDK version

14.1.1

Installation platform & version

Gradle

Package

Core & AppEvents

Goals

User not facing the crash

Expected results

User not facing the crash

Actual results

This was not happen when using facebook SDK is 11.2.0, only happen after update to 14.1.1 for supporting Android 12+ and adding client Token

I do not know how to reproduce as the crashlytic dashboard show early crash <1 Second. and there is no code reference to our codebase.

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tiket.gits/com.facebook.FacebookActivity}: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3763)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3931)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2356)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:264)
       at android.app.ActivityThread.main(ActivityThread.java:8315)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
Caused by com.facebook.FacebookSdkNotInitializedException: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.
       at com.facebook.internal.Validate.sdkInitialized(Validate.kt:129)
       at com.facebook.FacebookSdk.getApplicationId(FacebookSdk.kt:819)
       at com.facebook.login.LoginMethodHandler.getRedirectUrl(LoginMethodHandler.kt:83)
       at com.facebook.login.CustomTabLoginMethodHandler.getDeveloperDefinedRedirectURI(CustomTabLoginMethodHandler.kt:65)
       at com.facebook.login.CustomTabLoginMethodHandler.<init>(CustomTabLoginMethodHandler.kt:208)
       at com.facebook.login.CustomTabLoginMethodHandler$Companion$CREATOR$1.createFromParcel(CustomTabLoginMethodHandler.kt:226)
       at com.facebook.login.CustomTabLoginMethodHandler$Companion$CREATOR$1.createFromParcel(CustomTabLoginMethodHandler.kt:224)
       at android.os.Parcel.readParcelable(Parcel.java:3298)
       at android.os.Parcel.readParcelableArray(Parcel.java:3415)
       at com.facebook.login.LoginClient.<init>(LoginClient.kt:666)
       at com.facebook.login.LoginClient$Companion$CREATOR$1.createFromParcel(LoginClient.kt:707)
       at com.facebook.login.LoginClient$Companion$CREATOR$1.createFromParcel(LoginClient.kt:705)
       at android.os.Parcel.readParcelable(Parcel.java:3298)
       at android.os.Parcel.readValue(Parcel.java:3191)
       at android.os.Parcel.readArrayMapInternal(Parcel.java:3584)
       at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:296)
       at android.os.BaseBundle.unparcel(BaseBundle.java:240)
       at android.os.Bundle.getSparseParcelableArray(Bundle.java:1079)
       at androidx.fragment.app.FragmentStateManager.restoreState(FragmentStateManager.java:405)
       at androidx.fragment.app.FragmentManager.restoreSaveState(FragmentManager.java:2735)
       at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:198)
       at androidx.fragment.app.FragmentActivity$2.onContextAvailable(FragmentActivity.java:149)
       at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99)
       at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:322)
       at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:273)
       at com.facebook.FacebookActivity.onCreate(FacebookActivity.kt:58)
       at android.app.Activity.performCreate(Activity.java:8151)
       at android.app.Activity.performCreate(Activity.java:8135)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3732)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3931)
       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2356)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:264)
       at android.app.ActivityThread.main(ActivityThread.java:8315)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

Steps to reproduce

Update Facebook SDK to 14.1.1

Code samples & details

class FacebookAuthImpl(context: Context) : LifecycleObserver, FacebookAuth {

    companion object {
        @Synchronized
        fun initializeSdkIfNeeded(context: Context) {
            if (!FacebookSdk.isInitialized()) {
                FacebookSdk.setClientToken(BuildConfig.FB_CLIENT_ID)
                FacebookSdk.sdkInitialize(context)
            }
        }
    }

    init {
        initializeSdkIfNeeded(context.applicationContext)
    }

    private var mSocialAuthCallback: SocialAuthCallback? = null
    private var fbAuthIntentResult: ActivityResultLauncher<Collection<String>>? = null
    private val facebookCallbackManager by lazy { CallbackManager.Factory.create() }
    private val loginManager by lazy { LoginManager.getInstance() }

    override fun login(fragment: Fragment) {
        val context = fragment.context ?: return
        initializeSdkIfNeeded(context.applicationContext)
        fbAuthIntentResult?.launch(listOf("email", "public_profile"))
    }

    override fun login(activity: Activity) {
        initializeSdkIfNeeded(activity.applicationContext)
        fbAuthIntentResult?.launch(listOf("email", "public_profile"))
    }

    override fun logout() {
        AccessToken.getCurrentAccessToken()?.let {
            LoginManager.getInstance().logOut()
        }
    }

    private fun registerFacebookCallback() {
        loginManager.registerCallback(facebookCallbackManager, object : FacebookCallback<LoginResult> {
            override fun onSuccess(result: LoginResult) {
                val successResult = SocialAuthResult.Facebook(
                    _token = result.accessToken.token
                )
                mSocialAuthCallback?.onSocialAuthSuccess(successResult)
            }

            override fun onCancel() {
                mSocialAuthCallback?.onSocialAuthCancel()
            }

            override fun onError(error: FacebookException) {
                mSocialAuthCallback?.onSocialAuthFail(
                    SocialAuthErrorResult.LoginError(
                        SocialAuthErrorResult.AUTH_SOURCE_FACEBOOK,
                        error.localizedMessage.orEmpty()
                    )
                )
            }
        })
    }

    override fun setup(fragmentActivity: FragmentActivity, socialAuthCallback: SocialAuthCallback) {
        mSocialAuthCallback = socialAuthCallback
        fbAuthIntentResult = fragmentActivity.registerForActivityResult(
            loginManager.createLogInActivityResultContract(facebookCallbackManager)
        ) {
        }
        registerFacebookCallback()
        registerLifecycleOwner(fragmentActivity)
    }

    override fun setup(fragment: Fragment, socialAuthCallback: SocialAuthCallback) {
        mSocialAuthCallback = socialAuthCallback
        fbAuthIntentResult = fragment.registerForActivityResult(
            loginManager.createLogInActivityResultContract(facebookCallbackManager)
        ) {
        }
        registerFacebookCallback()
        registerLifecycleOwner(fragment)
    }

    private fun registerLifecycleOwner(lifecycleOwner: LifecycleOwner) {
        if (lifecycleOwner.lifecycle.currentState == Lifecycle.State.DESTROYED) {
            resetCallback()
            return
        }
        lifecycleOwner.lifecycle.removeObserver(this)
        lifecycleOwner.lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun resetCallback() {
        fbAuthIntentResult = null
        mSocialAuthCallback = null
    }
}
@sannysinghs
Copy link

sannysinghs commented Mar 15, 2023

@michaeltiket got a bit confused. Were you able to reproduce the issue?. I'm facing exact same issue with the same stack trace. I'm also using 14.1.1. Wondering if you have found a fix for it ?

@michaeltiket
Copy link
Author

@sannysinghs no, i am not able to reproduce, and also hasn't found the fix 😢

@maximkir-fl
Copy link

maximkir-fl commented May 2, 2023

I face the same issue where the Facebook initialization (SDK version 13.1.0) is delayed.

To reproduce the error, we need to figure out how to trigger (or what triggers) the com.facebook.FacebookActivity before the main activity.

@admund
Copy link

admund commented Aug 28, 2023

I have the same issue on the newest 1.6.2.
Steps to reproduce:

  1. Start Facebook login. See the Facebook app login screen.
  2. Go to background.
  3. Force the Android System to kill your app.
  4. Go back to your app. See the Facebook app login screen.
  5. Click the back button -> crash.

@michaeltiket
Copy link
Author

michaeltiket commented Sep 6, 2023

Hi @admund , have tried your steps to reproduce, but i still cannot reproduce it on my side.
Maybe some question related your step to reproduce

Steps to reproduce:

  1. Start Facebook login. See the Facebook app login screen.
    Question: Open our app, then click login facebook and it open native facebook app right? not the custom tab

  2. Go to background.

  3. Force the Android System to kill your app.
    Question: Force kill using, OS App Setting Force stop? or using teminal, is it same like don't keep activities?

  4. Go back to your app. See the Facebook app login screen.
    Question: restore app from background or click the icon app? if restore it will still on facbeook login app

  5. Click the back button -> crash. (This step, on my case, it do not crash)

i try using FB SDK 16.1.3

@admund
Copy link

admund commented Sep 6, 2023

Hi @michaeltiket

  1. Yes, native Facebook app. But I'm not logged there. This can be important 🤷
  2. No, the most straightforward way is to revoke some permissions or change language, then your App will be recreated. There was a button for that in old Logcat, but right now, I don't know how to do that from AS.
  3. Both should work.

@michaeltiket
Copy link
Author

Thanks @admund i have reproduced it 🙏 , so the fix is need to merge your changes right?

@admund
Copy link

admund commented Sep 6, 2023

@michaeltiket you need to build Facebook SDK with this fix and add to your app

@blu3-b1rd
Copy link

Hi @admund , I saw your fix and it works and I was wondering if you are planning to merge it or just keep it open as reference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants