Browse Source

Migration to Firebase Crashlytics SDK

pull/70/head
Ezerous 5 years ago
parent
commit
f2b9c86ccb
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 12
      app/build.gradle
  2. 5
      app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  4. 56
      app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java
  5. 13
      app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReporter.java
  6. 16
      app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReportingTree.java
  7. 4
      build.gradle

12
app/build.gradle

@ -3,7 +3,8 @@ import groovy.json.JsonSlurper
apply from: 'gradle/grgit.gradle' apply from: 'gradle/grgit.gradle'
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
apply plugin: 'io.fabric' apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
android { android {
compileSdkVersion 29 compileSdkVersion 29
@ -33,8 +34,9 @@ android {
multiDexEnabled true multiDexEnabled true
def date = new Date().format('ddMMyy_HHmmss') def date = new Date().format('ddMMyy_HHmmss')
archivesBaseName = archivesBaseName + "-$date" archivesBaseName = archivesBaseName + "-$date"
// Disable fabric build ID generation for debug builds firebaseCrashlytics {
ext.enableCrashlytics = false 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 'androidx.multidex:multidex:2.0.1' //TODO: Remove when minSdkVersion >= 21
implementation 'com.google.android.material:material:1.1.0' implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.firebase:firebase-analytics:17.4.3' 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.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.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! 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 force = true //TODO: Remove when minSdkVersion >= 21
@ -116,5 +118,3 @@ dependencies {
testImplementation 'org.powermock:powermock-api-mockito2:2.0.2' testImplementation 'org.powermock:powermock-api-mockito2:2.0.2'
testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1' testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1'
} }
apply plugin: 'com.google.gms.google-services'

5
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))) { if (key.equals(getString(R.string.pref_privacy_crashlytics_enable_key))) {
enabled = sharedPreferences.getBoolean(key, false); enabled = sharedPreferences.getBoolean(key, false);
if(enabled) if(enabled)
BaseApplication.getInstance().startFirebaseCrashlyticsCollection(); BaseApplication.getInstance().setFirebaseCrashlyticsEnabled(true);
else { else {
Timber.i("Crashlytics collection will be disabled after restarting."); Timber.i("Crashlytics collection will be disabled after restarting.");
BaseApplication.getInstance().setFirebaseCrashlyticsEnabled(false);
displayRestartAppToTakeEffectToast(); displayRestartAppToTakeEffectToast();
} }
} else if (key.equals(getString(R.string.pref_privacy_analytics_enable_key))) { } else if (key.equals(getString(R.string.pref_privacy_analytics_enable_key))) {
enabled = sharedPreferences.getBoolean(key, false); enabled = sharedPreferences.getBoolean(key, false);
BaseApplication.getInstance().setFirebaseAnalyticsCollection(enabled); BaseApplication.getInstance().setFirebaseAnalyticsEnabled(enabled);
if(enabled) if(enabled)
Timber.i("Analytics collection enabled."); Timber.i("Analytics collection enabled.");
else else

4
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) -> { builder.setPositiveButton("Yes, I want to help", (dialogInterface, i) -> {
addUserConsent(); addUserConsent();
FirebaseMessaging.getInstance().setAutoInitEnabled(true); FirebaseMessaging.getInstance().setAutoInitEnabled(true);
BaseApplication.getInstance().startFirebaseCrashlyticsCollection(); BaseApplication.getInstance().setFirebaseCrashlyticsEnabled(true);
BaseApplication.getInstance().setFirebaseAnalyticsCollection(true); BaseApplication.getInstance().setFirebaseAnalyticsEnabled(true);
setUserDataShareEnabled(true); setUserDataShareEnabled(true);
}); });
builder.setNegativeButton("Nope, leave me alone", (dialogInterface, i) -> { builder.setNegativeButton("Nope, leave me alone", (dialogInterface, i) -> {

56
app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java

@ -14,13 +14,12 @@ import androidx.multidex.MultiDexApplication;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.bumptech.glide.Glide; 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.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseApp;
import com.google.firebase.analytics.FirebaseAnalytics; import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
import com.itkacher.okhttpprofiler.OkHttpProfilerInterceptor; import com.itkacher.okhttpprofiler.OkHttpProfilerInterceptor;
import com.mikepenz.fontawesome_typeface_library.FontAwesome; import com.mikepenz.fontawesome_typeface_library.FontAwesome;
import com.mikepenz.iconics.IconicsDrawable; import com.mikepenz.iconics.IconicsDrawable;
@ -40,7 +39,6 @@ import gr.thmmy.mthmmy.BuildConfig;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.crashreporting.CrashReportingTree; import gr.thmmy.mthmmy.utils.crashreporting.CrashReportingTree;
import io.fabric.sdk.android.Fabric;
import okhttp3.CipherSuite; import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec; import okhttp3.ConnectionSpec;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
@ -53,6 +51,8 @@ import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.DISPLAY_RELAT
public class BaseApplication extends MultiDexApplication { public class BaseApplication extends MultiDexApplication {
private static BaseApplication baseApplication; //BaseApplication singleton private static BaseApplication baseApplication; //BaseApplication singleton
private CrashReportingTree crashReportingTree;
//Firebase //Firebase
private static String firebaseProjectId; private static String firebaseProjectId;
private FirebaseAnalytics firebaseAnalytics; private FirebaseAnalytics firebaseAnalytics;
@ -88,19 +88,23 @@ public class BaseApplication extends MultiDexApplication {
SharedPreferences settingsSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences settingsSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences draftsPrefs = getSharedPreferences(getString(R.string.pref_topic_drafts_key), MODE_PRIVATE); SharedPreferences draftsPrefs = getSharedPreferences(getString(R.string.pref_topic_drafts_key), MODE_PRIVATE);
if (settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_crashlytics_enable_key), false)) if (settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_crashlytics_enable_key), false)){
startFirebaseCrashlyticsCollection(); Timber.i("Starting app with Firebase Crashlytics enabled.");
else setFirebaseCrashlyticsEnabled(true);
Timber.i("Starting app with Crashlytics disabled."); }
else {
Timber.i("Starting app with Firebase Crashlytics disabled.");
setFirebaseCrashlyticsEnabled(false);
}
firebaseProjectId = FirebaseApp.getInstance().getOptions().getProjectId(); firebaseProjectId = FirebaseApp.getInstance().getOptions().getProjectId();
firebaseAnalytics = FirebaseAnalytics.getInstance(this); firebaseAnalytics = FirebaseAnalytics.getInstance(this);
boolean enableAnalytics = settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_analytics_enable_key), false); boolean enableAnalytics = settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_analytics_enable_key), false);
firebaseAnalytics.setAnalyticsCollectionEnabled(enableAnalytics); firebaseAnalytics.setAnalyticsCollectionEnabled(enableAnalytics);
if (enableAnalytics) if (enableAnalytics)
Timber.i("Starting app with Analytics enabled."); Timber.i("Starting app with Firebase Analytics enabled.");
else else
Timber.i("Starting app with Analytics disabled."); Timber.i("Starting app with Firebase Analytics disabled.");
SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext()); SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext());
PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor); PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor);
@ -215,24 +219,32 @@ public class BaseApplication extends MultiDexApplication {
firebaseAnalytics.logEvent(event, params); firebaseAnalytics.logEvent(event, params);
} }
public void setFirebaseAnalyticsCollection(boolean enabled) { public void setFirebaseAnalyticsEnabled(boolean enabled) {
firebaseAnalytics.setAnalyticsCollectionEnabled(enabled); firebaseAnalytics.setAnalyticsCollectionEnabled(enabled);
if (!enabled) if (!enabled)
firebaseAnalytics.resetAnalyticsData(); firebaseAnalytics.resetAnalyticsData();
if(enabled)
Timber.i("Firebase Analytics enabled.");
else
Timber.i("Firebase Analytics disabled.");
} }
// Set up Crashlytics, disabled for debug builds public void setFirebaseCrashlyticsEnabled(boolean enable) {
public void startFirebaseCrashlyticsCollection() { FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(enable);
if (!Fabric.isInitialized()) { if(enable){
Crashlytics crashlyticsKit = new Crashlytics.Builder() crashReportingTree = new CrashReportingTree();
.core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()) Timber.plant(crashReportingTree);
.build(); Timber.i("CrashReporting tree planted.");
// Initialize Fabric with the debug-disabled Crashlytics. Timber.i("Firebase Crashlytics enabled.");
Fabric.with(this, crashlyticsKit); }
Timber.plant(new CrashReportingTree()); else{
Timber.i("Crashlytics enabled."); if(crashReportingTree!=null) {
} else Timber.uproot(crashReportingTree);
Timber.i("Crashlytics were already initialized for this app session."); Timber.i("CrashReporting tree uprooted.");
}
Timber.i("Firebase Crashlytics disabled.");
}
} }
public static String getFirebaseProjectId(){ public static String getFirebaseProjectId(){

13
app/src/main/java/gr/thmmy/mthmmy/utils/crashreporting/CrashReporter.java

@ -1,6 +1,7 @@
package gr.thmmy.mthmmy.utils.crashreporting; 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.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@ -43,12 +44,14 @@ public class CrashReporter {
else else
languageValue = "Unknown"; languageValue = "Unknown";
Crashlytics.setString(themeKey, themeValue); FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
Crashlytics.setString(languageKey, languageValue); crashlytics.setCustomKey(themeKey, themeValue);
Crashlytics.setBool("isLoggedIn", BaseApplication.getInstance().getSessionManager().isLoggedIn()); crashlytics.setCustomKey(languageKey, languageValue);
crashlytics.setCustomKey("isLoggedIn", BaseApplication.getInstance().getSessionManager().isLoggedIn());
} }
public static void reportDocument(Document document, String key) { public static void reportDocument(Document document, String key) {
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
String documentString = document.toString(); String documentString = document.toString();
ParseHelpers.Language language = ParseHelpers.Language.getLanguage(document); 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); batch = documentString.substring(i * STRING_BATCH_LENGTH, (i + 1) * STRING_BATCH_LENGTH);
else else
batch = documentString.substring(i * STRING_BATCH_LENGTH); batch = documentString.substring(i * STRING_BATCH_LENGTH);
Crashlytics.setString(key + "_" + i + 1, batch); crashlytics.setCustomKey(key + "_" + i + 1, batch);
} }
} }
} }

16
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 android.util.Log;
import com.crashlytics.android.Crashlytics; import com.google.firebase.crashlytics.FirebaseCrashlytics;
import timber.log.Timber.DebugTree; import timber.log.Timber.DebugTree;
public class CrashReportingTree extends DebugTree { public class CrashReportingTree extends DebugTree {
private FirebaseCrashlytics firebaseCrashlytics;
public CrashReportingTree() {
super();
firebaseCrashlytics = FirebaseCrashlytics.getInstance();
}
@Override @Override
protected void log(int priority, String tag, String message, Throwable t) { protected void log(int priority, String tag, String message, Throwable t) {
if (priority == Log.VERBOSE || priority == Log.DEBUG) { if (priority == Log.VERBOSE || priority == Log.DEBUG) {
@ -25,14 +30,13 @@ public class CrashReportingTree extends DebugTree {
else else
level = 'A'; level = 'A';
Crashlytics.log(level + "/" + tag + ": " + message); firebaseCrashlytics.log(level + "/" + tag + ": " + message);
if(priority == Log.ERROR) { if(priority == Log.ERROR) {
if (t!=null) if (t!=null)
Crashlytics.logException(t); firebaseCrashlytics.recordException(t);
else else
Crashlytics.logException(new Exception(message)); firebaseCrashlytics.recordException(new Exception(message));
} }
} }
} }

4
build.gradle

@ -3,7 +3,6 @@ apply plugin: "com.github.ben-manes.versions"
buildscript { buildscript {
repositories { repositories {
maven { url "https://maven.fabric.io/public" }
google() google()
jcenter() jcenter()
maven { url "https://jitpack.io" } maven { url "https://jitpack.io" }
@ -11,7 +10,7 @@ buildscript {
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.6.3' classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.google.gms:google-services:4.3.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 '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" classpath "com.github.ben-manes:gradle-versions-plugin:0.21.0"
} }
@ -20,6 +19,7 @@ buildscript {
allprojects { allprojects {
repositories { repositories {
maven { url "https://maven.google.com" } maven { url "https://maven.google.com" }
google()
jcenter() jcenter()
maven { url "https://jitpack.io" } maven { url "https://jitpack.io" }
} }

Loading…
Cancel
Save