Android崩溃com.google.android.gms.common.api.internal.TaskUtil.setResultOrApiException

时间:2018-10-10 01:33:21

标签: android firebase firebase-dynamic-links

我在crashlytics上遇到了其中一些崩溃,但是我无法重现。我试图找出导致此崩溃的原因。

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)

我正在尝试从Firebase生成动态链接。 我认为这可能是导致问题的代码:

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");
            }
        }

有人可以指出我正确的方向吗? TIA

1 个答案:

答案 0 :(得分:0)

导致此问题的原因是由于某些原因而导致构建Firebase短动态链接失败。我所做的工作是创建一个长链接,然后创建一个短链接。

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

我添加的另一件事是额外的检查,以查看请求是否成功。

addOnCompleteListener(task -> {
if(!task.isSuccessful()) return;

参考:

希望它对某人有帮助!