firebase / firebase-android-sdk

Firebase Android SDK
https://firebase.google.com
Apache License 2.0
2.26k stars 573 forks source link

Firebase Dynamic Link crashes. setResultOrApiException(Unknown Source:12) #69

Closed richardliveraise closed 4 years ago

richardliveraise commented 5 years ago

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

I've been getting this error log after I added Firebase Dynamic Link Invite. I still couldn't reproduce it though.

Fatal Exception: com.google.android.gms.f.f: com.google.android.gms.common.api.b: 8: 
       at com.google.android.gms.tasks.zzu.getResult(Unknown Source:17)
       at com.ordinaryman.android.referral.ReferralManager.init(Unknown Source:18)
       at com.ordinaryman.android.referral.ReferralManager$$Lambda$2.onComplete(Unknown Source:11)
       at com.google.android.gms.tasks.zzj.run(Unknown Source:23)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6938)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Caused by com.google.android.gms.common.api.b: 8: 
       at com.google.android.gms.common.api.internal.TaskUtil.setResultOrApiException(Unknown Source:12)
       at com.google.android.gms.internal.measurement.zzyn.zza(Unknown Source:2)
       at com.google.android.gms.internal.measurement.zzys.dispatchTransaction(Unknown Source:21)
       at com.google.android.gms.internal.measurement.zzo.onTransact(Unknown Source:22)
       at android.os.Binder.execTransact(Binder.java:682)

Relevant Code:

try {

            generateShortenedDeepLink(context, code, refId, src, imageUrl).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if (onDoneLoading != null) {
                        onDoneLoading.run();
                    }

                    log.error("Error generating deeplink", e);

                    if (context instanceof BaseActivity) {
                        ((BaseActivity) context).showToastWarn("Error generating code. ");
                    }

                    return;
                }
            }).addOnCompleteListener(task -> {

                BaseActivity act;
                act = (BaseActivity) context;

                String defaultMessage = "Hi, come join me on this app ";
                String linkUrl = task.getResult().getShortLink().toString();
                String textMessage = defaultMessage + " " + linkUrl;

                if (StringUtils.isEmpty(imageUrl)) {
                    shareHandler(act, textMessage, null);
                    if (onDoneLoading != null) {
                        onDoneLoading.run();
                    }
                    return;
                }

                String filename = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
                File cachePath = new File(context.getCacheDir(), "images");
                cachePath.mkdirs(); // don't forget to make the directory
                File imageFile = new File(cachePath, filename);

                Runnable continuation = () -> {
                    Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName(), imageFile);
                    shareHandler(act, textMessage, contentUri);
                    if (onDoneLoading != null) {
                        app.getMainHandler().postDelayed(onDoneLoading, 3000);
                    }
                };

                if (imageFile.exists()) {
                    continuation.run();
                    return;
                }

                if (act.isNoMoUIChanges()) return;

                Glide.with(context)
                        .asBitmap()
                        .load(imageUrl)
                        .into(new SimpleTarget<Bitmap>() {

                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

                                try (FileOutputStream stream = new FileOutputStream(imageFile)) {
                                    resource.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                                } catch (IOException e) {
                                    log.error("Error generating share", e);
                                    if (onDoneLoading != null) {
                                        onDoneLoading.run();
                                    }
                                    return;
                                }
                                continuation.run();
                            }
                        });
            });

        } catch (Exception e) {
            if (context instanceof BaseActivity) {
                ((BaseActivity) context).showToastWarn("Error generating code. Please try again later");
            }
        }

private void shareHandler(BaseActivity context, String smsMsg, Uri contentUri) {

        if(context.isNoMoUIChanges()) return;

        Resources resources = context.getResources();

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
        sendIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
        sendIntent.putExtra(Intent.EXTRA_TEXT, smsMsg);
        sendIntent.setType(context.getContentResolver().getType(contentUri));

        Intent openInChooser = Intent.createChooser(sendIntent, resources.getString(R.string.share_intent_chooser_title));
        context.startActivity(openInChooser);
    }
ashwinraghav commented 5 years ago

@richardliveraise Thanks for reaching out! We do anticipate that this would be fixed in the upcoming release schedule a couple of weeks down the line. Thanks for working with us on this.

richardliveraise commented 5 years ago

@ashwinraghav can you clarify what root cause of this issue? any workaround I can do in the meantime? Thanks!

richardliveraise commented 5 years ago

@ashwinraghav I figured the issue was generating the short link. The workaround I did was generate a long link, then get the short link.

 //Generate long link
 Uri dynamicLinkUri = dynamicLink.getUri();
 return FirebaseDynamicLinks.getInstance().createDynamicLink()
        .setLongLink(dynamicLinkUri)
        .buildShortDynamicLink(); 

Reference: https://stackoverflow.com/questions/52152116/short-dynamic-link-error-com-google-android-gms-common-api-apiexception-8/52157428#52157428

Note that prior doing the workaround, I updated the SDK to the latest one, then it'd work for newer OS (7.0, 8.0) but it wouldn't work for older OS (5.0, 6.0). If you could explain what the cause of this, I'd really appreciate it. Thanks!

aguatno commented 4 years ago

Hi @richardliveraise sorry for my late response here. It looks like the error log didn't persist in the latest version. I was wondering if you're still having this issue? If so, could you provide the following so that I can investigate the issue further:

google-oss-bot commented 4 years ago

Hey @richardliveraise. We need more information to resolve this issue but there hasn't been an update in 7 days. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 4 years ago

Since there haven't been any recent updates here, I am going to close this issue.

@richardliveraise if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.