From 0002ce849637a8180587746bddd2b8940ef6f603 Mon Sep 17 00:00:00 2001 From: oasisfeng Date: Sat, 17 Oct 2020 16:45:27 +0800 Subject: [PATCH] UPDATE: Migrate Fabric Crashlytics to Firebase Crashlytics. FirebaseServiceProxy is removed, as Firebase Crashlytics is apparently working in China now. --- assembly/build.gradle | 8 ++-- build.gradle | 1 + shared/build.gradle | 11 ++--- .../oasisfeng/island/IslandApplication.java | 8 ---- .../island/analytics/CrashReport.java | 33 ++++++-------- .../island/firebase/FirebaseServiceProxy.kt | 44 ------------------- shared/src/main/res/values/values.xml | 4 -- 7 files changed, 23 insertions(+), 86 deletions(-) delete mode 100644 shared/src/main/java/com/oasisfeng/island/firebase/FirebaseServiceProxy.kt delete mode 100644 shared/src/main/res/values/values.xml diff --git a/assembly/build.gradle b/assembly/build.gradle index bac05a0c6..0a3e3c32f 100644 --- a/assembly/build.gradle +++ b/assembly/build.gradle @@ -1,4 +1,6 @@ apply plugin: 'com.android.application' +apply plugin: 'com.google.gms.google-services' +apply plugin: 'com.google.firebase.crashlytics' android { compileSdkVersion this.compileSdkVersion @@ -14,6 +16,9 @@ android { buildFeatures.dataBinding true buildTypes { + debug { + firebaseCrashlytics.mappingFileUploadEnabled false + } release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' @@ -76,6 +81,3 @@ dependencies { // File Provider only fileproviderImplementation project(':fileprovider') } - -// Firebase plug-in -apply plugin: 'com.google.gms.google-services' diff --git a/build.gradle b/build.gradle index 4aeafb75d..38b4e533b 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ buildscript { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.android.tools.build:gradle:4.1.0' classpath 'com.google.gms:google-services:4.3.4' + classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/shared/build.gradle b/shared/build.gradle index 5bd567583..bd92937b0 100644 --- a/shared/build.gradle +++ b/shared/build.gradle @@ -10,13 +10,8 @@ android { } buildTypes { - release { - buildConfigField("boolean", "CRASHLYTICS_ENABLED", "true") - } - debug { - ext.enableCrashlytics = false - buildConfigField("boolean", "CRASHLYTICS_ENABLED", "${ext.enableCrashlytics}") - } + release { buildConfigField("boolean", "CRASHLYTICS_ENABLED", "true") } + debug { buildConfigField("boolean", "CRASHLYTICS_ENABLED", "false") } } kotlinOptions.jvmTarget = "1.8" @@ -54,7 +49,7 @@ dependencies { implementation 'com.google.firebase:firebase-core:17.5.1' implementation 'com.google.firebase:firebase-analytics:17.6.0' implementation 'com.google.firebase:firebase-config:19.2.0' - implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1' + implementation 'com.google.firebase:firebase-crashlytics:17.2.2' implementation 'com.oasisfeng.condom:library:2.5.0' implementation 'com.squareup.okhttp3:okhttp:3.12.1' implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.10.0' diff --git a/shared/src/main/java/com/oasisfeng/island/IslandApplication.java b/shared/src/main/java/com/oasisfeng/island/IslandApplication.java index d3a57a64b..edabe602d 100644 --- a/shared/src/main/java/com/oasisfeng/island/IslandApplication.java +++ b/shared/src/main/java/com/oasisfeng/island/IslandApplication.java @@ -3,8 +3,6 @@ import android.app.Application; import com.oasisfeng.island.analytics.CrashReport; -import com.oasisfeng.island.firebase.FirebaseServiceProxy; -import com.oasisfeng.island.shared.R; /** * For singleton instance purpose only. @@ -23,11 +21,5 @@ public IslandApplication() { CrashReport.initCrashHandler(); } - @Override public void onCreate() { - super.onCreate(); - final String firebase_proxy_host = getString(R.string.firebase_proxy_host); - if (! firebase_proxy_host.isEmpty()) FirebaseServiceProxy.initialize(firebase_proxy_host); - } - private static IslandApplication sInstance; } diff --git a/shared/src/main/java/com/oasisfeng/island/analytics/CrashReport.java b/shared/src/main/java/com/oasisfeng/island/analytics/CrashReport.java index 4f471b0c7..514a8131e 100644 --- a/shared/src/main/java/com/oasisfeng/island/analytics/CrashReport.java +++ b/shared/src/main/java/com/oasisfeng/island/analytics/CrashReport.java @@ -2,14 +2,13 @@ import android.os.Process; -import com.crashlytics.android.core.CrashlyticsCore; +import androidx.annotation.NonNull; + +import com.google.firebase.crashlytics.FirebaseCrashlytics; import com.oasisfeng.android.util.Suppliers; -import com.oasisfeng.island.firebase.FirebaseWrapper; -import com.oasisfeng.island.shared.BuildConfig; import java.util.function.Supplier; -import io.fabric.sdk.android.Fabric; /** * Lazy initializer for crash handler. @@ -18,24 +17,20 @@ */ public abstract class CrashReport { - private static final boolean DISABLED = BuildConfig.DEBUG && ! BuildConfig.CRASHLYTICS_ENABLED; - - static void logException(final Throwable t) { sSingleton.get().logException(t); } + static void logException(final Throwable t) { sSingleton.get().recordException(t); } static void log(final String message) { sSingleton.get().log(message); } - static void setProperty(final String key, final String value) { sSingleton.get().setString(key, value); } - static void setProperty(final String key, final int value) { sSingleton.get().setInt(key, value); } - static void setProperty(final String key, final boolean value) { sSingleton.get().setBool(key, value); } - - private static final Supplier sSingleton = Suppliers.memoize(() -> { - Fabric.with(new Fabric.Builder(FirebaseWrapper.init()).debuggable(BuildConfig.DEBUG) - .kits(new CrashlyticsCore.Builder().disabled(DISABLED).build()).build()); - final CrashlyticsCore instance = CrashlyticsCore.getInstance(); - instance.setInt("user", Process.myUserHandle().hashCode()); // Attach the current (Android) user ID to crash report. - return instance; + static void setProperty(final String key, final String value) { sSingleton.get().setCustomKey(key, value); } + static void setProperty(final String key, final int value) { sSingleton.get().setCustomKey(key, value); } + static void setProperty(final String key, final boolean value) { sSingleton.get().setCustomKey(key, value); } + + private static final Supplier sSingleton = Suppliers.memoize(() -> { + final FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance(); + crashlytics.setCrashlyticsCollectionEnabled(true/*BuildConfig.CRASHLYTICS_ENABLED*/); + crashlytics.setCustomKey("user", Process.myUserHandle().hashCode()); // Attach the current (Android) user ID to crash report. + return crashlytics; }); public static void initCrashHandler() { - if (DISABLED) return; final Thread.UncaughtExceptionHandler current_exception_handler = Thread.getDefaultUncaughtExceptionHandler(); if (! (current_exception_handler instanceof LazyThreadExceptionHandler)) Thread.setDefaultUncaughtExceptionHandler(new LazyThreadExceptionHandler(current_exception_handler)); @@ -43,7 +38,7 @@ public static void initCrashHandler() { private static class LazyThreadExceptionHandler implements Thread.UncaughtExceptionHandler { - @Override public void uncaughtException(final Thread thread, final Throwable e) { + @Override public void uncaughtException(final @NonNull Thread thread, final @NonNull Throwable e) { if (mHandlingUncaughtException) { // Avoid infinite recursion mOriginalHandler.uncaughtException(thread, e); return; diff --git a/shared/src/main/java/com/oasisfeng/island/firebase/FirebaseServiceProxy.kt b/shared/src/main/java/com/oasisfeng/island/firebase/FirebaseServiceProxy.kt deleted file mode 100644 index 2d035e911..000000000 --- a/shared/src/main/java/com/oasisfeng/island/firebase/FirebaseServiceProxy.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.oasisfeng.island.firebase - -import android.util.Log -import io.fabric.sdk.android.services.network.HttpRequest -import okhttp3.OkHttpClient -import okhttp3.internal.huc.OkHttpsURLConnection -import java.net.* - -/** - * Inject reverse proxy for Firebase service hosts. - * - * Created by Oasis on 2019-7-28. - */ -object FirebaseServiceProxy { - - private val FIREBASE_HOSTS = arrayOf( - "api.crashlytics.com", "settings.crashlytics.com", "reports.crashlytics.com", // Crashlytics - "firebaseremoteconfig.googleapis.com" // Remote Config - ) - - @JvmStatic fun initialize(altHost: String) = HttpRequest.setConnectionFactory(object: HttpRequest.ConnectionFactory { - - override fun create(url: URL): HttpURLConnection { - Log.d(TAG, "Create connection: $url") - if (url.host !in FIREBASE_HOSTS) return url.openConnection() as HttpURLConnection - return OkHttpsURLConnection(url, OkHttpClient.Builder().dns { host -> - Log.d(TAG, "Resolving: $host") - InetAddress.getAllByName(host).toMutableList().apply { - if (host in FIREBASE_HOSTS) try { - val altIp = InetAddress.getByName(altHost) - add(InetAddress.getByAddress(host, altIp.address)) - Log.d(TAG, "Attach alt address: ${altIp.hostAddress}") } - catch (e: UnknownHostException) { Log.w(TAG, "Failed to resolve alt host.") }} - }.build()) - } - - override fun create(url: URL, proxy: Proxy): HttpURLConnection { - Log.d(TAG, "Create connection with proxy: $url") - return url.openConnection(proxy) as HttpURLConnection - } - }) - - private const val TAG = "Island.FSP" -} diff --git a/shared/src/main/res/values/values.xml b/shared/src/main/res/values/values.xml deleted file mode 100644 index 04b14ba4d..000000000 --- a/shared/src/main/res/values/values.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file