Flipper: Docs: LeakCanary2 setup missing Java format

Created on 12 May 2021  路  3Comments  路  Source: facebook/flipper

Current documentation for setting up LeakCanary2 is showing the code snippet with Kotlin.

This makes it very difficult for a React Native developer to setup because everything with RN comes with Java code.

import com.facebook.flipper.plugins.leakcanary2.FlipperLeakListener
import com.facebook.flipper.plugins.leakcanary2.LeakCanary2FlipperPlugin

...

  override fun onCreate() {
    super.onCreate()
    setupFlipper()

    /*
    set the flipper listener in leak canary config
    */
    LeakCanary.config = LeakCanary.config.copy(
        onHeapAnalyzedListener = FlipperLeakListener()
    )

    SoLoader.init(this, false)

    if (BuildConfig.DEBUG && FlipperUtils.shouldEnableFlipper(this)) {
      val client = AndroidFlipperClient.getInstance(this)
      /*
      add leak canary plugin to flipper
      */
      client.addPlugin(LeakCanary2FlipperPlugin())
      client.start()
    }
  }

Could someone help me figure out how to translate this to Java code?

Most helpful comment

The LeakCanary can be configured in Java as shown in https://square.github.io/leakcanary/api/leakcanary-android-core/leakcanary/-leak-canary/config/
Please try this:

  @Override
  public void onCreate() {
    LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
      .onHeapAnalyzedListener(new FlipperLeakListener());
      .build()
    LeakCanary.setConfig(config);

    if (BuildConfig.DEBUG && FlipperUtils.shouldEnableFlipper(this)) {
      final FlipperClient client = AndroidFlipperClient.getInstance(this)

      // add leak canary plugin to flipper
      client.addPlugin(new LeakCanary2FlipperPlugin())
      client.start()
    }
  }

All 3 comments

The LeakCanary can be configured in Java as shown in https://square.github.io/leakcanary/api/leakcanary-android-core/leakcanary/-leak-canary/config/
Please try this:

  @Override
  public void onCreate() {
    LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
      .onHeapAnalyzedListener(new FlipperLeakListener());
      .build()
    LeakCanary.setConfig(config);

    if (BuildConfig.DEBUG && FlipperUtils.shouldEnableFlipper(this)) {
      final FlipperClient client = AndroidFlipperClient.getInstance(this)

      // add leak canary plugin to flipper
      client.addPlugin(new LeakCanary2FlipperPlugin())
      client.start()
    }
  }

LeakCanary is written in Kotlin and mostly designed for Kotlin users. Android is Kotlin-first now. It would be prudent to familiarize yourself with Kotlin if you want to continue developing for Android rather than complain on unrelated projects for not having a Java example for you.

LeakCanary is written in Kotlin and mostly designed for Kotlin users. Android is Kotlin-first now. It would be prudent to familiarize yourself with Kotlin if you want to continue developing for Android rather than complain on unrelated projects for not having a Java example for you.

First of all, I am not complaining, I was specifically asking for help for translating this to Java since the project I'm working on is configured with Java, not Kotlin (I have no control on that)

If there isn't supposed to be documentation for this, that's fine. I was just pointing out one possible issue when trying to use this plugin with a RN project.

Thanks for the actual help @cekkaewnumchai

Was this page helpful?
0 / 5 - 0 ratings