Android内存泄漏检测工具LeakCanary

LeakCanary

引入

dependencies {
    
    
    //https://github.com/square/leakcanary
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
}

2.0以上的leakcanary自带ContentProvider初始化

internal class PlumberInstaller : ContentProvider() {
    
    

  override fun onCreate(): Boolean {
    
    
    val application = context!!.applicationContext as Application
    AndroidLeakFixes.applyFixes(application)
    return true
  }
	...
}

如果项目中使用aspectj导致和kotlin不兼容问题,此时需要使用leakcanary2.0以下的版本

//    由于aspectj和kotlin不兼容问题,且leakcanary2.0以后的版本使用了kotlin进行重构,所以只能用旧版本
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
  • 老版本需要在Application中手动初始化
        if (BuildConfig.DEBUG) {
    
    
            if (LeakCanary.isInAnalyzerProcess(this)) {
    
    
                // This process is dedicated to LeakCanary for heap analysis.
                // You should not init your app in this process.
                return;
            }
            LeakCanary.install(this);
        }

猜你喜欢

转载自blog.csdn.net/yu540135101/article/details/117412248