Skip to content

Commit

Permalink
add crash and analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Mar 31, 2020
1 parent 46ab256 commit a0b7291
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/cpp/crash/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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} )
29 changes: 29 additions & 0 deletions app/src/main/cpp/crash/CrashHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "client/linux/handler/exception_handler.h"
#include "client/linux/handler/minidump_descriptor.h"
#include <jni.h>
#include <android/log.h>

/*
* 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);
}
19 changes: 18 additions & 1 deletion app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package space.celestia.mobilecelestia.utils;

public class CrashHandler {
public native static void setupNativeCrashesListener(String path);
}

0 comments on commit a0b7291

Please sign in to comment.