这是indexloc提供的服务,不要输入任何密码
Skip to content

DiskReadViolation triggered by setCrashlyticsCollectionEnabled()  #3265

@DanielNovak

Description

@DanielNovak

Describe your environment

  • Android Studio version: 2020.3.1 Patch 4
  • Firebase Component: Crashlytics
  • Component version: 18.2.6

Describe the problem

Firebase Crashlytics is triggering Android StrictMode DiskReadViolation when setCrashlyticsCollectionEnabled(true) is called.

StrictMode policy violation; ~duration=351 ms: android.os.strictmode.DiskReadViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1659)
        at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
        at libcore.io.ForwardingOs.access(ForwardingOs.java:131)
        at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7715)
        at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:281)
        at java.io.File.exists(File.java:813)
        at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:738)
        at android.app.SharedPreferencesImpl.access$900(SharedPreferencesImpl.java:59)
        at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:672)
        at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:691)
        at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:59)
        at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:604)
        at com.google.firebase.crashlytics.internal.common.DataCollectionArbiter.storeDataCollectionValueInSharedPreferences(DataCollectionArbiter.java:206)
        at com.google.firebase.crashlytics.internal.common.DataCollectionArbiter.setCrashlyticsDataCollectionEnabled(DataCollectionArbiter.java:92)
        at com.google.firebase.crashlytics.internal.common.CrashlyticsCore.setCrashlyticsCollectionEnabled(CrashlyticsCore.java:257)
        at com.google.firebase.crashlytics.FirebaseCrashlytics.setCrashlyticsCollectionEnabled(FirebaseCrashlytics.java:448)

Steps to reproduce:

Enable strict mode:

        StrictMode.setThreadPolicy(
            ThreadPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .build()

and call:

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(enableCrashlytics)

Relevant Code:

The problem is in Firebase DataCollectionArbitrer.java. It's using commit() instead of apply():

    private static void storeDataCollectionValueInSharedPreferences(SharedPreferences sharedPreferences, Boolean enabled) {
        Editor prefsEditor = sharedPreferences.edit();
        if (enabled != null) {
            prefsEditor.putBoolean("firebase_crashlytics_collection_enabled", enabled);
        } else {
            prefsEditor.remove("firebase_crashlytics_collection_enabled");
        }

        prefsEditor.commit();
    }

For some reason the developers also marked it with @SuppressLint({"ApplySharedPref"}). So maybe they had a reason to do it like this? But then this call is always blocking the main thread on many apps on start...

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions