Skip to content

Commit 907ae30

Browse files
committed
1、修复Provider初始化问题
2、完善crash捕获
1 parent 0bacbb8 commit 907ae30

File tree

8 files changed

+62
-11
lines changed

8 files changed

+62
-11
lines changed

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ dependencies {
4141
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
4242
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
4343

44-
implementation 'com.github.Lbyao:AndroidLogUtils:0.0.1'
44+
// implementation 'com.github.Lbyao:AndroidLogUtils:0.1.1'
45+
implementation project(':utils')
4546
}

app/src/main/java/com/liubuyao/androidlogutils/MainActivity.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ package com.liubuyao.androidlogutils
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5-
import com.liubuyao.utils.LogUtils
5+
import android.widget.TextView
6+
import androidx.appcompat.widget.AppCompatButton
7+
import kotlinx.coroutines.GlobalScope
68

79
/**
810
* @author lby
911
*/
1012
class MainActivity : AppCompatActivity() {
13+
14+
val list = mutableListOf<String>()
15+
1116
override fun onCreate(savedInstanceState: Bundle?) {
1217
super.onCreate(savedInstanceState)
1318
setContentView(R.layout.activity_main)
19+
20+
findViewById<AppCompatButton>(R.id.btnTest).setOnClickListener {
21+
findViewById<TextView>(R.id.tvText).text = list[2]
22+
}
23+
1424
}
1525
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6+
android:orientation="vertical"
67
tools:context=".MainActivity">
78

8-
</androidx.constraintlayout.widget.ConstraintLayout>
9+
<TextView
10+
android:id="@+id/tvText"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"/>
13+
14+
<androidx.appcompat.widget.AppCompatButton
15+
android:id="@+id/btnTest"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"/>
18+
19+
</LinearLayout>

utils/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ afterEvaluate {
6363
}
6464

6565
dependencies {
66-
66+
// implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1'
6767
}

utils/src/main/java/com/liubuyao/utils/AppUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object AppUtils {
4242
}
4343
val pm = getApp().packageManager
4444
val packageInfo = pm.getPackageInfo(packageName, 0)
45-
return if (packageInfo == null) "" else packageInfo.packageName
45+
return if (packageInfo == null) "" else packageInfo.versionName
4646
}
4747

4848
fun getVersionCode(): Int {

utils/src/main/java/com/liubuyao/utils/CrashUtils.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package com.liubuyao.utils
55
*/
66
object CrashUtils {
77

8+
val DEFAULT = Thread.getDefaultUncaughtExceptionHandler()
9+
810
/**
911
* 初始化
1012
*/
@@ -16,7 +18,7 @@ object CrashUtils {
1618
* 初始化
1719
*/
1820
fun init(path: String, onCrashListener: OnCrashListener?) {
19-
val realPath = if (path.isNotEmpty()) {
21+
val realPath = if (MyUtils.isSpace(path)) {
2022
""
2123
} else if (path.endsWith(Constants.separtor)) {
2224
path
@@ -42,9 +44,15 @@ object CrashUtils {
4244
return Thread.UncaughtExceptionHandler { thread, throwable ->
4345
val crashDateTime = MyUtils.getNowDateByCrash()
4446
val throwableInfoToString = MyUtils.getThrowableInfoToString(throwable)
45-
val phoneInfo = MyUtils.getPhoneInfo()
46-
MyUtils.writeToFile("$path$crashDateTime.txt",phoneInfo+throwableInfoToString,false)
47+
val phoneInfo = MyUtils.getPhoneInfo(thread)
48+
MyUtils.writeToFile(
49+
"${path}crash_log_${crashDateTime}.txt",
50+
phoneInfo + throwableInfoToString,
51+
false
52+
)
4753
onCrashListener?.onCrash(thread, throwable)
54+
//需要用默认的接收器处理,否则会卡死一段时间
55+
DEFAULT?.uncaughtException(thread, throwable)
4856
}
4957
}
5058

utils/src/main/java/com/liubuyao/utils/MyUtils.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ object MyUtils {
4141
*/
4242
fun getPhoneInfo(): String {
4343
val map = hashMapOf<String, String>()
44-
map["VersionName"] = AppUtils.getVersionName()
4544
map["VersionCode"] = "" + AppUtils.getVersionCode()
45+
map["SDKVersionCode"] = AppUtils.getSDKVersionCode().toString()
4646
map["SDKVersionName"] = "" + AppUtils.getSDKVersionName()
4747
map["Manufacturer"] = "" + AppUtils.getManufacturer()
4848
map["Model"] = AppUtils.getModel()
@@ -55,6 +55,27 @@ object MyUtils {
5555
return str.toString()
5656
}
5757

58+
/**
59+
* 获取手机信息
60+
*/
61+
fun getPhoneInfo(thread: Thread): String {
62+
val map = hashMapOf<String, String>()
63+
map["VersionCode"] = "" + AppUtils.getVersionCode()
64+
map["VersionName"] = "" + AppUtils.getVersionName()
65+
map["SDKVersionCode"] = AppUtils.getSDKVersionCode().toString()
66+
map["SDKVersionName"] = "" + AppUtils.getSDKVersionName()
67+
map["Manufacturer"] = "" + AppUtils.getManufacturer()
68+
map["Model"] = AppUtils.getModel()
69+
map["ABI"] = AppUtils.getABI()
70+
map["Thread"] = thread.name
71+
val str = StringBuilder("=".repeat(20) + "PHONE_INFO_HEAD" + "=".repeat(20) + "\n")
72+
for (info in map) {
73+
str.append(info.key).append(" ====> ").append(info.value).append("\n")
74+
}
75+
str.append("=".repeat(20) + "PHONE_INFO_HEAD_END" + "=".repeat(20) + "\n")
76+
return str.toString()
77+
}
78+
5879
fun isSpace(string: String?): Boolean {
5980
return StringUtils.isSpace(string)
6081
}

utils/src/main/java/com/liubuyao/utils/provider/SuperUtilsProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import com.liubuyao.utils.MyUtils
1515
*/
1616
class SuperUtilsProvider : ContentProvider() {
1717
override fun onCreate(): Boolean {
18-
CrashUtils.init(MyUtils.getExternalAppPath())
1918
AppUtils.init(context)
19+
CrashUtils.init(MyUtils.getExternalAppPath())
2020
return false
2121
}
2222

0 commit comments

Comments
 (0)