From a0b7291cdd6c9887be5a25f87e86215f31ed4fb7 Mon Sep 17 00:00:00 2001 From: Li Linfeng Date: Tue, 31 Mar 2020 12:04:35 +0800 Subject: [PATCH] add crash and analytics --- app/build.gradle | 5 ++++ app/src/main/cpp/CMakeLists.txt | 2 ++ app/src/main/cpp/crash/CMakeLists.txt | 26 +++++++++++++++++ app/src/main/cpp/crash/CrashHandler.cpp | 29 +++++++++++++++++++ .../celestia/mobilecelestia/MainActivity.kt | 19 +++++++++++- .../mobilecelestia/utils/CrashHandler.java | 5 ++++ 6 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 app/src/main/cpp/crash/CMakeLists.txt create mode 100644 app/src/main/cpp/crash/CrashHandler.cpp create mode 100644 app/src/main/java/space/celestia/mobilecelestia/utils/CrashHandler.java diff --git a/app/build.gradle b/app/build.gradle index b9193813..7c71ec8a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -79,6 +79,11 @@ dependencies { implementation 'com.squareup.retrofit2:converter-gson:2.5.0' implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' implementation 'com.kaopiz:kprogresshud:1.2.0' + + def appCenterSdkVersion = '3.1.0' + implementation "com.microsoft.appcenter:appcenter-analytics:${appCenterSdkVersion}" + implementation "com.microsoft.appcenter:appcenter-crashes:${appCenterSdkVersion}" + testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 1cc06fe9..fe7830d4 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -5,6 +5,8 @@ cmake_minimum_required(VERSION 3.4.1) +add_subdirectory( crash ) + # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. diff --git a/app/src/main/cpp/crash/CMakeLists.txt b/app/src/main/cpp/crash/CMakeLists.txt new file mode 100644 index 00000000..79e82a6b --- /dev/null +++ b/app/src/main/cpp/crash/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.4.1) + +set(THIRD_PARTY_DIR "${CMAKE_SOURCE_DIR}/libs") +set(THIRD_PARTY_LIB_DIR "${THIRD_PARTY_DIR}/libs") +set(THIRD_PARTY_INCLUDE_DIR "${THIRD_PARTY_DIR}/include") + +include_directories( + ${THIRD_PARTY_INCLUDE_DIR}/breakpad/breakpad +) + +add_library(breakpad STATIC IMPORTED) +set_target_properties(breakpad PROPERTIES IMPORTED_LOCATION ${THIRD_PARTY_LIB_DIR}/${CMAKE_ANDROID_ARCH_ABI}/libbreakpad_client.a) + +find_library( + log-lib + log ) + +add_library( + nativecrashhandler + SHARED + CrashHandler.cpp ) + +target_link_libraries( + nativecrashhandler + breakpad + ${log-lib} ) diff --git a/app/src/main/cpp/crash/CrashHandler.cpp b/app/src/main/cpp/crash/CrashHandler.cpp new file mode 100644 index 00000000..a9ab0e9c --- /dev/null +++ b/app/src/main/cpp/crash/CrashHandler.cpp @@ -0,0 +1,29 @@ +#include "client/linux/handler/exception_handler.h" +#include "client/linux/handler/minidump_descriptor.h" +#include +#include + +/* + * Triggered automatically after an attempt to write a minidump file to the breakpad folder. + */ +static bool dumpCallback(const google_breakpad::MinidumpDescriptor &descriptor, + void *context, + bool succeeded) { + + // Allow system to log the native stack trace. + __android_log_print(ANDROID_LOG_INFO, "CelestiaCrashReporter", + "Wrote breakpad minidump at %s succeeded=%d\n", descriptor.path(), + succeeded); + return false; +} + +extern "C" +JNIEXPORT void JNICALL +Java_space_celestia_mobilecelestia_utils_CrashHandler_setupNativeCrashesListener(JNIEnv *env, +jclass clazz, + jstring path) { + const char *dumpPath = (char *) env->GetStringUTFChars(path, NULL); + google_breakpad::MinidumpDescriptor descriptor(dumpPath); + new google_breakpad::ExceptionHandler(descriptor, NULL, dumpCallback, NULL, true, -1); + env->ReleaseStringUTFChars(path, dumpPath); +} \ No newline at end of file diff --git a/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt b/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt index 56d01282..98eae5ce 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt @@ -20,6 +20,9 @@ import androidx.fragment.app.Fragment import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.kaopiz.kprogresshud.KProgressHUD +import com.microsoft.appcenter.AppCenter +import com.microsoft.appcenter.analytics.Analytics +import com.microsoft.appcenter.crashes.Crashes import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -85,6 +88,17 @@ class MainActivity : AppCompatActivity(), override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + AppCenter.start( + application, "d1108985-aa25-4fb5-9269-31a70a87d28e", + Analytics::class.java, Crashes::class.java + ) + + Crashes.getMinidumpDirectory().thenAccept { path -> + if (path != null) { + CrashHandler.setupNativeCrashesListener(path) + } + } + window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) AppStatusReporter.shared().register(this) @@ -816,7 +830,10 @@ class MainActivity : AppCompatActivity(), private const val CELESTIA_CFG_NAME = "celestia.cfg" private const val CELESTIA_EXTRA_FOLDER_NAME = "CelestiaResources/extras" - private const val TAG = "MainActivity" + + init { + System.loadLibrary("nativecrashhandler") + } } } diff --git a/app/src/main/java/space/celestia/mobilecelestia/utils/CrashHandler.java b/app/src/main/java/space/celestia/mobilecelestia/utils/CrashHandler.java new file mode 100644 index 00000000..4e2c5794 --- /dev/null +++ b/app/src/main/java/space/celestia/mobilecelestia/utils/CrashHandler.java @@ -0,0 +1,5 @@ +package space.celestia.mobilecelestia.utils; + +public class CrashHandler { + public native static void setupNativeCrashesListener(String path); +}