LeakCanary 2.1 的使用

一、2.1版本直接在build.gradle添加依赖,官方文档提示只需添加依赖即可。

dependencies {
  debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
}

二、但build时有可能会报以下错误:


错误代码.png

解决方案:
在module的build.gradle的android标签下,添加如下代码

android {
    packagingOptions {
        pickFirst "META-INF/shark.kotlin_module"
    }
}

android {
    packagingOptions {
        exclude "META-INF/shark.kotlin_module"
    }
}

三、app大概率现在就能顺利跑起来了,但 logocat 过滤LeakCanary,出现LeakCanary is currently disabled,说明被禁用了。
这是因为leakcanary通过检测 org.junit.Test 在类路径中。不幸的是,有些应用程序在其app debug类路径中提供junit。
解决方法:


添加路径.png
<resources>
  <string name="leak_canary_test_class_name">assertk.Assert</string>
</resources>

再次运行,logcat 过滤LeakCanary,出现以下日志,集成成功!

D LeakCanary: LeakCanary is running and ready to detect leaks
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容