利用 LeakCanary 来检查 Android 内存泄漏 6.0以上版本空指针解决

LeakCanary 是一个开源的在debug(Relese)版本中检测内存泄漏的java库,链接:

https://github.com/square/leakcanary

你也许会遇到如下类似错误

1.3. 在6.0预览版报错

* FAILURE:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

at com.squareup.leakcanary.HeapAnalyzer.findLeakingReference(HeapAnalyzer.java:160)

at com.squareup.leakcanary.HeapAnalyzer.checkForLeak(HeapAnalyzer.java:95)

at com.squareup.leakcanary.internal.HeapAnalyzerService.onHandleIntent(HeapAnalyzerService.java:57)

at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:148)

at android.os.HandlerThread.run(HandlerThread.java:61)

之前我们的配置也许是这样的:

dependencies { debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'

releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'

}

在Application中:

publicclassExampleApplicationextendsApplication{

@OverridepublicvoidonCreate(){

super.onCreate();

LeakCanary.install(this);}}


新更新的SDK 1.5版本,已经完美解决、android6.0以上运行时权限,Notification无法弹出的问题,需要重新配置

  build.gradle:

 dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
   testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
 }
public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    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);
    // Normal app init code...
  }
}

你可能感兴趣的:(Android开发)