今天想用一下leakcanary(介绍点我)来检测下内存泄漏,到了githup上一看,已经到1.5.2版本了,没问题根据wiki直接compile就集成好了。
dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.2'
}
然后在application中初始化
public class TestApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initLeakCanary();
}
private RefWatcher mRefWatcher;
private void initLeakCanary() {
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
mRefWatcher = RefWatcher.DISABLED;
return;
}
mRefWatcher = LeakCanary.install(this);
}
}
至此都没什么问题,然而令人意想不到的事发生了,当我运行项目的时候,就报了这样的错误。
Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/squareup/leakcanary/watcher/R.class
这什么鬼,我也没在其他地方引入这个库啊怎么就报重复了??
本来今颠高高兴兴,你为什么要报这种错?
没办法,报错了就想办法解决吧,然后经过网上一通搜索,发现大部分文章都没用什么卵用。看过别人的demo后发现他们都是用的低版本的,难道是这个原因?然后紧接着搜到一篇文章中(链接)也提到了这点,验证了我的猜想。
最终结论就是版本过高引发的一些未知错误,所以将版本降低,即可解决。
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
运行了下,测试通过。
2017.9.29更新 使用1.4版本 release版本下 依然会有点问题,今天再去githup上看了下,已经发布了1.5.4版本了,我更新了最新版本,发现debug还是release版本都没有问题了,看来suqare团队已经解决了此前的问题了。
最后请使用这个版本:
debugCompile ‘com.squareup.leakcanary:leakcanary-android:1.5.4’
releaseCompile ‘com.squareup.leakcanary:leakcanary-android-no-op:1.5.4’