From 2feddc9bd6cea8e00ca89b5eb7b6292b1a91667f Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Fri, 12 Jul 2019 00:23:28 -0500 Subject: [PATCH] No issue: Enable Strict Mode in Debug Builds (#4014) --- .../mozilla/fenix/DebugFenixApplication.kt | 3 ++- .../org/mozilla/fenix/FenixApplication.kt | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/src/debug/java/org/mozilla/fenix/DebugFenixApplication.kt b/app/src/debug/java/org/mozilla/fenix/DebugFenixApplication.kt index fa75b4e6c..facd5f48d 100644 --- a/app/src/debug/java/org/mozilla/fenix/DebugFenixApplication.kt +++ b/app/src/debug/java/org/mozilla/fenix/DebugFenixApplication.kt @@ -26,7 +26,6 @@ import java.io.File class DebugFenixApplication : FenixApplication() { override fun onCreate() { - super.onCreate() SoLoader.init(this, false) if (FlipperUtils.shouldEnableFlipper(this)) { @@ -39,6 +38,8 @@ class DebugFenixApplication : FenixApplication() { start() } } + + super.onCreate() } private var heapDumper: ToggleableHeapDumper? = null diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index 8e8febeb2..706a1b13b 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -8,6 +8,7 @@ import android.annotation.SuppressLint import android.app.Application import android.os.Build import android.os.Build.VERSION.SDK_INT +import android.os.StrictMode import androidx.appcompat.app.AppCompatDelegate import io.reactivex.plugins.RxJavaPlugins import kotlinx.coroutines.Deferred @@ -55,6 +56,7 @@ open class FenixApplication : Application() { setupLogging(megazordEnabled) registerRxExceptionHandling() setupCrashReporting() + enableStrictMode() if (!isMainProcess()) { // If this is not the main process then do not continue with the initialization here. Everything that @@ -237,4 +239,25 @@ open class FenixApplication : Application() { } } } + + private fun enableStrictMode() { + if (BuildConfig.DEBUG) { + StrictMode.setThreadPolicy( + StrictMode.ThreadPolicy.Builder() + .detectAll() + .penaltyLog() + .build() + ) + var builder = StrictMode.VmPolicy.Builder() + .detectLeakedSqlLiteObjects() + .detectLeakedClosableObjects() + .detectLeakedRegistrationObjects() + .detectActivityLeaks() + .detectFileUriExposure() + .penaltyLog() + if (SDK_INT >= Build.VERSION_CODES.O) builder = builder.detectContentUriWithoutPermission() + if (SDK_INT >= Build.VERSION_CODES.P) builder = builder.detectNonSdkApiUsage() + StrictMode.setVmPolicy(builder.build()) + } + } }