Skip to content

Commit d548f84

Browse files
authored
Merge pull request #4 from acious/feature/kotlin-crash-app
Converted firebase crash app from Java to Kotlin
2 parents a6a0fc0 + 4dd99df commit d548f84

File tree

3 files changed

+89
-85
lines changed

3 files changed

+89
-85
lines changed

crash/app/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
23
check.dependsOn 'assembleDebugAndroidTest'
34

45
android {
@@ -40,6 +41,10 @@ dependencies {
4041
testCompile 'junit:junit:4.12'
4142
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
4243
androidTestCompile 'com.android.support.test:runner:0.5'
44+
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
4345
}
4446

4547
apply plugin: 'com.google.gms.google-services'
48+
repositories {
49+
mavenCentral()
50+
}

crash/app/src/main/java/com/google/samples/quickstart/crash/MainActivity.java

-85
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.quickstart.crash
18+
19+
import android.os.Bundle
20+
import android.support.v7.app.AppCompatActivity
21+
import android.util.Log
22+
import android.widget.Button
23+
import android.widget.CheckBox
24+
25+
import com.google.firebase.crash.FirebaseCrash
26+
27+
/**
28+
* This Activity shows the different ways of reporting application crashes.
29+
* - Report caught crashes with Crash.report().
30+
* - Automatically Report uncaught crashes.
31+
*
32+
*
33+
* It also shows how to add log messages to crash reports using Crash.log().
34+
*
35+
*
36+
* Check https://console.firebase.google.com to view and analyze your crash reports.
37+
*
38+
*
39+
* Check https://firebase.google.com/docs/crash/android for more on
40+
* Firebase Crash on Android.
41+
*/
42+
class MainActivity : AppCompatActivity() {
43+
private val TAG = "MainActivity"
44+
45+
override fun onCreate(savedInstanceState: Bundle?) {
46+
super.onCreate(savedInstanceState)
47+
setContentView(R.layout.activity_main)
48+
49+
// Checkbox to indicate when to catch the thrown exception.
50+
val catchCrashCheckBox = findViewById(R.id.catchCrashCheckBox) as CheckBox?
51+
52+
// Button that causes the NullPointerException to be thrown.
53+
(findViewById(R.id.crashButton) as Button).setOnClickListener {
54+
// Log that crash button was clicked. This version of Crash.log() will include the
55+
// message in the crash report as well as show the message in logcat.
56+
FirebaseCrash.logcat(Log.INFO, TAG, "Crash button clicked")
57+
58+
// If catchCrashCheckBox is checked catch the exception and report is using
59+
// Crash.report(). Otherwise throw the exception and let Firebase Crash automatically
60+
// report the crash.
61+
when (catchCrashCheckBox?.isChecked) {
62+
true -> {
63+
try {
64+
throw NullPointerException()
65+
} catch (ex: NullPointerException) {
66+
// [START log_and_report]
67+
FirebaseCrash.logcat(Log.ERROR, TAG, "NPE caught")
68+
FirebaseCrash.report(ex)
69+
// [END log_and_report]
70+
}
71+
}
72+
false -> {
73+
throw NullPointerException()
74+
}
75+
}
76+
}
77+
78+
// Log that the Activity was created. This version of Crash.log() will include the message
79+
// in the crash report but will not be shown in logcat.
80+
// [START log_event]
81+
FirebaseCrash.log("Activity created")
82+
// [END log_event]
83+
}
84+
}

0 commit comments

Comments
 (0)