Skip to content

Commit

Permalink
简单封装 CrashUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
yangkun19921001 committed Sep 17, 2019
1 parent bc89e03 commit 539824b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 19 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/com/devyk/ykcrash/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String nativePath = Environment.getExternalStorageDirectory() + "/T01/native/";
String nativePath = Environment.getExternalStorageDirectory() + "/T01/nativeCrash/";
File natPath = new File(nativePath);
if (!natPath.exists())
natPath.mkdirs();
CrashUtils.initCrash(nativePath);

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

CrashUtils.initNativeCrash(getApplicationContext(), nativePath);

}

Expand Down
22 changes: 11 additions & 11 deletions crash_breakpad_build/src/main/cpp/breakpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,7 @@ bool DumpCallback(const google_breakpad::MinidumpDescriptor &descriptor,
return succeeded;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_devyk_crash_1module_CrashUtils_initBreakpadNative(JNIEnv *env, jclass type,
jstring path_) {
const char *path = env->GetStringUTFChars(path_, 0);

// TODO
google_breakpad::MinidumpDescriptor descriptor(path);
static google_breakpad::ExceptionHandler eh(descriptor, NULL, DumpCallback, NULL, true, -1);

env->ReleaseStringUTFChars(path_, path);
}

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
Expand All @@ -43,3 +32,14 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_VERSION_1_6;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_devyk_crash_1module_inter_NativeCrashImp_initBreakpadNative(JNIEnv *env, jclass type,
jstring path_) {
const char *path = env->GetStringUTFChars(path_, 0);

// TODO
google_breakpad::MinidumpDescriptor descriptor(path);
static google_breakpad::ExceptionHandler eh(descriptor, NULL, DumpCallback, NULL, true, -1);
env->ReleaseStringUTFChars(path_, path);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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.JavaCrashImp;
import com.devyk.crash_module.inter.NativeCrashImp;

import java.io.File;

/**
* <pre>
* author : devyk on 2019-09-17 00:34
Expand All @@ -11,13 +20,23 @@
*/
public class CrashUtils {

static {
System.loadLibrary("breakpad-core");
}
/**
* Java 层 崩溃实现
*/
private static ICrash javaCrash = new JavaCrashImp();

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

public static void initCrash(String path) {
initBreakpadNative(path);
/**
* native 崩溃日志
*/
public static void initNativeCrash(Context context, String nativePath) {
if (!TextUtils.isEmpty(nativePath) || new File(nativePath).exists())
nativeCrashImp.init(context, nativePath);
}

private static native void initBreakpadNative(String path);

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

import android.content.Context;

/**
* <pre>
* author : devyk on 2019-09-18 01:29
* blog : https://juejin.im/user/578259398ac2470061f3a3fb/posts
* github : https://github.com/yangkun19921001
* mailbox : [email protected]
* desc : This is ICrash
* </pre>
*/
public interface ICrash {

void init(Context context,String logPath);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.devyk.crash_module.inter;

import android.content.Context;

/**
* <pre>
* author : devyk on 2019-09-18 01:31
* blog : https://juejin.im/user/578259398ac2470061f3a3fb/posts
* github : https://github.com/yangkun19921001
* mailbox : [email protected]
* desc : This is JavaCrashImp
* </pre>
*/
public class JavaCrashImp implements ICrash {
@Override
public void init(Context context, String logPath) {

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

import android.content.Context;

import com.devyk.crash_module.CrashUtils;

/**
* <pre>
* author : devyk on 2019-09-18 01:30
* blog : https://juejin.im/user/578259398ac2470061f3a3fb/posts
* github : https://github.com/yangkun19921001
* mailbox : [email protected]
* desc : This is NativeCrashImp
* </pre>
*/
public class NativeCrashImp implements ICrash {

static {
System.loadLibrary("breakpad-core");
}

private static native void initBreakpadNative(String path);

@Override
public void init(Context context, String logPath) {
initBreakpadNative(logPath);
}
}

0 comments on commit 539824b

Please sign in to comment.