Last active
December 13, 2022 03:32
-
-
Save hongbeomi/5d3e38f51564199930db5b8fbaed1c10 to your computer and use it in GitHub Desktop.
Firebase Crashlytics 사용 & 글로벌 에러 핸들링
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// references : https://github.com/firebase/firebase-android-sdk/issues/1952#issuecomment-713094146 | |
// error handling page | |
class ErrorActivity : AppCompatActivity() { | |
companion object { | |
private const val ERROR_TEXT = "ERROR_TEXT" | |
fun start(context: Context, errorText: String) = with(context) { | |
val intent = Intent(this, ErrorActivity::class.java).apply { | |
putExtra(ERROR_TEXT, errorText) | |
// applicationContext에서 실행되기 때문에 NEW_TASK 필수. | |
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) | |
} | |
startActivity(intent) | |
} | |
} | |
private val errorText: String by lazy { intent.getStringExtra(ERROR_TEXT).orEmpty() } | |
private lateinit var binding: ErrorActivityBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.error_activity) | |
// TODO debug시에만 표시하도록 | |
binding.tvError.text = errorText | |
} | |
} | |
// error handling ContentProvider | |
class ExceptionHandleProvider : ContentProvider() { | |
override fun onCreate(): Boolean { | |
Thread.setDefaultUncaughtExceptionHandler { _, e -> | |
context?.run { | |
val stringWriter = StringWriter() | |
e.printStackTrace(PrintWriter(stringWriter)) | |
ErrorActivity.start(this, stringWriter.toString()) | |
Runtime.getRuntime().exit(0) | |
} | |
} | |
return true | |
} | |
override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? = | |
null | |
override fun getType(uri: Uri): String? = null | |
override fun insert(uri: Uri, values: ContentValues?): Uri? = null | |
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0 | |
override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int = 0 | |
} | |
// register manifest | |
<activity android:name=".ErrorActivity"/> | |
<!-- Firebase Crashlytics(100)보다 우선 순위를 높게 가져가야 우선적으로 핸들링 실행 가능--> | |
<provider | |
android:name=".ExceptionHandleProvider" | |
android:authorities="${applicationId}.exceptionhandleprovider" | |
android:exported="false" | |
android:initOrder="101"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment