From 6ac0c8b7d68e3edf3da4184c8623699dab8a657a Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 7 May 2024 09:11:53 +0800 Subject: [PATCH] feat: sentry crash report --- .gitignore | 1 + TMessagesProj/build.gradle | 143 ++++++++++++++++++ TMessagesProj/src/main/AndroidManifest.xml | 2 + .../telegram/messenger/ApplicationLoader.java | 4 + .../nekogram/helpers/AnalyticsHelper.java | 61 ++++++++ .../settings/NekoGeneralSettingsActivity.java | 3 + .../kotlin/xyz/nextalone/nagram/NaConfig.kt | 6 + .../src/main/res/values-zh-rCN/strings_na.xml | 1 + .../src/main/res/values/strings_na.xml | 1 + build.gradle | 1 + 10 files changed, 223 insertions(+) create mode 100644 TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/AnalyticsHelper.java diff --git a/.gitignore b/.gitignore index 48f4c6078..7c1a02a78 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ .vscode/ build/ local.properties +sentry.properties obj/ service_account_credentials.json __pycache__/ diff --git a/TMessagesProj/build.gradle b/TMessagesProj/build.gradle index 1393dcc82..ac294a794 100644 --- a/TMessagesProj/build.gradle +++ b/TMessagesProj/build.gradle @@ -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: "kotlin-android" +apply plugin: "io.sentry.android.gradle" repositories { maven { @@ -43,6 +47,8 @@ configurations { def keystorePwd = null def alias = 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 Properties properties @@ -59,6 +65,7 @@ if (properties != null) { keystorePwd = properties.getProperty("KEYSTORE_PASS") alias = properties.getProperty("ALIAS_NAME") pwd = properties.getProperty("ALIAS_PASS") + sentry_token = properties.getProperty("SENTRY_AUTH_TOKEN", sentry_token) } 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 +} diff --git a/TMessagesProj/src/main/AndroidManifest.xml b/TMessagesProj/src/main/AndroidManifest.xml index 16aee4334..366bcf23d 100644 --- a/TMessagesProj/src/main/AndroidManifest.xml +++ b/TMessagesProj/src/main/AndroidManifest.xml @@ -142,6 +142,8 @@ android:allowAudioPlaybackCapture="true" tools:replace="android:supportsRtl"> + + { Log.e("nekox", "from " + thread.toString(), error); + AnalyticsHelper.captureException(error); }); } @@ -297,6 +299,8 @@ public class ApplicationLoader extends Application { super.onCreate(); + AnalyticsHelper.start(this); + if (BuildVars.LOGS_ENABLED) { FileLog.d("app start time = " + (startTime = SystemClock.elapsedRealtime())); try { diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/AnalyticsHelper.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/AnalyticsHelper.java new file mode 100644 index 000000000..ae9803486 --- /dev/null +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/AnalyticsHelper.java @@ -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); + } +} diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java index 32cbd7076..afaa66185 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java @@ -214,6 +214,7 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity { 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 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 header7 = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString("General"))); @@ -497,6 +498,8 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity { ApplicationLoader.startPushService(); } else if (key.equals(NaConfig.INSTANCE.getPushServiceTypeUnifiedGateway().getKey())) { 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); } }; diff --git a/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt b/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt index 980a400cd..150746c6a 100644 --- a/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt +++ b/TMessagesProj/src/main/kotlin/xyz/nextalone/nagram/NaConfig.kt @@ -590,6 +590,12 @@ object NaConfig { ConfigItem.configTypeBool, false ) + val sentryAnalytics = + addConfig( + "SentryAnalytics", + ConfigItem.configTypeBool, + true + ) private fun addConfig( k: String, diff --git a/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml b/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml index 2e2f260e8..f0073733f 100644 --- a/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml +++ b/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml @@ -143,4 +143,5 @@ 发送图片 发送视频 发送文件 + Sentry 崩溃自动上报 diff --git a/TMessagesProj/src/main/res/values/strings_na.xml b/TMessagesProj/src/main/res/values/strings_na.xml index 7ffdb27cd..b0d864e9f 100644 --- a/TMessagesProj/src/main/res/values/strings_na.xml +++ b/TMessagesProj/src/main/res/values/strings_na.xml @@ -143,4 +143,5 @@ Send Photo Send Video Send File + Sentry Crash Report diff --git a/build.gradle b/build.gradle index c28d02a9e..02ce0cbd4 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,7 @@ buildscript { //noinspection GradleDependency classpath "com.github.triplet.gradle:play-publisher:3.6.0" classpath 'cn.hutool:hutool-core:5.7.13' + classpath "io.sentry:sentry-android-gradle-plugin:4.5.1" } }