Skip to content

Commit

Permalink
框架添加 Java native 层日志捕获,优化框架结构
Browse files Browse the repository at this point in the history
  • Loading branch information
yangkun19921001 committed Sep 18, 2019
1 parent b4cf593 commit 42fb78e
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 71 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation rootProject.ext.dependencies["recyclerview-v7"]
implementation rootProject.ext.dependencies["appcompat-v7"]
// implementation project(path: ':crash_breakpad_build')
implementation 'com.github.yangkun19921001:YKCrash:1.0.1'
implementation project(path: ':crash_breakpad_build')
// implementation 'com.github.yangkun19921001:YKCrash:1.0.1'
}
21 changes: 16 additions & 5 deletions app/src/main/java/com/devyk/ykcrash/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import com.devyk.crash_module.CrashUtils;
import com.devyk.crash_module.Crash;
import com.devyk.crash_module.inter.JavaCrashUtils;

import java.io.File;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements JavaCrashUtils.OnCrashListener {
static {
System.loadLibrary("crash-lib");
}
Expand All @@ -19,17 +21,21 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String nativePath = Environment.getExternalStorageDirectory() + "/T01/nativeCrash";
String nativePath = Environment.getExternalStorageDirectory() + "/CRASH/nativeCrash";
File natPath = new File(nativePath);
if (!natPath.exists())
natPath.mkdirs();

String javaPath = Environment.getExternalStorageDirectory() + "/T01/javaCrash";
String javaPath = Environment.getExternalStorageDirectory() + "/CRASH/javaCrash";
File javPath = new File(javaPath);
if (!javPath.exists())
javPath.mkdirs();

CrashUtils.initNativeCrash(getApplicationContext(), nativePath);
//框架初始化
new Crash.CrashBuild(getApplicationContext())
.nativeCrashPath(nativePath)
.javaCrashPath(javaPath, this)
.build();

}

Expand All @@ -43,4 +49,9 @@ public void nativeCrash(View view) {
public void javaCrash(View view) {
System.out.println(1 / 0);
}

@Override
public void onCrash(String crashInfo, Throwable e) {
Log.d("MainActivity", e.getMessage());
}
}
1 change: 1 addition & 0 deletions crash_breakpad_build/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.devyk.crash_module;

import android.content.Context;
import android.text.TextUtils;

import com.devyk.crash_module.inter.ICrash;
import com.devyk.crash_module.inter.JavaCrashUtils;
import com.devyk.crash_module.inter.NativeCrashImp;

import java.io.File;

/**
* <pre>
* author : devyk on 2019-09-17 00:34
* blog : https://juejin.im/user/578259398ac2470061f3a3fb/posts
* github : https://github.com/yangkun19921001
* mailbox : [email protected]
* desc : This is Crash
* </pre>
*/
public class Crash {


/**
* native 崩溃实现
*/
private ICrash nativeCrashImp = new NativeCrashImp();

/**
* 上下文
*/
private Context context = null;


/**
* native 崩溃日志
*/
private void initNativeCrash(Context context, String nativePath) {
if (!TextUtils.isEmpty(nativePath) &&
new File(nativePath).exists())
nativeCrashImp.init(context, nativePath);
}

private void initJavaCrash(Context context, String javaPath, JavaCrashUtils.OnCrashListener onCrashListener) {
if (context != null &&
!TextUtils.isEmpty(javaPath) &&
new File(javaPath).exists() &&
onCrashListener != null)
JavaCrashUtils.getInstance().init(context, javaPath, onCrashListener);
}


public static class CrashBuild {
private String javaCrashPath;
private String nativeCrashPath;
private Context context;
private JavaCrashUtils.OnCrashListener onCrashListener;

public CrashBuild(Context context) {
this.context = context;
}

public CrashBuild javaCrashPath(String path, JavaCrashUtils.OnCrashListener onCrashListener) {
this.javaCrashPath = path;
this.onCrashListener = onCrashListener;
return this;
}

public CrashBuild nativeCrashPath(String path) {
this.nativeCrashPath = path;
return this;
}

public void build() {
Crash crash = new Crash();
crash.context = this.context;
crash.initNativeCrash(this.context, nativeCrashPath);
crash.initJavaCrash(this.context, this.javaCrashPath, this.onCrashListener);
}
}


}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
*/
public interface ICrash {

void init(Context context,String logPath);
void init(Context context, String logPath);

}

This file was deleted.

Loading

0 comments on commit 42fb78e

Please sign in to comment.