mobile-messaging-sdk-android
mobile-messaging-sdk-android copied to clipboard
StrictMode DiskReadViolation during initialization
Hey all.
We are using the com.infobip:infobip-mobile-messaging-android-chat-sdk:12.6.2
version of the SDK inside our app. During the initialization of the SDK with StrictMode enabled like so:
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.penaltyDeathOnNetwork()
.build()
)
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build()
)
we get the following reports:
15:12:40.889 D StrictMode policy violation; ~duration=151 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7795)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:759)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:750)
at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:706)
at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:931)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:553)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:217)
at org.infobip.mobile.messaging.util.PreferenceHelper.getPrivateMMSharedPreferences(PreferenceHelper.java:56)
at org.infobip.mobile.messaging.util.PreferenceHelper.findBoolean(PreferenceHelper.java:177)
at org.infobip.mobile.messaging.util.PreferenceHelper.shouldUsePrivateSharedPrefs(PreferenceHelper.java:197)
at org.infobip.mobile.messaging.util.PreferenceHelper.getDefaultMMSharedPreferences(PreferenceHelper.java:46)
at org.infobip.mobile.messaging.util.PreferenceHelper.saveString(PreferenceHelper.java:106)
at org.infobip.mobile.messaging.util.PreferenceHelper.saveString(PreferenceHelper.java:96)
at org.infobip.mobile.messaging.util.PreferenceHelper.saveString(PreferenceHelper.java:91)
at org.infobip.mobile.messaging.util.PreferenceHelper.saveString(PreferenceHelper.java:87)
at org.infobip.mobile.messaging.NotificationSettings.setDefaultTitle(NotificationSettings.java:65)
at org.infobip.mobile.messaging.NotificationSettings.access$000(NotificationSettings.java:37)
at org.infobip.mobile.messaging.NotificationSettings$Builder.build(NotificationSettings.java:432)
at org.infobip.mobile.messaging.MobileMessaging$Builder.loadNotificationSettings(MobileMessaging.java:534)
at org.infobip.mobile.messaging.MobileMessaging$Builder.<init>(MobileMessaging.java:526)
at *.support.chat.flow.main.infobip.MobileMessagingFactoryKt.build(MobileMessagingFactory.kt:8)
at *.support.chat.flow.main.infobip.InfobipChat.init(InfobipChat.kt:21)
at *.initializers.InfobipChatInitializer.init(InfobipChatInitializer.kt:10)
at *.initializers.CompositeInitializer.init(CompositeInitializer.kt:8)
at *.App.onCreate(App.kt:16)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1277)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6805)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
15:12:40.894 D StrictMode policy violation; ~duration=150 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:275)
at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:344)
at org.infobip.mobile.messaging.util.PreferenceHelper.findBoolean(PreferenceHelper.java:177)
at org.infobip.mobile.messaging.util.PreferenceHelper.shouldUsePrivateSharedPrefs(PreferenceHelper.java:197)
at org.infobip.mobile.messaging.util.PreferenceHelper.getDefaultMMSharedPreferences(PreferenceHelper.java:46)
at org.infobip.mobile.messaging.util.PreferenceHelper.saveString(PreferenceHelper.java:106)
at org.infobip.mobile.messaging.util.PreferenceHelper.saveString(PreferenceHelper.java:96)
at org.infobip.mobile.messaging.util.PreferenceHelper.saveString(PreferenceHelper.java:91)
at org.infobip.mobile.messaging.util.PreferenceHelper.saveString(PreferenceHelper.java:87)
at org.infobip.mobile.messaging.NotificationSettings.setDefaultTitle(NotificationSettings.java:65)
at org.infobip.mobile.messaging.NotificationSettings.access$000(NotificationSettings.java:37)
at org.infobip.mobile.messaging.NotificationSettings$Builder.build(NotificationSettings.java:432)
at org.infobip.mobile.messaging.MobileMessaging$Builder.loadNotificationSettings(MobileMessaging.java:534)
at org.infobip.mobile.messaging.MobileMessaging$Builder.<init>(MobileMessaging.java:526)
at *.support.chat.flow.main.infobip.MobileMessagingFactoryKt.build(MobileMessagingFactory.kt:8)
at *.support.chat.flow.main.infobip.InfobipChat.init(InfobipChat.kt:21)
at *.initializers.InfobipChatInitializer.init(InfobipChatInitializer.kt:10)
at *.initializers.CompositeInitializer.init(CompositeInitializer.kt:8)
at *.App.onCreate(App.kt:16)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1277)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6805)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
15:12:40.901 D StrictMode policy violation; ~duration=25 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7795)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:759)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:750)
at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:706)
at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:931)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:553)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:217)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:545)
at org.infobip.mobile.messaging.util.PreferenceHelper.getPublicSharedPreferences(PreferenceHelper.java:60)
at org.infobip.mobile.messaging.util.PreferenceHelper.publicPrefsContains(PreferenceHelper.java:391)
at org.infobip.mobile.messaging.util.PreferenceHelper.publicPrefsAreNotMigratedToPrivatePrefs(PreferenceHelper.java:191)
at org.infobip.mobile.messaging.util.PreferenceHelper.shouldMigrateToPrivatePrefs(PreferenceHelper.java:187)
at org.infobip.mobile.messaging.MobileMessagingCore.migratePrefsIfNecessary(MobileMessagingCore.java:196)
at org.infobip.mobile.messaging.MobileMessagingCore.<init>(MobileMessagingCore.java:186)
at org.infobip.mobile.messaging.MobileMessagingCore.<init>(MobileMessagingCore.java:157)
at org.infobip.mobile.messaging.MobileMessagingCore$Builder.build(MobileMessagingCore.java:2098)
at org.infobip.mobile.messaging.MobileMessaging$Builder.build(MobileMessaging.java:927)
at org.infobip.mobile.messaging.MobileMessaging$Builder.build(MobileMessaging.java:890)
at *.support.chat.flow.main.infobip.MobileMessagingFactoryKt.build(MobileMessagingFactory.kt:8)
at *.support.chat.flow.main.infobip.InfobipChat.init(InfobipChat.kt:21)
at *.initializers.InfobipChatInitializer.init(InfobipChatInitializer.kt:10)
at *.initializers.CompositeInitializer.init(CompositeInitializer.kt:8)
at *.App.onCreate(App.kt:16)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1277)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6805)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
15:12:40.908 D StrictMode policy violation; ~duration=25 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:275)
at android.app.SharedPreferencesImpl.contains(SharedPreferencesImpl.java:353)
at org.infobip.mobile.messaging.util.PreferenceHelper.publicPrefsContains(PreferenceHelper.java:391)
at org.infobip.mobile.messaging.util.PreferenceHelper.publicPrefsAreNotMigratedToPrivatePrefs(PreferenceHelper.java:191)
at org.infobip.mobile.messaging.util.PreferenceHelper.shouldMigrateToPrivatePrefs(PreferenceHelper.java:187)
at org.infobip.mobile.messaging.MobileMessagingCore.migratePrefsIfNecessary(MobileMessagingCore.java:196)
at org.infobip.mobile.messaging.MobileMessagingCore.<init>(MobileMessagingCore.java:186)
at org.infobip.mobile.messaging.MobileMessagingCore.<init>(MobileMessagingCore.java:157)
at org.infobip.mobile.messaging.MobileMessagingCore$Builder.build(MobileMessagingCore.java:2098)
at org.infobip.mobile.messaging.MobileMessaging$Builder.build(MobileMessaging.java:927)
at org.infobip.mobile.messaging.MobileMessaging$Builder.build(MobileMessaging.java:890)
at *.support.chat.flow.main.infobip.MobileMessagingFactoryKt.build(MobileMessagingFactory.kt:8)
at *.support.chat.flow.main.infobip.InfobipChat.init(InfobipChat.kt:21)
at *.initializers.InfobipChatInitializer.init(InfobipChatInitializer.kt:10)
at *.initializers.CompositeInitializer.init(CompositeInitializer.kt:8)
at *.App.onCreate(App.kt:16)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1277)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6805)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
15:12:41.591 D StrictMode policy violation; ~duration=92 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7795)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.getDataDir(ContextImpl.java:2962)
at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:704)
at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:931)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:553)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:217)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:217)
at org.infobip.mobile.messaging.util.PreferenceHelper.getPrivateMMSharedPreferences(PreferenceHelper.java:56)
at org.infobip.mobile.messaging.util.PreferenceHelper.getDefaultMMSharedPreferences(PreferenceHelper.java:49)
at org.infobip.mobile.messaging.util.PreferenceHelper.findBoolean(PreferenceHelper.java:172)
at org.infobip.mobile.messaging.MobileMessagingCore.shouldSaveApplicationCode(MobileMessagingCore.java:1285)
at org.infobip.mobile.messaging.MobileMessagingCore.getApplicationCode(MobileMessagingCore.java:1135)
at org.infobip.mobile.messaging.app.ActivityLifecycleMonitor.dispatchEventToModules(ActivityLifecycleMonitor.java:68)
at org.infobip.mobile.messaging.app.ActivityLifecycleMonitor.setForeground(ActivityLifecycleMonitor.java:56)
at org.infobip.mobile.messaging.app.ActivityLifecycleMonitor.onActivityResumed(ActivityLifecycleMonitor.java:95)
at android.app.Application.dispatchActivityResumed(Application.java:431)
at android.app.Activity.dispatchActivityResumed(Activity.java:1440)
at android.app.Activity.onResume(Activity.java:2022)
at androidx.fragment.app.FragmentActivity.onResume(FragmentActivity.java:309)
at *.main.MainActivity.onResume(MainActivity.kt:89)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1564)
at android.app.Activity.performResume(Activity.java:8474)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4763)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4806)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:57)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:179)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
15:12:41.597 D StrictMode policy violation; ~duration=92 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7795)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:759)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:750)
at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:706)
at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:931)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:553)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:217)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:217)
at org.infobip.mobile.messaging.util.PreferenceHelper.getPrivateMMSharedPreferences(PreferenceHelper.java:56)
at org.infobip.mobile.messaging.util.PreferenceHelper.getDefaultMMSharedPreferences(PreferenceHelper.java:49)
at org.infobip.mobile.messaging.util.PreferenceHelper.findBoolean(PreferenceHelper.java:172)
at org.infobip.mobile.messaging.MobileMessagingCore.shouldSaveApplicationCode(MobileMessagingCore.java:1285)
at org.infobip.mobile.messaging.MobileMessagingCore.getApplicationCode(MobileMessagingCore.java:1135)
at org.infobip.mobile.messaging.app.ActivityLifecycleMonitor.dispatchEventToModules(ActivityLifecycleMonitor.java:68)
at org.infobip.mobile.messaging.app.ActivityLifecycleMonitor.setForeground(ActivityLifecycleMonitor.java:56)
at org.infobip.mobile.messaging.app.ActivityLifecycleMonitor.onActivityResumed(ActivityLifecycleMonitor.java:95)
at android.app.Application.dispatchActivityResumed(Application.java:431)
at android.app.Activity.dispatchActivityResumed(Activity.java:1440)
at android.app.Activity.onResume(Activity.java:2022)
at androidx.fragment.app.FragmentActivity.onResume(FragmentActivity.java:309)
at *.main.MainActivity.onResume(MainActivity.kt:89)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1564)
at android.app.Activity.performResume(Activity.java:8474)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4763)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4806)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:57)
at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:179)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
We've followed the setup instructions from the README and basically have initialized the SDK from the Application
class.
It takes roughly half a second to initialize the SDK on the UI thread, which is quite a lot.
Can something be done about this in terms of a fix? Should we initialize it on the backgrround thread and will it even work?
Looking forward to hearing from you.
Hello @mars885 , we are checking this and will let you know with more info.
Hello @mars885,
well, seems like you answered your own question. Yes, you should move initialisation to non-UI thread.
In your case, the error is a DiskReadViolation which means you are performing a disk read operation on the main thread. This is not recommended because it can block the UI thread and make the app unresponsive. The SharedPreferences operations like get and put are disk I/O operations. If you are calling these operations on the main thread, it will cause this error. To fix this, you should move initialisation to a background thread.
Please let us know if this helped.
Hey.
Thanks for an update.
The setup guide does not mention clearly what thread a client of your SDK should use to initialize it. Does it make sense to add a mention for that?
Apart from that, moving initialization logic on a background thread will fix 1-4, but the problems 5-6 will still remain due to the fact that the SDK listens to the ActivityLifecycleCallbacks
events through the registerActivityLifecycleCallbacks
method. We aren't going to be able to fix those issues without your help.
Hey @mars885,
do you still get the same error messages for 5-6 after doing init fix as I see there still some Preferences error? If not the same, can you maybe share them?
Yes, thanks, we might add note to our docs regarding initialisation.