diff --git a/app/src/main/kotlin/io/element/android/x/ElementXApplication.kt b/app/src/main/kotlin/io/element/android/x/ElementXApplication.kt index a3712ebbf6..228fac342b 100644 --- a/app/src/main/kotlin/io/element/android/x/ElementXApplication.kt +++ b/app/src/main/kotlin/io/element/android/x/ElementXApplication.kt @@ -9,6 +9,7 @@ package io.element.android.x import android.app.Application +import android.os.StrictMode import androidx.compose.material3.ComposeMaterial3Flags.isAnchoredDraggableComponentsStrictOffsetCheckEnabled import androidx.compose.material3.ExperimentalMaterial3Api import androidx.startup.AppInitializer @@ -38,6 +39,21 @@ class ElementXApplication : Application(), DependencyInjectionGraphOwner, Config @OptIn(ExperimentalMaterial3Api::class) override fun onCreate() { super.onCreate() + + // Workaround to avoid NetworkOnMainThreadException when using the network on the main thread. + // This is a temporary solution to rustls doing the certificate verification using blocking network requests. + // It should be removed once the underlying issue is fixed. + // Also, we only want to enable it on nightly and release builds so we can debug the issue on debug builds. + if (!BuildConfig.DEBUG) { + StrictMode.setThreadPolicy( + StrictMode.ThreadPolicy.Builder() + .detectNetwork() + .penaltyLog() + .penaltyDialog() + .build() + ) + } + AppInitializer.getInstance(this).apply { initializeComponent(CrashInitializer::class.java) initializeComponent(PlatformInitializer::class.java) diff --git a/app/src/main/kotlin/io/element/android/x/initializer/PlatformInitializer.kt b/app/src/main/kotlin/io/element/android/x/initializer/PlatformInitializer.kt index 910b2851e6..1d2504ae67 100644 --- a/app/src/main/kotlin/io/element/android/x/initializer/PlatformInitializer.kt +++ b/app/src/main/kotlin/io/element/android/x/initializer/PlatformInitializer.kt @@ -8,8 +8,10 @@ package io.element.android.x.initializer +import android.annotation.SuppressLint import android.content.Context import android.system.Os +import android.util.Log import androidx.startup.Initializer import io.element.android.features.rageshake.api.logs.createWriteToFilesConfiguration import io.element.android.libraries.architecture.bindings @@ -25,6 +27,15 @@ private const val ELEMENT_X_TARGET = "elementx" class PlatformInitializer : Initializer { override fun create(context: Context) { + // Init the SDK with the Android Context, so it can be used with rustls and other Android specific features + runCatchingExceptions { + org.matrix.rustsdk.Android.init(context) + }.onFailure { + // We can't log this to Timber since the logs aren't initialized yet, so we log it to Logcat instead + @SuppressLint("LogNotTimber") + Log.e("PlatformInitializer", "Failed to init the SDK with an Android Context. The app probably won't work properly.", it) + } + val appBindings = context.bindings() val tracingService = appBindings.tracingService() val platformService = appBindings.platformService() @@ -47,13 +58,6 @@ class PlatformInitializer : Initializer { platformService.init(tracingConfiguration) // Also set env variable for rust back trace Os.setenv("RUST_BACKTRACE", "1", true) - - // Init the SDK with the Android Context, so it can be used with rustls and other Android specific features - runCatchingExceptions { - org.matrix.rustsdk.Android.init(context) - }.onFailure { - Timber.e(it, "Failed to init the SDK with an Android Context. The app probably won't work properly.") - } } override fun dependencies(): List>> = mutableListOf()