Skip to content

Commit

Permalink
Integrate sentry sdk to enable crash reporting (#382)
Browse files Browse the repository at this point in the history
- Setup Sentry with gradle plugin and some default values
- Setup "environment" option to refer to the build type
- Added `unsignedRelease` build type
- Added `sentry.properties` to files to encrypt/decrypt
- Encrypted `sentryDsn` value in `secret.properties`
(@MozillaSocial/android you'll need to run `./decrypt` locally to be
able to send sentry events)
- Setup Timber to log to sentry on anything other than a debug build
using the
[sentry-android-timber](https://docs.sentry.io/platforms/android/integrations/timber/)
library
  • Loading branch information
devotaaabel committed Jan 18, 2024
1 parent d2223b7 commit 9997565
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ jobs:
uses: ./.github/actions/setup-environment
- name: Verify KSP version
run: ci/verify-ksp-version.sh
- name: Decrypt secrets
env:
GPG_KEY: ${{ secrets.gpg_key }}
run: ./decrypt.sh
working-directory: ./secrets
- name: Compile a release build
env:
GRADLE_OPTS: -Dorg.gradle.daemon=false
Expand Down
4 changes: 3 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/build
/build
# Sentry Config File
sentry.properties
23 changes: 23 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id("org.mozilla.social.android.application")
id("org.mozilla.social.android.application.compose")
id("org.mozilla.social.android.application.secrets")
alias(libs.plugins.sentry)
}

android {
Expand All @@ -15,6 +17,7 @@ android {
vectorDrawables {
useSupportLibrary = true
}
manifestPlaceholders["debug"] = false
}

buildTypes {
Expand All @@ -23,14 +26,24 @@ android {
isShrinkResources = true
proguardFile("proguard-rules.pro")
matchingFallbacks += "release"
manifestPlaceholders["environment"] = "release"
}
debug {
isDefault = true
applicationIdSuffix = ".debug"
manifestPlaceholders["environment"] = "debug"
manifestPlaceholders["debug"] = true
}
create("nightly") {
initWith(getByName("release"))
applicationIdSuffix = ".nightly"
manifestPlaceholders["environment"] = "nightly"
}

create("unsignedRelease") {
initWith(getByName("release"))
signingConfig = signingConfigs.getByName("debug")
manifestPlaceholders["environment"] = "unsignedRelease"
}
}

Expand Down Expand Up @@ -112,3 +125,13 @@ dependencies {
debugImplementation(libs.androidx.compose.ui.test.manifest)
debugImplementation(libs.androidx.compose.ui.tooling)
}


sentry {
org.set("mozilla")
projectName.set("moso-android")

// this will upload your source code to Sentry to show it as part of the stack traces
// disable if you don't want to expose your sources
includeSourceContext.set(true)
}
1 change: 1 addition & 0 deletions app/sentry.properties.gpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
� �^a�hWH�Ҍ�k��3�;�a��Gt�k����&���!5Mb�1��WLDb�AC��=����\I�Y, [?�;Vc���.H�si��/ڱ��f>��x�z]�b0�(q���%WJi=�I���(�,�z��o�kؿZ�,O�3�
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,22 @@
android:value="androidx.startup"
tools:node="remove" />
</provider>
</application>

<!-- Required: set your sentry.io project identifier (DSN) -->
<meta-data android:name="io.sentry.dsn" android:value="${sentryDsn}" />
<meta-data android:name="io.sentry.debug" android:value="${debug}" />
<meta-data android:name="io.sentry.environment" android:value="${environment}" />

<!-- enable automatic breadcrumbs for user interactions (clicks, swipes, scrolls) -->
<meta-data android:name="io.sentry.traces.user-interaction.enable" android:value="true" />
<!-- enable screenshot for crashes (could contain sensitive/PII data) -->
<meta-data android:name="io.sentry.attach-screenshot" android:value="true" />
<!-- enable view hierarchy for crashes -->
<meta-data android:name="io.sentry.attach-view-hierarchy" android:value="true" />

<!-- enable the performance API by setting a sample-rate, adjust in production env -->
<meta-data android:name="io.sentry.traces.sample-rate" android:value="1.0" />
<!-- enable profiling when starting transactions, adjust in production env -->
<meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />
</application>
</manifest>
2 changes: 2 additions & 0 deletions app/src/main/java/org/mozilla/social/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.koin.androidx.compose.KoinAndroidContext
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.annotation.KoinExperimentalAPI
import org.mozilla.social.core.designsystem.theme.MoSoTheme
import org.mozilla.social.core.ui.common.MoSoSurface
import org.mozilla.social.core.workmanager.DatabasePurgeWorker
Expand All @@ -20,6 +21,7 @@ import timber.log.Timber
class MainActivity : ComponentActivity() {
private val viewModel: MainViewModel by viewModel()

@OptIn(KoinExperimentalAPI::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch {
Expand Down
36 changes: 31 additions & 5 deletions app/src/main/java/org/mozilla/social/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import coil.ImageLoaderFactory
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.VideoFrameDecoder
import io.sentry.SentryLevel
import io.sentry.android.core.SentryAndroid
import io.sentry.android.timber.SentryTimberIntegration
import org.koin.android.ext.android.get
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
Expand Down Expand Up @@ -34,16 +37,35 @@ import org.mozilla.social.search.searchModule
import timber.log.Timber

class MainApplication : Application(), ImageLoaderFactory {
private lateinit var authCredentialObserver: AuthCredentialObserver

private lateinit var authCredentialObserver: AuthCredentialObserver
private val analytics: Analytics by inject()

override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
initializeAppVersion()
initializeSentryAndTimber()
initializeKoin()
initializeAnalytics()
initializeAuthCredentialInterceptor()
}

private fun initializeSentryAndTimber() {
SentryAndroid.init(this) { options ->
if (!BuildConfig.DEBUG) {
options.addIntegration(
SentryTimberIntegration(
minEventLevel = SentryLevel.ERROR,
minBreadcrumbLevel = SentryLevel.INFO
)
)
} else {
Timber.plant(Timber.DebugTree())
}
}
}

private fun initializeKoin() {
startKoin {
androidLogger()
androidContext(this@MainApplication)
Expand All @@ -54,10 +76,14 @@ class MainApplication : Application(), ImageLoaderFactory {
workManagerModule,
)
}
}

analytics.initialize(applicationContext)

private fun initializeAnalytics() = analytics.initialize(applicationContext)
private fun initializeAuthCredentialInterceptor() {
authCredentialObserver = get()
}

private fun initializeAppVersion() {
Version.name = BuildConfig.VERSION_NAME
Version.code = BuildConfig.VERSION_CODE
}
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,4 @@ kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-ksp = { id = "com.google.devtools.ksp", version.ref = "kotlin-ksp" }
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin"}
protobuf = { id = "com.google.protobuf", version = "0.9.3" }
sentry = { id = "io.sentry.android.gradle", version = "4.2.0" }
1 change: 1 addition & 0 deletions secrets/decrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ if [[ -z "$GPG_KEY" ]]; then
fi

decrypt "secret.properties"
decrypt "../app/sentry.properties"
decrypt "secret-environment-variables.sh" && chmod +x secret-environment-variables.sh
decrypt "boxwood-axon-825-ed7aa5764ee6.json"
1 change: 1 addition & 0 deletions secrets/encrypt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ if [[ -z "$GPG_KEY" ]]; then
fi

encrypt "secret.properties"
encrypt "../app/sentry.properties"
encrypt "secret-environment-variables.sh"
encrypt "boxwood-axon-825-ed7aa5764ee6.json"
3 changes: 2 additions & 1 deletion secrets/secret.default.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mozillaSocialClientSecret=fake default value just to get the code to compile but it won't work
newTabConsumerKey=fake default value just to get the code to compile but it won't work
stagingUrl=mozilla.social
stagingUrl=mozilla.social
sentryDsn=fake default value
Binary file modified secrets/secret.properties.gpg
Binary file not shown.

0 comments on commit 9997565

Please sign in to comment.