From 6b7fd4d53e2172778ed685a134ed4c1930770ae4 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Mon, 24 Oct 2022 17:55:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Efirebase-crashlytics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 5 ++++ .../java/com/shayu/phonelive/AppContext.java | 26 ++++++++++++++++--- .../com/shayu/phonelive/NeverCrashUtils.java | 6 ++--- build.gradle | 1 + 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d5d500093..9c373ad88 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'com.android.application' apply plugin: 'img-optimizer' apply plugin: 'com.google.gms.google-services' +apply plugin: 'com.google.firebase.crashlytics' android { dexOptions { @@ -217,6 +218,10 @@ repositories { dependencies { implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) + + implementation platform('com.google.firebase:firebase-bom:30.5.0') + implementation 'com.google.firebase:firebase-crashlytics' + //直播 api project(':main') //短视频 diff --git a/app/src/main/java/com/shayu/phonelive/AppContext.java b/app/src/main/java/com/shayu/phonelive/AppContext.java index 678e0fe79..c22bd16f7 100644 --- a/app/src/main/java/com/shayu/phonelive/AppContext.java +++ b/app/src/main/java/com/shayu/phonelive/AppContext.java @@ -6,9 +6,11 @@ import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.net.http.HttpResponseCache; +import android.os.Build; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.os.Message; -import android.os.Process; import android.text.TextUtils; import android.util.Log; @@ -23,6 +25,9 @@ import com.alibaba.android.arouter.launcher.ARouter; import com.blankj.utilcode.util.Utils; import com.facebook.appevents.AppEventsLogger; import com.fm.openinstall.OpenInstall; +import com.google.firebase.FirebaseApp; +import com.google.firebase.analytics.FirebaseAnalytics; +import com.google.firebase.crashlytics.FirebaseCrashlytics; import com.tencent.imsdk.v2.V2TIMGroupMemberInfo; import com.tencent.imsdk.v2.V2TIMManager; import com.tencent.imsdk.v2.V2TIMSimpleMsgListener; @@ -53,6 +58,7 @@ import java.io.File; import java.io.IOException; import java.lang.ref.WeakReference; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import io.rong.imkit.config.RongConfigCenter; @@ -124,9 +130,11 @@ public class AppContext extends CommonAppContext { super.onCreate(); //注册全局异常捕获 registerError(); + registerFirebaseCrash(); sInstance = this; L.setDeBug(BuildConfig.DEBUG); AppEventsLogger.activateApp(this); + FirebaseAnalytics.getInstance(this); File httpCacheDir = new File(getCacheDir(), "https"); try { @@ -302,8 +310,9 @@ public class AppContext extends CommonAppContext { activity.get().finish(); } } - Process.killProcess(Process.myPid()); - System.exit(0); + /* Process.killProcess(Process.myPid()); + System.exit(0);*/ + new Handler(Looper.getMainLooper()).postDelayed(()->{throw new RuntimeException(e);},100); }) .setUncaughtCrashHandler((t, e) -> { Log.e("ApplicationError", "子线程异常");//此处log只是展示,当debug为true时,主类内部log会打印异常信息 @@ -311,4 +320,15 @@ public class AppContext extends CommonAppContext { }) .register(this); } + public void registerFirebaseCrash(){ + FirebaseAnalytics.getInstance(this); + FirebaseApp.initializeApp(this); + FirebaseCrashlytics.getInstance().setUserId(CommonAppConfig.getInstance().getUid()); + FirebaseCrashlytics.getInstance().setCustomKey("uid",CommonAppConfig.getInstance().getUid()); + FirebaseCrashlytics.getInstance().setCustomKey("userData",SpUtil.getInstance().getStringValue(SpUtil.USER_INFO)); + FirebaseCrashlytics.getInstance().setCustomKey("isGoogle",CommonAppConfig.IS_GOOGLE_PLAY); + FirebaseCrashlytics.getInstance().setCustomKey("PhoneName", Build.BRAND); + FirebaseCrashlytics.getInstance().setCustomKey("Phone", Build.MODEL); + FirebaseCrashlytics.getInstance().setCustomKey("CPU", Arrays.toString(Build.SUPPORTED_ABIS)); + } } diff --git a/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java b/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java index 524c37b1f..61e644127 100644 --- a/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java +++ b/app/src/main/java/com/shayu/phonelive/NeverCrashUtils.java @@ -102,11 +102,11 @@ public class NeverCrashUtils { Toast.makeText(application, "发生闪退", Toast.LENGTH_SHORT).show(); FileUtil.saveStringToFile(new File(application.getDir("files", Context.MODE_PRIVATE).getAbsolutePath()),throwableToString(e),"error.log"); getMainCrashHandler().mainException(Looper.getMainLooper().getThread(), e); - + return; } } }); - +/* //子线程异常拦截 Thread.setDefaultUncaughtExceptionHandler((t, e) -> { if (isDebugMode()) { @@ -118,7 +118,7 @@ public class NeverCrashUtils { getMainCrashHandler().mainException(Looper.getMainLooper().getThread(), e); getUncaughtCrashHandler().uncaughtException(t, e); - }); + });*/ } public interface MainCrashHandler { diff --git a/build.gradle b/build.gradle index c23bf76cb..830820683 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,7 @@ buildscript { classpath 'com.chenenyu:img-optimizer:1.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20" classpath 'com.google.gms:google-services:4.3.3' + classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.2' }