Make LeakCanary a Separate Plugin

Summary: Pull Request resolved: https://github.com/facebook/flipper/pull/559

Reviewed By: passy

Differential Revision: D17546936

fbshipit-source-id: dce128a771b73e8b18dfc2b78ee99502c3f1ce86
This commit is contained in:
Chaiwat Ekkaewnumchai
2019-09-25 03:03:39 -07:00
committed by Facebook Github Bot
parent a5f17494d5
commit 053cfc09f3
9 changed files with 55 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
apply plugin: 'com.android.library'
apply plugin: 'maven'
android {
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
}
dependencies {
implementation project(':android')
implementation deps.leakcanary
compileOnly deps.jsr305
}
}
apply from: rootProject.file('gradle/release.gradle')

View File

@@ -0,0 +1,12 @@
#
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the LICENSE
# file in the root directory of this source tree.
#
POM_NAME=Flipper LeakCanary Plugin
POM_DESCRIPTION=LeakCanary plugin for Flipper
POM_ARTIFACT_ID=flipper-leakcanary-plugin
POM_PACKAGING=aar

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) Facebook, Inc. and its affiliates.
~
~ This source code is licensed under the MIT license found in the LICENSE
~ file in the root directory of this source tree.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.facebook.flipper.plugins.leakcanary">
</manifest>

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
package com.facebook.flipper.plugins.leakcanary;
import android.util.Log;
import com.facebook.flipper.core.FlipperConnection;
import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.core.FlipperReceiver;
import com.facebook.flipper.core.FlipperResponder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class LeakCanaryFlipperPlugin implements FlipperPlugin {
private static final String TAG = "LeakCanaryFlipperPlugin";
private static final String REPORT_LEAK_EVENT = "reportLeak";
private static final String CLEAR_EVENT = "clear";
private static final String LEAKS_KEY = "leaks";
private FlipperConnection mConnection;
private final List<String> leakList = new ArrayList<>();
@Override
public String getId() {
return "LeakCanary";
}
@Override
public void onConnect(FlipperConnection connection) {
mConnection = connection;
sendLeakList();
mConnection.receive(
CLEAR_EVENT,
new FlipperReceiver() {
@Override
public void onReceive(FlipperObject params, FlipperResponder responder) throws Exception {
leakList.clear();
}
});
}
@Override
public void onDisconnect() throws Exception {
mConnection = null;
}
@Override
public boolean runInBackground() {
return false;
}
private void sendLeakList() {
if (mConnection != null) {
JSONObject obj = new JSONObject();
try {
obj.put(LEAKS_KEY, new JSONArray(leakList));
mConnection.send(REPORT_LEAK_EVENT, new FlipperObject(obj));
} catch (JSONException e) {
Log.w(TAG, "Failure to serialize leak list: ", e);
}
}
}
public void reportLeak(String leakInfo) {
leakList.add(leakInfo);
sendLeakList();
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
package com.facebook.flipper.plugins.leakcanary;
import static com.squareup.leakcanary.LeakCanary.leakInfo;
import com.facebook.flipper.android.AndroidFlipperClient;
import com.facebook.flipper.core.FlipperClient;
import com.squareup.leakcanary.AbstractAnalysisResultService;
import com.squareup.leakcanary.AnalysisResult;
import com.squareup.leakcanary.HeapDump;
/**
* When a leak is detected, sends results to connected Flipper desktop app. In order to use this
* service in place of the default, a custom RefWatcher will need to be created See
* https://github.com/square/leakcanary/wiki/Customizing-LeakCanary#uploading-to-a-server
*/
public class RecordLeakService extends AbstractAnalysisResultService {
@Override
protected void onHeapAnalyzed(HeapDump heapDump, AnalysisResult result) {
final String leakInfo = leakInfo(this, heapDump, result, true);
final FlipperClient client = AndroidFlipperClient.getInstance(this);
if (client != null) {
final LeakCanaryFlipperPlugin plugin = client.getPlugin("LeakCanary");
if (plugin != null) {
plugin.reportLeak(leakInfo);
}
}
}
}