From f2b9c86ccbef84b9f984a8779360697317d902bb Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 8 Jun 2020 18:47:47 +0300 Subject: [PATCH] Migration to Firebase Crashlytics SDK --- app/build.gradle | 12 ++-- .../activities/settings/SettingsFragment.java | 5 +- .../gr/thmmy/mthmmy/base/BaseActivity.java | 4 +- .../gr/thmmy/mthmmy/base/BaseApplication.java | 56 +++++++++++-------- .../utils/crashreporting/CrashReporter.java | 13 +++-- .../crashreporting/CrashReportingTree.java | 16 ++++-- build.gradle | 4 +- 7 files changed, 65 insertions(+), 45 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index afd40760..e2eb5c50 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,7 +3,8 @@ import groovy.json.JsonSlurper apply from: 'gradle/grgit.gradle' apply plugin: 'com.android.application' -apply plugin: 'io.fabric' +apply plugin: 'com.google.gms.google-services' +apply plugin: 'com.google.firebase.crashlytics' android { compileSdkVersion 29 @@ -33,8 +34,9 @@ android { multiDexEnabled true def date = new Date().format('ddMMyy_HHmmss') archivesBaseName = archivesBaseName + "-$date" - // Disable fabric build ID generation for debug builds - ext.enableCrashlytics = false + firebaseCrashlytics { + mappingFileUploadEnabled false // Disable mapping file uploading for debug builds + } } } @@ -87,8 +89,8 @@ dependencies { implementation 'androidx.multidex:multidex:2.0.1' //TODO: Remove when minSdkVersion >= 21 implementation 'com.google.android.material:material:1.1.0' implementation 'com.google.firebase:firebase-analytics:17.4.3' + implementation 'com.google.firebase:firebase-crashlytics:17.0.1' implementation 'com.google.firebase:firebase-messaging:20.2.0' - implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1' implementation 'com.snatik:storage:2.1.0' implementation ('com.squareup.okhttp3:okhttp:3.12.12') { //TODO: Warning: OkHttp has dropped support for Android 19 since OkHttp 3.13! force = true //TODO: Remove when minSdkVersion >= 21 @@ -116,5 +118,3 @@ dependencies { testImplementation 'org.powermock:powermock-api-mockito2:2.0.2' testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1' } - -apply plugin: 'com.google.gms.google-services' diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java index edd06356..ef2d1693 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java @@ -200,14 +200,15 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared if (key.equals(getString(R.string.pref_privacy_crashlytics_enable_key))) { enabled = sharedPreferences.getBoolean(key, false); if(enabled) - BaseApplication.getInstance().startFirebaseCrashlyticsCollection(); + BaseApplication.getInstance().setFirebaseCrashlyticsEnabled(true); else { Timber.i("Crashlytics collection will be disabled after restarting."); + BaseApplication.getInstance().setFirebaseCrashlyticsEnabled(false); displayRestartAppToTakeEffectToast(); } } else if (key.equals(getString(R.string.pref_privacy_analytics_enable_key))) { enabled = sharedPreferences.getBoolean(key, false); - BaseApplication.getInstance().setFirebaseAnalyticsCollection(enabled); + BaseApplication.getInstance().setFirebaseAnalyticsEnabled(enabled); if(enabled) Timber.i("Analytics collection enabled."); else diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 37f88876..5adfd788 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -807,8 +807,8 @@ public abstract class BaseActivity extends AppCompatActivity { builder.setPositiveButton("Yes, I want to help", (dialogInterface, i) -> { addUserConsent(); FirebaseMessaging.getInstance().setAutoInitEnabled(true); - BaseApplication.getInstance().startFirebaseCrashlyticsCollection(); - BaseApplication.getInstance().setFirebaseAnalyticsCollection(true); + BaseApplication.getInstance().setFirebaseCrashlyticsEnabled(true); + BaseApplication.getInstance().setFirebaseAnalyticsEnabled(true); setUserDataShareEnabled(true); }); builder.setNegativeButton("Nope, leave me alone", (dialogInterface, i) -> { diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java index 755b539a..51d01bba 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java @@ -14,13 +14,12 @@ import androidx.multidex.MultiDexApplication; import androidx.preference.PreferenceManager; import com.bumptech.glide.Glide; -import com.crashlytics.android.Crashlytics; -import com.crashlytics.android.core.CrashlyticsCore; import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import com.google.firebase.FirebaseApp; import com.google.firebase.analytics.FirebaseAnalytics; +import com.google.firebase.crashlytics.FirebaseCrashlytics; import com.itkacher.okhttpprofiler.OkHttpProfilerInterceptor; import com.mikepenz.fontawesome_typeface_library.FontAwesome; import com.mikepenz.iconics.IconicsDrawable; @@ -40,7 +39,6 @@ import gr.thmmy.mthmmy.BuildConfig; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.crashreporting.CrashReportingTree; -import io.fabric.sdk.android.Fabric; import okhttp3.CipherSuite; import okhttp3.ConnectionSpec; import okhttp3.HttpUrl; @@ -53,6 +51,8 @@ import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.DISPLAY_RELAT public class BaseApplication extends MultiDexApplication { private static BaseApplication baseApplication; //BaseApplication singleton + private CrashReportingTree crashReportingTree; + //Firebase private static String firebaseProjectId; private FirebaseAnalytics firebaseAnalytics; @@ -88,19 +88,23 @@ public class BaseApplication extends MultiDexApplication { SharedPreferences settingsSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences draftsPrefs = getSharedPreferences(getString(R.string.pref_topic_drafts_key), MODE_PRIVATE); - if (settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_crashlytics_enable_key), false)) - startFirebaseCrashlyticsCollection(); - else - Timber.i("Starting app with Crashlytics disabled."); + if (settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_crashlytics_enable_key), false)){ + Timber.i("Starting app with Firebase Crashlytics enabled."); + setFirebaseCrashlyticsEnabled(true); + } + else { + Timber.i("Starting app with Firebase Crashlytics disabled."); + setFirebaseCrashlyticsEnabled(false); + } firebaseProjectId = FirebaseApp.getInstance().getOptions().getProjectId(); firebaseAnalytics = FirebaseAnalytics.getInstance(this); boolean enableAnalytics = settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_analytics_enable_key), false); firebaseAnalytics.setAnalyticsCollectionEnabled(enableAnalytics); if (enableAnalytics) - Timber.i("Starting app with Analytics enabled."); + Timber.i("Starting app with Firebase Analytics enabled."); else - Timber.i("Starting app with Analytics disabled."); + Timber.i("Starting app with Firebase Analytics disabled."); SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext()); PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor); @@ -215,24 +219,32 @@ public class BaseApplication extends MultiDexApplication { firebaseAnalytics.logEvent(event, params); } - public void setFirebaseAnalyticsCollection(boolean enabled) { + public void setFirebaseAnalyticsEnabled(boolean enabled) { firebaseAnalytics.setAnalyticsCollectionEnabled(enabled); if (!enabled) firebaseAnalytics.resetAnalyticsData(); + + if(enabled) + Timber.i("Firebase Analytics enabled."); + else + Timber.i("Firebase Analytics disabled."); } - // Set up Crashlytics, disabled for debug builds - public void startFirebaseCrashlyticsCollection() { - if (!Fabric.isInitialized()) { - Crashlytics crashlyticsKit = new Crashlytics.Builder() - .core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()) - .build(); - // Initialize Fabric with the debug-disabled Crashlytics. - Fabric.with(this, crashlyticsKit); - Timber.plant(new CrashReportingTree()); - Timber.i("Crashlytics enabled."); - } else - Timber.i("Crashlytics were already initialized for this app session."); + public void setFirebaseCrashlyticsEnabled(boolean enable) { + FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(enable); + if(enable){ + crashReportingTree = new CrashReportingTree(); + Timber.plant(crashReportingTree); + Timber.i("CrashReporting tree planted."); + Timber.i("Firebase Crashlytics enabled."); + } + else{ + if(crashReportingTree!=null) { + Timber.uproot(crashReportingTree); + Timber.i("CrashReporting tree uprooted."); + } + Timber.i("Firebase Crashlytics disabled."); + } } public static String getFirebaseProjectId(){ diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReporter.java b/app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReporter.java index 65011440..4842013c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReporter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReporter.java @@ -1,6 +1,7 @@ package gr.thmmy.mthmmy.utils.crashreporting; -import com.crashlytics.android.Crashlytics; + +import com.google.firebase.crashlytics.FirebaseCrashlytics; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -43,12 +44,14 @@ public class CrashReporter { else languageValue = "Unknown"; - Crashlytics.setString(themeKey, themeValue); - Crashlytics.setString(languageKey, languageValue); - Crashlytics.setBool("isLoggedIn", BaseApplication.getInstance().getSessionManager().isLoggedIn()); + FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); + crashlytics.setCustomKey(themeKey, themeValue); + crashlytics.setCustomKey(languageKey, languageValue); + crashlytics.setCustomKey("isLoggedIn", BaseApplication.getInstance().getSessionManager().isLoggedIn()); } public static void reportDocument(Document document, String key) { + FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); String documentString = document.toString(); ParseHelpers.Language language = ParseHelpers.Language.getLanguage(document); @@ -71,7 +74,7 @@ public class CrashReporter { batch = documentString.substring(i * STRING_BATCH_LENGTH, (i + 1) * STRING_BATCH_LENGTH); else batch = documentString.substring(i * STRING_BATCH_LENGTH); - Crashlytics.setString(key + "_" + i + 1, batch); + crashlytics.setCustomKey(key + "_" + i + 1, batch); } } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReportingTree.java b/app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReportingTree.java index f378000e..83e55a37 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReportingTree.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReportingTree.java @@ -2,12 +2,17 @@ package gr.thmmy.mthmmy.utils.crashreporting; import android.util.Log; -import com.crashlytics.android.Crashlytics; +import com.google.firebase.crashlytics.FirebaseCrashlytics; import timber.log.Timber.DebugTree; public class CrashReportingTree extends DebugTree { - + private FirebaseCrashlytics firebaseCrashlytics; + public CrashReportingTree() { + super(); + firebaseCrashlytics = FirebaseCrashlytics.getInstance(); + } + @Override protected void log(int priority, String tag, String message, Throwable t) { if (priority == Log.VERBOSE || priority == Log.DEBUG) { @@ -25,14 +30,13 @@ public class CrashReportingTree extends DebugTree { else level = 'A'; - Crashlytics.log(level + "/" + tag + ": " + message); + firebaseCrashlytics.log(level + "/" + tag + ": " + message); if(priority == Log.ERROR) { if (t!=null) - Crashlytics.logException(t); + firebaseCrashlytics.recordException(t); else - Crashlytics.logException(new Exception(message)); + firebaseCrashlytics.recordException(new Exception(message)); } - } } diff --git a/build.gradle b/build.gradle index 0400e33c..d966ef2f 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,6 @@ apply plugin: "com.github.ben-manes.versions" buildscript { repositories { - maven { url "https://maven.fabric.io/public" } google() jcenter() maven { url "https://jitpack.io" } @@ -11,7 +10,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.6.3' classpath 'com.google.gms:google-services:4.3.3' - classpath 'io.fabric.tools:gradle:1.31.2' + classpath 'com.google.firebase:firebase-crashlytics-gradle:2.1.1' classpath 'org.ajoberstar.grgit:grgit-core:3.1.1' // Also change in app/gradle/grgit.gradle classpath "com.github.ben-manes:gradle-versions-plugin:0.21.0" } @@ -20,6 +19,7 @@ buildscript { allprojects { repositories { maven { url "https://maven.google.com" } + google() jcenter() maven { url "https://jitpack.io" } }