├── .github └── FUNDING.yml ├── .gitignore ├── .jitpack.yml ├── LICENSE ├── MIT License ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── release │ └── output.json ├── spiderman.jks └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── simple │ │ └── spiderman │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── example │ │ │ └── simple │ │ │ └── spiderman │ │ │ ├── App.java │ │ │ ├── CrashViewModel.kt │ │ │ ├── MainActivity.java │ │ │ └── TestActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── activity_test.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── simple │ └── spiderman │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── spiderman-callback ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── simple │ └── spiderman │ ├── SpiderMan.java │ └── SpiderManInitProvider.java ├── spiderman-libs ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── simple │ │ └── spiderman │ │ └── utils │ │ ├── CrashModel.java │ │ └── SpiderManUtils.java │ └── res │ ├── values-en │ └── strings.xml │ └── values │ ├── attrs.xml │ ├── colors.xml │ ├── colors_dark.xml │ ├── colors_light.xml │ ├── strings.xml │ ├── styles.xml │ └── values.xml ├── spiderman-no-op ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── simple │ │ └── spiderman │ │ └── SpiderMan.java │ └── res │ └── values │ ├── attrs.xml │ ├── colors.xml │ ├── colors_dark.xml │ ├── colors_light.xml │ ├── strings.xml │ ├── styles.xml │ └── values.xml ├── spiderman ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── simple │ │ └── spiderman │ │ ├── CrashActivity.java │ │ ├── SpiderMan.java │ │ ├── SpiderManFileProvider.java │ │ └── SpiderManInitProvider.java │ └── res │ ├── layout │ └── activity_crash.xml │ ├── menu │ └── menu_more.xml │ ├── values-en │ └── strings.xml │ ├── values │ ├── attrs.xml │ ├── colors.xml │ ├── colors_dark.xml │ ├── colors_light.xml │ ├── strings.xml │ ├── styles.xml │ └── values.xml │ └── xml │ └── spiderman_file_paths.xml └── statics ├── crash_info.png ├── crash_info_share.png ├── dark_en.png ├── light_en.png └── q_group.jpg /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: https://simplepeng.github.io/merge_pay_code/ 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | /.idea 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /.jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk11 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Peng Chen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MIT License: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 simplepeng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SpiderMan 2 | 3 | [![](https://jitpack.io/v/simplepeng/SpiderMan.svg)](https://jitpack.io/#simplepeng/SpiderMan)![MIT](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square) ![](https://img.shields.io/badge/API-14%2B-brightgreen?style=flat-square) ![](https://img.shields.io/badge/Size-40k-yellow?style=flat-square) ![](https://img.shields.io/badge/Author-simplepeng-red?style=flat-square) 4 | 5 | 6 | SpiderMan能为您做的事: 7 | 8 | * 在Android手机上自动显示闪退崩溃信息,直接分享给相关开发人员! 9 | * 再也不用担心测试妹妹给你重现怎样操作才能触发闪退崩溃的尴尬! 10 | * 再也不用担心产品给你说哪儿哪儿会闪退崩溃,但是又不能场景还原的无奈! 11 | * 再也不用担心某些国产Rom禁止异常log输出! 12 | * 再也不用担心开发工具异常log信息输出时灵时不灵! 13 | 14 | | Debug环境 | Share | 15 | | :-----------------------------------: | :------------------------------------------------: | 16 | | ![crash_info](statics/crash_info.png) | ![crash_info_share](statics/crash_info_share.png) | 17 | 18 | ## 引入依赖 19 | 20 | 从`v1.1.8`开始使用`jitpack`仓库,记得添加`jitpack`仓库的引用。 21 | 22 | ```groovy 23 | maven { url 'https://jitpack.io' } 24 | ``` 25 | 26 | 在`app`的`build.gradle`引入依赖: 27 | 28 | ```groovy 29 | def spider_man = "v1.2.1" 30 | ``` 31 | 32 | ### 方式一 33 | 34 | ```groovy 35 | debugImplementation "com.github.simplepeng.SpiderMan:spiderman:${spider_man}" 36 | releaseImplementation "com.github.simplepeng.SpiderMan:spiderman-no-op:${spider_man}" 37 | ``` 38 | 39 | ### 方式二 40 | 41 | ```java 42 | implementation "com.github.simplepeng.SpiderMan:spiderman:${spider_man}" 43 | ``` 44 | 45 | 上面`方式一`debug环境有奔溃信息提示,release环境则没有,`方式二`都有,但是记得添加混淆。 46 | 47 | ## 直接显示错误页面 48 | 49 | 有时候可能因为一些特殊环境下才会发生的崩溃很难复现,所以我们不得以会将一些代码放到`try/catch`中运行,这样虽然保证了可以不崩溃,但是当发生崩溃时又会很容易忽略掉错误信息。现在我们可以直接在`catch`代码块中调用`SpiderMan.show(Throwable e)`方法,这样就可以直接显示崩溃提示页面。 50 | 51 | ```java 52 | try { 53 | String text = null; 54 | text.toUpperCase(); 55 | } catch (Exception e) { 56 | SpiderMan.show(e); 57 | } 58 | ``` 59 | 60 | ## Crash回调 61 | 62 | 发生crash时,如果你希望能拿到异常信息,保存到本地或者其他自定义操作,那么你可以使用下面的回调方法。 63 | 64 | ```java 65 | //回调crash 66 | SpiderMan.setOnCrashListener(new SpiderMan.OnCrashListener() { 67 | @Override 68 | public void onCrash(Thread t, Throwable ex) { 69 | saveCrash(t, ex); 70 | } 71 | }); 72 | ``` 73 | 74 | `SpiderManUtils`提供了一些封装好的方法,例如`saveTextToFile`,`parseCrash`,自行按需使用。 75 | 76 | 如果release也需要回调,请使用release回调库,从`1.1.9`开始提供。 77 | 78 | ```groovy 79 | releaseImplementation "com.github.simplepeng.SpiderMan:spiderman-callback:${spider_man}" 80 | ``` 81 | 82 | ## 冲突 83 | 84 | ### androidx 85 | 86 | 项目已经依赖了`androidx.appcompat:appcompat`包,如果产生冲突请使用下面的方式依赖。 87 | 88 | ```groovy 89 | debugImplementation("com.github.simplepeng.SpiderMan:spiderman:${spider_man}") { 90 | exclude group: "androidx.appcompat" 91 | } 92 | releaseImplementation("com.github.simplepeng.SpiderMan:spiderman-no-op:${spider_man}") { 93 | exclude group: "androidx.appcompat" 94 | } 95 | ``` 96 | 97 | ### support 98 | 99 | 项目已经依赖了`com.android.support:appcompat-v7`包,如果产生冲突请使用下面的方式依赖。 100 | 101 | ```groovy 102 | debugImplementation("com.github.simplepeng.SpiderMan:spiderman:${spider_man}") { 103 | exclude group: "com.android.support" 104 | } 105 | 106 | releaseImplementation("com.github.simplepeng.SpiderMan:spiderman-no-op:${spider_man}") { 107 | exclude group: "com.android.support" 108 | } 109 | ``` 110 | 111 | ## 混淆 112 | 113 | ```java 114 | -keep class com.simple.spiderman.** { *; } 115 | -keepnames class com.simple.spiderman.** { *; } 116 | -keep public class * extends android.app.Activity 117 | -keep class * implements Android.os.Parcelable { 118 | public static final Android.os.Parcelable$Creator *; 119 | } 120 | # support 121 | -keep public class * extends android.support.annotation.** { *; } 122 | -keep public class * extends android.support.v4.content.FileProvider 123 | # androidx 124 | -keep public class * extends androidx.annotation.** { *; } 125 | -keep public class * extends androidx.core.content.FileProvider 126 | ``` 127 | 128 | ## 自定义界面样式 129 | 130 | ```java 131 | SpiderMan.setTheme(R.style.SpiderManTheme_Dark); 132 | ``` 133 | 134 | `SpiderMan`内置了两种主题样式`light`和`dark`。 135 | 136 | | light | dark | custom | 137 | | :--------------------------------------------------: | :--------------------------------------------------: | :--------------------------------------------------: | 138 | | ![](https://i.loli.net/2019/02/24/5c726ef04a909.png) | ![](https://i.loli.net/2019/02/24/5c726f0dc7159.png) | ![](https://i.loli.net/2019/02/24/5c72a0f278b9b.png) | 139 | 140 | 所有自定义属性定义在`attrs.xml`中 141 | 142 | * smToolbar:toolbar的背景色 143 | * smToolbarText:toolb title的颜色 144 | * smToolbarShareText:分享文字按钮的颜色 145 | * smContentBackground:toolb下方内容的背景色 146 | * smIdentText:标签名字的颜色 147 | * smDescText:标签描述的颜色 148 | 149 | 具体可以参考`app`中的用法。 150 | 151 | ## 赞助 152 | 153 | 如果您觉得`SpideMan`帮助了您,可选择精准扶贫🙇🙇🙇 154 | 155 | 您的支持是作者继续努力创作的动力😁😁😁 156 | 157 | 萌戳下方链接精准扶贫⤵️⤵️⤵️ 158 | 159 | **[扶贫方式](https://simplepeng.github.io/merge_pay_code/)** 160 | 161 | ## 技术支持Q群:1078185041 162 | 163 | 164 | 165 | ## 版本迭代 166 | 167 | * v1.2.0:解决协程Crash异常信息不对的问题 168 | * v1.1.9:增加`crash-callback`module,升级gradle版本 169 | * v1.1.8:使用`jitpack`仓库 170 | * v1.1.7: 自动初始化 171 | * v1.1.6: 解决view id重名引发的bug 172 | * v1.1.5: 增加`cpu-abi`,`versionCode`,`versionName`输出 173 | * v1.1.4: 切换到androidx 174 | * v1.1.3: change minSdkVersion to 14 175 | * v1.1.2: 解决FileProvider file_path重名bug(bug来源LuckSiege/PictureSelector) 176 | * v1.1.1: 新增直接显示错误页面的方法`SpiderMan.show(Throwable e)`,优化错误类型 177 | * v1.1.0: 增加自定义界面主题和国际化 178 | * v1.0.9: 增加appcompat包冲突解决方案 179 | * v1.0.8: 发现很多小伙伴不会代理异常收集,所以删除了异常回调 180 | * v1.0.7: 删除spiderman-no-op never-crash,优化报错类型显示 181 | * v1.0.6: 增加spiderman-no-op 182 | * v1.0.5: 奔溃文本分享美化排版 183 | * v1.0.4: 崩溃输出改为error级别 184 | * v1.0.3: 增加 拷贝/分享 崩溃文字/图片信息 185 | * v1.0.2: 重构,新增设备信息 186 | * v1.0.1: 去除 allowBackup,label 187 | * v1.0.0: 首次上传 188 | 189 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /.idea -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | compileSdk rootProject.ext.compileSdkVersion 8 | 9 | defaultConfig { 10 | applicationId "example.simple.spiderman" 11 | minSdk rootProject.ext.minSdkVersion 12 | targetSdk rootProject.ext.targetSdkVersion 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | } 17 | 18 | signingConfigs { 19 | release { 20 | storeFile file("spiderman.jks") 21 | storePassword "123456" 22 | keyAlias "spiderman" 23 | keyPassword "123456" 24 | v2SigningEnabled true 25 | } 26 | } 27 | 28 | buildTypes { 29 | release { 30 | minifyEnabled false 31 | signingConfig signingConfigs.release 32 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 33 | } 34 | debug { 35 | minifyEnabled false 36 | signingConfig signingConfigs.release 37 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 38 | } 39 | } 40 | 41 | compileOptions { 42 | sourceCompatibility JavaVersion.VERSION_1_8 43 | targetCompatibility JavaVersion.VERSION_1_8 44 | } 45 | kotlinOptions { 46 | jvmTarget = '11' 47 | } 48 | 49 | } 50 | 51 | dependencies { 52 | implementation fileTree(include: ['*.jar'], dir: 'libs') 53 | implementation 'androidx.appcompat:appcompat:1.0.0' 54 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 55 | implementation 'androidx.core:core-ktx:1.13.1' 56 | implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2") 57 | 58 | // def spider_man = "1.1.7" 59 | // def spider_man_no_op = "1.1.5" 60 | 61 | // implementation project(':spiderman') 62 | 63 | // debugImplementation project(':spiderman') 64 | // releaseImplementation project(':spiderman-no-op') 65 | // debugImplementation project(':spiderman-callback') 66 | 67 | // debugImplementation project(':spiderman-no-op') 68 | 69 | // debugImplementation "com.simple:spiderman:$spider_man" 70 | // releaseImplementation "com.simple:spiderman-no-op:$spider_man_no_op" 71 | 72 | // debugImplementation ("com.simple:spiderman:$spider_man"){ 73 | // exclude group: "com.android.support" 74 | // } 75 | // releaseImplementation ("com.simple:spiderman-no-op:$spider_man"){ 76 | // exclude group: "com.android.support" 77 | // } 78 | 79 | //androidx 80 | // debugImplementation("com.simple:spiderman:$spider_man") { 81 | // exclude group: "androidx.appcompat" 82 | // } 83 | // releaseImplementation("com.simple:spiderman-no-op:$spider_man") { 84 | // exclude group: "androidx.appcompat" 85 | // } 86 | 87 | //开始使用jitpack 88 | def spider_man = "v1.2.1" 89 | debugImplementation "com.github.simplepeng.SpiderMan:spiderman:${spider_man}" 90 | releaseImplementation "com.github.simplepeng.SpiderMan:spiderman-no-op:${spider_man}" 91 | // releaseImplementation "com.github.simplepeng.SpiderMan:spiderman-callback:${spider_man}" 92 | } 93 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | 23 | -keep class com.simple.spiderman.** { *; } 24 | -keepnames class com.simple.spiderman.** { *; } 25 | -keep public class * extends android.app.Activity 26 | -keep public class * extends android.support.annotation.** { *; } 27 | -keep public class * extends android.support.v4.content.FileProvider 28 | -keep class * implements Android.os.Parcelable { 29 | public static final Android.os.Parcelable$Creator *; 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/release/output.json: -------------------------------------------------------------------------------- 1 | [{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] -------------------------------------------------------------------------------- /app/spiderman.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simplepeng/SpiderMan/135bc6d2b0cc4d65276c8f83d39759e3ef9cec45/app/spiderman.jks -------------------------------------------------------------------------------- /app/src/androidTest/java/com/simple/spiderman/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.simple.spiderman; 2 | 3 | import android.content.Context; 4 | import androidx.test.platform.app.InstrumentationRegistry; 5 | import androidx.test.ext.junit.runners.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.simple.spiderman", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/example/simple/spiderman/App.java: -------------------------------------------------------------------------------- 1 | package example.simple.spiderman; 2 | 3 | import android.app.Application; 4 | import android.widget.Toast; 5 | 6 | import com.simple.spiderman.SpiderMan; 7 | import com.simple.spiderman.utils.CrashModel; 8 | import com.simple.spiderman.utils.SpiderManUtils; 9 | 10 | import java.io.File; 11 | 12 | 13 | /** 14 | * author : ChenPeng 15 | * date : 2018/4/21 16 | * description : 17 | */ 18 | public class App extends Application { 19 | 20 | @Override 21 | public void onCreate() { 22 | super.onCreate(); 23 | 24 | //更改主题颜色 25 | SpiderMan.setTheme(R.style.SpiderManTheme_Dark); 26 | 27 | //回调crash 28 | SpiderMan.setOnCrashListener(new SpiderMan.OnCrashListener() { 29 | @Override 30 | public void onCrash(Thread t, Throwable ex) { 31 | saveCrash(t, ex); 32 | } 33 | }); 34 | 35 | } 36 | 37 | private void saveCrash(Thread t, Throwable ex) { 38 | CrashModel model = SpiderManUtils.parseCrash(this, ex); 39 | String time = String.valueOf(System.currentTimeMillis()); 40 | String logPath = this.getApplicationContext().getCacheDir().getAbsolutePath() + File.separator + "log-" + time + ".txt"; 41 | String text = SpiderManUtils.getShareText(this.getApplicationContext(), model); 42 | saveToFile(text, logPath); 43 | // showToast(model.toString()); 44 | } 45 | 46 | public static void saveToFile(String text, String filePath) { 47 | try { 48 | SpiderManUtils.saveTextToFile(text, filePath); 49 | } catch (Throwable e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | 54 | private void showToast(String text) { 55 | Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/example/simple/spiderman/CrashViewModel.kt: -------------------------------------------------------------------------------- 1 | package example.simple.spiderman 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.viewModelScope 5 | import com.simple.spiderman.SpiderMan 6 | import kotlinx.coroutines.Dispatchers 7 | import kotlinx.coroutines.launch 8 | 9 | class CrashViewModel : ViewModel() { 10 | 11 | fun makeCrash() { 12 | // 1 / 0 13 | // val text: String? = null; 14 | // text!!.toUpperCase(); 15 | 16 | viewModelScope.launch(Dispatchers.IO) { 17 | 1 / 0 18 | // val text: String? = null; 19 | // text!!.toUpperCase(); 20 | } 21 | 22 | // thread { 23 | // 1 / 0 24 | // } 25 | } 26 | 27 | fun makeCrashWithTryCatch() { 28 | viewModelScope.launch(Dispatchers.IO) { 29 | try { 30 | 1 / 0 31 | } catch (e: Exception) { 32 | SpiderMan.show(e) 33 | } 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /app/src/main/java/example/simple/spiderman/MainActivity.java: -------------------------------------------------------------------------------- 1 | package example.simple.spiderman; 2 | 3 | import android.os.Bundle; 4 | 5 | import androidx.appcompat.app.AppCompatActivity; 6 | import androidx.lifecycle.ViewModelProvider; 7 | 8 | import android.view.View; 9 | import android.widget.TextView; 10 | 11 | import com.simple.spiderman.SpiderMan; 12 | 13 | 14 | public class MainActivity extends AppCompatActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | CrashViewModel viewModel = new ViewModelProvider(this).get(CrashViewModel.class); 22 | 23 | TextView tv_buildType = findViewById(R.id.tv_buildType); 24 | tv_buildType.setText(BuildConfig.BUILD_TYPE); 25 | findViewById(R.id.btn_crash).setOnClickListener(new View.OnClickListener() { 26 | @Override 27 | public void onClick(View view) { 28 | viewModel.makeCrash(); 29 | // String text = null; 30 | // text.toUpperCase(); 31 | // startActivity(new Intent(MainActivity.this,TestActivity.class)); 32 | } 33 | }); 34 | 35 | findViewById(R.id.btn_show).setOnClickListener(new View.OnClickListener() { 36 | @Override 37 | public void onClick(View view) { 38 | // viewModel.makeCrashWithTryCatch(); 39 | try { 40 | String text = null; 41 | text.toUpperCase(); 42 | } catch (Exception e) { 43 | SpiderMan.show(e); 44 | } 45 | } 46 | }); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/example/simple/spiderman/TestActivity.java: -------------------------------------------------------------------------------- 1 | package example.simple.spiderman; 2 | 3 | import android.os.Bundle; 4 | import androidx.annotation.Nullable; 5 | import androidx.appcompat.app.AppCompatActivity; 6 | 7 | 8 | 9 | public class TestActivity extends AppCompatActivity { 10 | 11 | @Override 12 | protected void onCreate(@Nullable Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_test); 15 | String s = null; 16 | s.toCharArray(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 19 | 20 | 21 | 22 |