feat: sentry crash report

This commit is contained in:
xtaodada 2024-05-07 09:11:53 +08:00
parent 6fa3c687f0
commit 6ac0c8b7d6
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
10 changed files with 223 additions and 0 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@
.vscode/ .vscode/
build/ build/
local.properties local.properties
sentry.properties
obj/ obj/
service_account_credentials.json service_account_credentials.json
__pycache__/ __pycache__/

View File

@ -1,5 +1,9 @@
import io.sentry.android.gradle.extensions.InstrumentationFeature
import io.sentry.android.gradle.instrumentation.logcat.LogcatLevel
apply plugin: "com.android.application" apply plugin: "com.android.application"
apply plugin: "kotlin-android" apply plugin: "kotlin-android"
apply plugin: "io.sentry.android.gradle"
repositories { repositories {
maven { maven {
@ -43,6 +47,8 @@ configurations {
def keystorePwd = null def keystorePwd = null
def alias = null def alias = null
def pwd = null def pwd = null
def sentry_token = System.getenv("SENTRY_AUTH_TOKEN")
def sentry_upload = project.rootProject.file("sentry.properties").exists()
def disableCMakeRelWithDebInfo = System.getenv("COMPILE_NATIVE") == null def disableCMakeRelWithDebInfo = System.getenv("COMPILE_NATIVE") == null
Properties properties Properties properties
@ -59,6 +65,7 @@ if (properties != null) {
keystorePwd = properties.getProperty("KEYSTORE_PASS") keystorePwd = properties.getProperty("KEYSTORE_PASS")
alias = properties.getProperty("ALIAS_NAME") alias = properties.getProperty("ALIAS_NAME")
pwd = properties.getProperty("ALIAS_PASS") pwd = properties.getProperty("ALIAS_PASS")
sentry_token = properties.getProperty("SENTRY_AUTH_TOKEN", sentry_token)
} }
keystorePwd = keystorePwd ?: System.getenv("KEYSTORE_PASS") keystorePwd = keystorePwd ?: System.getenv("KEYSTORE_PASS")
@ -366,3 +373,139 @@ android {
} }
} }
sentry {
// Disables or enables debug log output, e.g. for for sentry-cli.
// Default is disabled.
debug = false
// The slug of the Sentry organization to use for uploading proguard mappings/source contexts.
org = "pagermaid"
// The slug of the Sentry project to use for uploading proguard mappings/source contexts.
projectName = "nagram"
// The authentication token to use for uploading proguard mappings/source contexts.
// WARNING: Do not expose this token in your build.gradle files, but rather set an environment
// variable and read it into this property.
authToken = sentry_token
// The url of your Sentry instance. If you're using SAAS (not self hosting) you do not have to
// set this. If you are self hosting you can set your URL here
url = null
// Disables or enables the handling of Proguard mapping for Sentry.
// If enabled the plugin will generate a UUID and will take care of
// uploading the mapping to Sentry. If disabled, all the logic
// related to proguard mapping will be excluded.
// Default is enabled.
includeProguardMapping = sentry_upload
// Whether the plugin should attempt to auto-upload the mapping file to Sentry or not.
// If disabled the plugin will run a dry-run and just generate a UUID.
// The mapping file has to be uploaded manually via sentry-cli in this case.
// Default is enabled.
autoUploadProguardMapping = sentry_upload
// Experimental flag to turn on support for GuardSquare's tools integration (Dexguard and External Proguard).
// If enabled, the plugin will try to consume and upload the mapping file produced by Dexguard and External Proguard.
// Default is disabled.
dexguardEnabled = false
// Disables or enables the automatic configuration of Native Symbols
// for Sentry. This executes sentry-cli automatically so
// you don't need to do it manually.
// Default is disabled.
uploadNativeSymbols = sentry_upload
// Whether the plugin should attempt to auto-upload the native debug symbols to Sentry or not.
// If disabled the plugin will run a dry-run.
// Default is enabled.
autoUploadNativeSymbols = sentry_upload
// Does or doesn't include the source code of native code for Sentry.
// This executes sentry-cli with the --include-sources param. automatically so
// you don't need to do it manually.
// This option has an effect only when [uploadNativeSymbols] is enabled.
// Default is disabled.
includeNativeSources = false
// Generates a JVM (Java, Kotlin, etc.) source bundle and uploads your source code to Sentry.
// This enables source context, allowing you to see your source
// code as part of your stack traces in Sentry.
includeSourceContext = sentry_upload
// Configure additional directories to be included in the source bundle which is used for
// source context. The directories should be specified relative to the Gradle module/project's
// root. For example, if you have a custom source set alongside 'main', the parameter would be
// 'src/custom/java'.
additionalSourceDirsForSourceContext = []
// Enable or disable the tracing instrumentation.
// Does auto instrumentation for specified features through bytecode manipulation.
// Default is enabled.
tracingInstrumentation {
enabled = true
// Specifies a set of instrumentation features that are eligible for bytecode manipulation.
// Defaults to all available values of InstrumentationFeature enum class.
features = [InstrumentationFeature.DATABASE, InstrumentationFeature.FILE_IO, InstrumentationFeature.OKHTTP, InstrumentationFeature.COMPOSE]
// Enable or disable logcat instrumentation through bytecode manipulation.
// Default is enabled.
logcat {
enabled = true
// Specifies a minimum log level for the logcat breadcrumb logging.
// Defaults to LogcatLevel.WARNING.
minLevel = LogcatLevel.WARNING
}
// The set of glob patterns to exclude from instrumentation. Classes matching any of these
// patterns in the project's sources and dependencies JARs won't be instrumented by the Sentry
// Gradle plugin.
//
// Don't include the file extension. Filtering is done on compiled classes and
// the .class suffix isn't included in the pattern matching.
//
// Example usage:
// ```
// excludes = ['com/example/donotinstrument/**', '**/*Test']
// ```
//
// Only supported when using Android Gradle plugin (AGP) version 7.4.0 and above.
excludes = []
}
// Enable auto-installation of Sentry components (sentry-android SDK and okhttp, timber and fragment integrations).
// Default is enabled.
// Only available v3.1.0 and above.
autoInstallation {
enabled = true
// Specifies a version of the sentry-android SDK and fragment, timber and okhttp integrations.
//
// This is also useful, when you have the sentry-android SDK already included into a transitive dependency/module and want to
// align integration versions with it (if it's a direct dependency, the version will be inferred).
//
// NOTE: if you have a higher version of the sentry-android SDK or integrations on the classpath, this setting will have no effect
// as Gradle will resolve it to the latest version.
//
// Defaults to the latest published Sentry version.
sentryVersion = '7.8.0'
}
// Disables or enables dependencies metadata reporting for Sentry.
// If enabled, the plugin will collect external dependencies and
// upload them to Sentry as part of events. If disabled, all the logic
// related to the dependencies metadata report will be excluded.
//
// Default is enabled.
includeDependenciesReport = true
// Whether the plugin should send telemetry data to Sentry.
// If disabled the plugin won't send telemetry data.
// This is auto disabled if running against a self hosted instance of Sentry.
// Default is enabled.
telemetry = true
}

View File

@ -142,6 +142,8 @@
android:allowAudioPlaybackCapture="true" android:allowAudioPlaybackCapture="true"
tools:replace="android:supportsRtl"> tools:replace="android:supportsRtl">
<meta-data android:name="io.sentry.auto-init" android:value="false" />
<activity-alias <activity-alias
android:enabled="true" android:enabled="true"
android:name="org.telegram.messenger.DefaultIcon" android:name="org.telegram.messenger.DefaultIcon"

View File

@ -63,6 +63,7 @@ import java.util.LinkedList;
import tw.nekomimi.nekogram.NekoConfig; import tw.nekomimi.nekogram.NekoConfig;
import tw.nekomimi.nekogram.NekoXConfig; import tw.nekomimi.nekogram.NekoXConfig;
import tw.nekomimi.nekogram.helpers.AnalyticsHelper;
import tw.nekomimi.nekogram.parts.SignturesKt; import tw.nekomimi.nekogram.parts.SignturesKt;
import tw.nekomimi.nekogram.utils.FileUtil; import tw.nekomimi.nekogram.utils.FileUtil;
import xyz.nextalone.nagram.NaConfig; import xyz.nextalone.nagram.NaConfig;
@ -111,6 +112,7 @@ public class ApplicationLoader extends Application {
} }
Thread.currentThread().setUncaughtExceptionHandler((thread, error) -> { Thread.currentThread().setUncaughtExceptionHandler((thread, error) -> {
Log.e("nekox", "from " + thread.toString(), error); Log.e("nekox", "from " + thread.toString(), error);
AnalyticsHelper.captureException(error);
}); });
} }
@ -297,6 +299,8 @@ public class ApplicationLoader extends Application {
super.onCreate(); super.onCreate();
AnalyticsHelper.start(this);
if (BuildVars.LOGS_ENABLED) { if (BuildVars.LOGS_ENABLED) {
FileLog.d("app start time = " + (startTime = SystemClock.elapsedRealtime())); FileLog.d("app start time = " + (startTime = SystemClock.elapsedRealtime()));
try { try {

View File

@ -0,0 +1,61 @@
package tw.nekomimi.nekogram.helpers;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import org.telegram.messenger.BuildConfig;
import org.telegram.messenger.BuildVars;
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.android.core.SentryAndroid;
public class AnalyticsHelper {
public static String DSN = "https://58f21343622b885cb7ad43fee0943f77@o416616.ingest.us.sentry.io/4507212454428672";
public static boolean loaded = false;
public static void start(Application application) {
if (!getSentryStatus(application)) {
return;
}
SentryAndroid.init(application, options -> {
options.setDsn(DSN);
options.setEnvironment(BuildVars.DEBUG_VERSION ? "debug" : "release");
options.setEnableAutoSessionTracking(true);
options.setTracesSampleRate(1.0);
options.setAttachAnrThreadDump(true);
options.setRelease(BuildConfig.APPLICATION_ID + "@" + BuildConfig.VERSION_NAME + "+" + BuildConfig.VERSION_CODE);
options.setBeforeScreenshotCaptureCallback((event, hint, debounce) -> {
// always capture crashed events
if (event.isCrashed()) {
return true;
}
// if debounce is active, skip capturing
if (debounce) {
return false;
} else {
// also capture fatal events
return event.getLevel() == SentryLevel.FATAL;
}
});
});
loaded = true;
}
public static void captureException(Throwable e) {
if (loaded) {
Sentry.captureException(e);
}
}
public static boolean getSentryStatus(Application application) {
SharedPreferences preferences = application.getApplicationContext().getSharedPreferences(
"nkmrcfg",
Context.MODE_PRIVATE
);
return preferences.getBoolean("SentryAnalytics", true);
}
}

View File

@ -214,6 +214,7 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity {
private final AbstractConfigCell doNotShareMyPhoneNumberRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDoNotShareMyPhoneNumber())); private final AbstractConfigCell doNotShareMyPhoneNumberRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDoNotShareMyPhoneNumber()));
private final AbstractConfigCell disableSuggestionViewRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableSuggestionView())); private final AbstractConfigCell disableSuggestionViewRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableSuggestionView()));
private final AbstractConfigCell disableAutoWebLoginRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableAutoWebLogin())); private final AbstractConfigCell disableAutoWebLoginRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableAutoWebLogin()));
private final AbstractConfigCell sentryAnalyticsRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getSentryAnalytics()));
private final AbstractConfigCell divider6 = cellGroup.appendCell(new ConfigCellDivider()); private final AbstractConfigCell divider6 = cellGroup.appendCell(new ConfigCellDivider());
private final AbstractConfigCell header7 = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString("General"))); private final AbstractConfigCell header7 = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString("General")));
@ -497,6 +498,8 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity {
ApplicationLoader.startPushService(); ApplicationLoader.startPushService();
} else if (key.equals(NaConfig.INSTANCE.getPushServiceTypeUnifiedGateway().getKey())) { } else if (key.equals(NaConfig.INSTANCE.getPushServiceTypeUnifiedGateway().getKey())) {
restartTooltip.showWithAction(0, UndoView.ACTION_NEED_RESATRT, null, null); restartTooltip.showWithAction(0, UndoView.ACTION_NEED_RESATRT, null, null);
} else if (key.equals(NaConfig.INSTANCE.getSentryAnalytics().getKey())) {
restartTooltip.showWithAction(0, UndoView.ACTION_NEED_RESATRT, null, null);
} }
}; };

View File

@ -590,6 +590,12 @@ object NaConfig {
ConfigItem.configTypeBool, ConfigItem.configTypeBool,
false false
) )
val sentryAnalytics =
addConfig(
"SentryAnalytics",
ConfigItem.configTypeBool,
true
)
private fun addConfig( private fun addConfig(
k: String, k: String,

View File

@ -143,4 +143,5 @@
<string name="PreviewSendPhoto">发送图片</string> <string name="PreviewSendPhoto">发送图片</string>
<string name="PreviewSendVideo">发送视频</string> <string name="PreviewSendVideo">发送视频</string>
<string name="PreviewSendFile">发送文件</string> <string name="PreviewSendFile">发送文件</string>
<string name="SentryAnalytics">Sentry 崩溃自动上报</string>
</resources> </resources>

View File

@ -143,4 +143,5 @@
<string name="PreviewSendPhoto">Send Photo</string> <string name="PreviewSendPhoto">Send Photo</string>
<string name="PreviewSendVideo">Send Video</string> <string name="PreviewSendVideo">Send Video</string>
<string name="PreviewSendFile">Send File</string> <string name="PreviewSendFile">Send File</string>
<string name="SentryAnalytics">Sentry Crash Report</string>
</resources> </resources>

View File

@ -14,6 +14,7 @@ buildscript {
//noinspection GradleDependency //noinspection GradleDependency
classpath "com.github.triplet.gradle:play-publisher:3.6.0" classpath "com.github.triplet.gradle:play-publisher:3.6.0"
classpath 'cn.hutool:hutool-core:5.7.13' classpath 'cn.hutool:hutool-core:5.7.13'
classpath "io.sentry:sentry-android-gradle-plugin:4.5.1"
} }
} }