├── .gitignore ├── AndroidCrash.iml ├── README.md ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── githang │ │ └── androidcrash │ │ └── app │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── githang │ │ └── androidcrash │ │ └── app │ │ ├── AppManager.java │ │ ├── MainActivity.java │ │ └── MyApplication.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── layout │ └── activity_main.xml │ └── values │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle-app.setting ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── library.iml ├── libs │ ├── activation.jar │ ├── additionnal.jar │ └── mail.jar ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── githang │ │ └── androidcrash │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── githang │ │ └── androidcrash │ │ ├── AndroidCrash.java │ │ ├── log │ │ ├── CrashCatcher.java │ │ ├── CrashListener.java │ │ └── LogWriter.java │ │ ├── reporter │ │ ├── AbstractCrashHandler.java │ │ ├── httpreporter │ │ │ ├── CrashHttpReporter.java │ │ │ └── SimpleMultipartEntity.java │ │ └── mailreporter │ │ │ ├── CrashEmailReporter.java │ │ │ └── LogMail.java │ │ └── util │ │ └── AssertUtil.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ └── values │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | *.iml 3 | .idea/ 4 | .DS_Store 5 | 6 | # Built application files 7 | *.apk 8 | *.ap_ 9 | 10 | # Files for the Dalvik VM 11 | *.dex 12 | 13 | # Java class files 14 | *.class 15 | 16 | # Generated files 17 | bin/ 18 | gen/ 19 | 20 | # Gradle files 21 | .gradle/ 22 | build/ 23 | 24 | # Local configuration file (sdk path, etc) 25 | local.properties 26 | 27 | # Proguard folder generated by Eclipse 28 | proguard/ 29 | -------------------------------------------------------------------------------- /AndroidCrash.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PLEASE NOTE, THIS PROJECT IS NO LONGER BEING MAINTAINED 2 | --- 3 | *** 4 | 5 | android-crash 6 | ============= 7 | 8 | android crash 是我写的一个Android程序崩溃信息处理框架。通过它,可以在程序崩溃时收集崩溃信息并用你指定的方式发送出来。 9 | 10 | 在本框架中,我实现了邮件及HTTP POST请求的发送方式。如果要采用其他方式,可以继承AbstractCrashReportHandler类并实现其抽象方法。 11 | 12 | 使用本框架的方法只需要两个步骤。 13 | 1、添加依赖: 14 | 在repository中添加jcenter。 15 | ```groovy 16 | repository { 17 | jcenter() // or mavenCentral() 18 | } 19 | ``` 20 | 在dependencies中添加如下依赖。 21 | ```groovy 22 | 23 | compile 'com.githang:android-crash:1.0' 24 | ``` 25 | 26 | 2、写一个类,继承自Application,并在AndroidManifest.xml中指定。然后在onCreate方法中添加如下代码: 27 | 28 | 使用E-mail方式,需要添加activation.jar, additionnal.jar, mail.jar 这三个jar包,可以从本项目的libs文件夹中获取。使用HTTP方式则不需要其他依赖库。 29 | 30 | ```java 31 | @Override 32 | public void onCreate() { 33 | super.onCreate(); 34 | 35 | initHttpReporter(); 36 | } 37 | 38 | /** 39 | * 使用EMAIL发送日志 40 | */ 41 | private void initEmailReporter() { 42 | CrashEmailReporter reporter = new CrashEmailReporter(this); 43 | reporter.setReceiver("你的接收邮箱"); 44 | reporter.setSender("irain_log@163.com"); 45 | reporter.setSendPassword("xxxxxxxx"); 46 | reporter.setSMTPHost("smtp.163.com"); 47 | reporter.setPort("465"); 48 | AndroidCrash.getInstance().setCrashReporter(reporter).init(this); 49 | } 50 | 51 | /** 52 | * 使用HTTP发送日志 53 | */ 54 | private void initHttpReporter() { 55 | CrashHttpReporter reporter = new CrashHttpReporter(this) { 56 | /** 57 | * 重写此方法,可以弹出自定义的崩溃提示对话框,而不使用系统的崩溃处理。 58 | * @param thread 59 | * @param ex 60 | */ 61 | @Override 62 | public void closeApp(Thread thread, Throwable ex) { 63 | final Activity activity = AppManager.currentActivity(); 64 | Toast.makeText(activity, "发生异常,正在退出", Toast.LENGTH_SHORT).show(); 65 | // 自定义弹出对话框 66 | new AlertDialog.Builder(activity). 67 | setMessage("程序发生异常,现在退出"). 68 | setPositiveButton("确定", new DialogInterface.OnClickListener() { 69 | @Override 70 | public void onClick(DialogInterface dialog, int which) { 71 | AppManager.AppExit(activity); 72 | } 73 | }).create().show(); 74 | Log.d("MyApplication", "thead:" + Thread.currentThread().getName()); 75 | } 76 | }; 77 | reporter.setUrl("http://crashreport.jd-app.com/your_receiver").setFileParam("fileName") 78 | .setToParam("to").setTo("你的接收邮箱") 79 | .setTitleParam("subject").setBodyParam("message"); 80 | reporter.setCallback(new CrashHttpReporter.HttpReportCallback() { 81 | @Override 82 | public boolean isSuccess(int i, String s) { 83 | return s.endsWith("ok"); 84 | } 85 | }); 86 | AndroidCrash.getInstance().setCrashReporter(reporter).init(this); 87 | } 88 | 89 | ``` 90 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 21 5 | buildToolsVersion "21.1.0" 6 | 7 | defaultConfig { 8 | applicationId "com.githang.androidcrash.app" 9 | minSdkVersion 8 10 | targetSdkVersion 21 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | lintOptions { 21 | abortOnError false 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | compile project(':library') 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\android-sdk-windows/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/githang/androidcrash/app/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.githang.androidcrash.app; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/githang/androidcrash/app/AppManager.java: -------------------------------------------------------------------------------- 1 | package com.githang.androidcrash.app; 2 | 3 | import android.app.Activity; 4 | import android.app.ActivityManager; 5 | import android.content.Context; 6 | 7 | import java.util.Stack; 8 | 9 | /** 10 | * 应用程序Activity管理类:用于Activity管理和应用程序退出 11 | * 12 | * @author liux (http://my.oschina.net/liux) 13 | * @version 1.0 14 | * @created 2012-3-21 15 | */ 16 | public class AppManager { 17 | private static Stack activityStack = new Stack(); 18 | 19 | /** 20 | * 添加Activity到堆栈 21 | */ 22 | public static void addActivity(Activity activity) { 23 | activityStack.push(activity); 24 | } 25 | 26 | /** 27 | * 获取当前Activity(堆栈中最后一个压入的) 28 | */ 29 | public static Activity currentActivity() { 30 | return activityStack.lastElement(); 31 | } 32 | 33 | /** 34 | * 结束当前Activity(堆栈中最后一个压入的) 35 | */ 36 | public static void finishCurrentActivity() { 37 | Activity activity = activityStack.pop(); 38 | activity.finish(); 39 | } 40 | 41 | /** 42 | * 结束指定的Activity 43 | */ 44 | public static void finishActivity(Activity activity) { 45 | if (activity != null) { 46 | activityStack.remove(activity); 47 | if(!activity.isFinishing()) { 48 | activity.finish(); 49 | } 50 | } 51 | } 52 | 53 | /** 54 | * 结束指定类名的Activity 55 | */ 56 | public static void finishActivity(Class cls) { 57 | for (Activity activity : activityStack) { 58 | if (activity.getClass().equals(cls)) { 59 | finishActivity(activity); 60 | } 61 | } 62 | } 63 | 64 | /** 65 | * 结束所有Activity 66 | */ 67 | public static void finishAllActivity() { 68 | for (Activity activity : activityStack) { 69 | if (activity != null) { 70 | activity.finish(); 71 | } 72 | } 73 | activityStack.clear(); 74 | } 75 | 76 | /** 77 | * 退出应用程序 78 | */ 79 | public static void AppExit(Context context) { 80 | try { 81 | finishAllActivity(); 82 | ActivityManager manager = (ActivityManager) context 83 | .getSystemService(Context.ACTIVITY_SERVICE); 84 | manager.killBackgroundProcesses(context.getPackageName()); 85 | System.exit(0); 86 | } catch (Exception e) { 87 | e.printStackTrace(); 88 | } 89 | } 90 | } -------------------------------------------------------------------------------- /app/src/main/java/com/githang/androidcrash/app/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.githang.androidcrash.app; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | 7 | /** 8 | * User: Geek_Soledad(msdx.android@qq.com) 9 | * Date: 2014-11-03 10 | * Time: 23:25 11 | * FIXME 12 | */ 13 | public class MainActivity extends Activity { 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | this.setContentView(R.layout.activity_main); 19 | findViewById(R.id.bt_crash).setOnClickListener(new View.OnClickListener() { 20 | @Override 21 | public void onClick(View v) { 22 | throw new NullPointerException("Test"); 23 | } 24 | }); 25 | AppManager.addActivity(this); 26 | } 27 | 28 | @Override 29 | protected void onDestroy() { 30 | super.onDestroy(); 31 | AppManager.finishActivity(this); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/githang/androidcrash/app/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.githang.androidcrash.app; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.app.Application; 6 | import android.content.DialogInterface; 7 | import android.util.Log; 8 | import android.widget.Toast; 9 | 10 | import com.githang.androidcrash.AndroidCrash; 11 | import com.githang.androidcrash.reporter.httpreporter.CrashHttpReporter; 12 | import com.githang.androidcrash.reporter.mailreporter.CrashEmailReporter; 13 | 14 | /** 15 | * User: Geek_Soledad(msdx.android@qq.com) 16 | * Date: 2014-11-03 17 | * Time: 23:26 18 | * FIXME 19 | */ 20 | public class MyApplication extends Application{ 21 | 22 | @Override 23 | public void onCreate() { 24 | super.onCreate(); 25 | 26 | initHttpReporter(); 27 | } 28 | 29 | /** 30 | * 使用EMAIL发送日志 31 | */ 32 | private void initEmailReporter() { 33 | CrashEmailReporter reporter = new CrashEmailReporter(this); 34 | reporter.setReceiver("你的接收邮箱"); 35 | reporter.setSender("你的发送邮箱"); 36 | reporter.setSendPassword("xxxxxxxx"); 37 | reporter.setSMTPHost("smtp.163.com"); 38 | reporter.setPort("465"); 39 | AndroidCrash.getInstance().setCrashReporter(reporter).init(this); 40 | } 41 | 42 | /** 43 | * 使用HTTP发送日志 44 | */ 45 | private void initHttpReporter() { 46 | CrashHttpReporter reporter = new CrashHttpReporter(this) { 47 | /** 48 | * 重写此方法,可以弹出自定义的崩溃提示对话框,而不使用系统的崩溃处理。 49 | * @param thread 50 | * @param ex 51 | */ 52 | @Override 53 | public void closeApp(Thread thread, Throwable ex) { 54 | final Activity activity = AppManager.currentActivity(); 55 | Toast.makeText(activity, "发生异常,正在退出", Toast.LENGTH_SHORT).show(); 56 | // 自定义弹出对话框 57 | new AlertDialog.Builder(activity). 58 | setMessage("程序发生异常,现在退出"). 59 | setPositiveButton("确定", new DialogInterface.OnClickListener() { 60 | @Override 61 | public void onClick(DialogInterface dialog, int which) { 62 | AppManager.AppExit(activity); 63 | } 64 | }).create().show(); 65 | Log.d("MyApplication", "thead:" + Thread.currentThread().getName()); 66 | } 67 | }; 68 | reporter.setUrl("接收你请求的API").setFileParam("fileName") 69 | .setToParam("to").setTo("你的接收邮箱") 70 | .setTitleParam("subject").setBodyParam("message"); 71 | reporter.setCallback(new CrashHttpReporter.HttpReportCallback() { 72 | @Override 73 | public boolean isSuccess(int i, String s) { 74 | return s.endsWith("ok"); 75 | } 76 | }); 77 | AndroidCrash.getInstance().setCrashReporter(reporter).init(this); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/msdx/android-crash/3773a1b38e4cf1864551cfb6271b87047a1ea3e5/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/msdx/android-crash/3773a1b38e4cf1864551cfb6271b87047a1ea3e5/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/msdx/android-crash/3773a1b38e4cf1864551cfb6271b87047a1ea3e5/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/msdx/android-crash/3773a1b38e4cf1864551cfb6271b87047a1ea3e5/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 |