Should I remove leak canary code/classes for release build?
I found this online.
dependencies {
// Real LeakCanary for debug builds only: notifications, analysis, etc
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
// No-Op version of LeakCanary for release builds: no notifications, no analysis, nothing
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
}
The answers are correct but there's an updated and easier solution as of 4/13/19:
dependencies {
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-beta-3'
}
no need for no-ops nor proguard.
but why?
ok but how?
I was using the latest version of the library 2.0-alpha-1 in debug mode, and I didn't find the release dependency of the library. I did the following:
- I created separate folders for each build mode (release and debug)
- The path of the debug folder: app/src/debug
- The path of the release folder: app/src/release
Then I created a class for initializing leak canary called LeakCanaryInitializer.kt (I created it inside the two build folders)
- debug: app/src/debug/java/LeakCanaryInitializer.kt
- release: app/src/release/java/LeakCanaryInitializer.kt
The class in the release mode contains:
import android.content.Context
object LeakCanaryManager {
fun init(context: Context) {
// We should do nothing in the release mode
}
}
The class in the debug mode contains:
import android.content.Context
import leakcanary.LeakCanary
import leakcanary.LeakSentry
object LeakCanaryManager {
fun init(context: Context) {
// Here you should write your custom initializing
}
}
Then in your Application class call the init method:
LeakCanaryManager.init(this)
My gradle file only contains the debug dependency:
debugImplementation "com.squareup.leakcanary:leakcanary-android:2.0-alpha-1"