Search code examples
androidrealmleakcanary

Using LeakCanary on project with Realm


I have an android project that uses Realm. I'm trying to integrate LeakCanary to find memory leaks, but when LeakCanary tries to create report, it crashes with exception

encrypted.realm: Encrypted interprocess sharing is currently unsupported

As I understand, LeakCanary tries to access Realm from different process, that causes crash.

How to fix this problem? Is anybody using Realm and LeakCanary in project?


Solution

  • As I found in this topic https://github.com/realm/realm-java/issues/3053 you can skip Realm initialization for LeakCanary process in this way

     if (!LeakCanary.isInAnalyzerProcess(this)) {
                byte[] key = new byte[64];
                Arrays.fill(key, (byte) 0);
    
                config = new RealmConfiguration.Builder(this).encryptionKey(key).build();
                realm = DynamicRealm.getInstance(config);
            }
    

    Or, if you still need Realm, you can create dumb realm config

    Realm.init(context);
    
    if (LeakCanary.isInAnalyzerProcess(context)) {
        return new RealmConfiguration.Builder().build();
    }