Skip to content

Commit

Permalink
Merge pull request #48 from BaristaVentures/fix/stetho-memory-leak
Browse files Browse the repository at this point in the history
Fix stetho memory leak
  • Loading branch information
danielgomezrico authored Jun 22, 2017
2 parents 47bbcf0 + 6aac721 commit 9c05d15
Show file tree
Hide file tree
Showing 22 changed files with 341 additions and 67 deletions.
25 changes: 6 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,15 @@
Make developers life easier with some tools, you can add it to your debug builds and have some debug
libraries like [Leakcanary](https://github.com/square/leakcanary) and others without extra work.

Library contains:
- [Debug Drawer Menu](docs/debug-drawer.md): right menu with features for development, (*yeah, it's for you! developer!*).
* __Note:__ check [shake to report bug feature](docs/custom-bug-report-service.md)
The [Debug Drawer Menu](docs/debug-drawer.md): right menu with features for development.
![](img/sample.gif)

- [ViewServer](https://github.com/romainguy/ViewServer): helps to debug layouts with more devices.
- __Note:__ check [sample](sample/) for a working project using this library to see how it works.
- __Note:__ check [shake to report bug feature](docs/custom-bug-report-service.md)

## Some Comments

- If you want to differentiate release/staging/debug builds on the drawer you need to add:

```
buildTypes {
debug {
versionNameSuffix "-staging" // THIS LINE
//...
}
//...
}
```

Looks like we cant get
Contains [ViewServer](https://github.com/romainguy/ViewServer) to help you to debug layouts with more devices.

## Add to gradle project

Expand Down
4 changes: 2 additions & 2 deletions deps.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ ext {
targetSdk : 25]

versions = [supportLibrary: "25.3.1",
kotlin : "1.1.2-3",
kotlin : "1.1.2-5",
retrofit : "2.3.0",
okHttp : "3.8.0",
okHttp : "3.8.1",
rxJava : "1.3.0"]

plugs = [kotlin : "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}",
Expand Down
15 changes: 10 additions & 5 deletions docs/debug-drawer.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
# Debug Drawer

![](img/debug-drawer.jpg)

## How to use it

**Please** check the [sample/](sample/) project.
**Please** check the `sample/` project.

1. Add this on `BaseActivity.onResume` so your child activities always add it to each activity:

- _Note: You **should** create a `BaseActivity` class and make everything inherith from it, the idea is that
you just put this code in your `debug` buildType and not in `release`._

1. Add this on each activity `onResume` so your child activities always add it to each activity:
- _Note: you build your own menu with the features you want_

Java:
```java
@Override
private DebugDrawer mDebugDrawer;

@Override
protected void onResume() {
super.onResume();

Expand Down
Binary file added img/sample.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ dependencies {
compile('com.mikepenz:materialdrawer:5.3.6@aar') { transitive = true }

// Features
compile 'com.squareup.leakcanary:leakcanary-android:1.5'
compile 'com.squareup.leakcanary:leakcanary-android:1.5.1'

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.jakewharton.scalpel:scalpel:1.1.2'
compile 'com.facebook.stetho:stetho-okhttp:1.5.0'
compile 'com.github.pedrovgs:lynx:1.6'
compile 'com.jakewharton:process-phoenix:1.1.1'
compile 'nl.littlerobots.rxlint:rxlint:1.3'
compile 'com.facebook.stetho:stetho:1.5.0'

// Bug Reporter
compile "io.reactivex:rxjava:${versions.rxJava}"
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/debug_artist/menu/DebugActor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package debug_artist.menu
import android.app.Activity
import android.app.Application
import android.util.Log
import debug_artist.menu.drawer.Actor
import com.facebook.stetho.Stetho
import com.github.pedrovgs.lynx.LynxActivity
import com.jakewharton.processphoenix.ProcessPhoenix
import com.jakewharton.scalpel.ScalpelFrameLayout
import com.squareup.leakcanary.LeakCanary
import com.squareup.picasso.Picasso
import debug_artist.menu.drawer.Actor
import java.lang.ref.WeakReference

class DebugActor(application: Application, activity: Activity) : Actor {
Expand Down Expand Up @@ -46,7 +46,7 @@ class DebugActor(application: Application, activity: Activity) : Actor {
override fun disableScalpelLayout() = enableScalpelLayout(false)

override fun enableStetho() {
activityWeakReference.get()?.let {
applicationWeakReference.get()?.let {
Stetho.initializeWithDefaults(it)
Log.i(TAG, "Stetho enabled")
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/debug_artist/menu/drawer/DebugDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class DebugDrawer @JvmOverloads constructor(application: Application,
AlertDialog.Builder(activity)
.setTitle(drawerItem.name.toString())
.setView(entryView)
.setPositiveButton(android.R.string.yes) { dialog, which ->
.setPositiveButton(android.R.string.yes) { _, _ ->
val inputText = entryView.text.toString()
presenter.onTextInputEntered(drawerItem.tag as Int, inputText)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import debug_artist.menu.R
import debug_artist.menu.report_bug.BugRepository
import java.lang.ref.WeakReference

class ReportBugActivity : AppCompatActivity(), ReportBugView {
Expand Down Expand Up @@ -43,7 +42,7 @@ class ReportBugActivity : AppCompatActivity(), ReportBugView {
(findViewById(R.id.descriptionEditText) as EditText).text.toString())
}

(findViewById(R.id.descriptionEditText) as EditText).setOnEditorActionListener { textView, id, keyEvent ->
(findViewById(R.id.descriptionEditText) as EditText).setOnEditorActionListener { _, id, keyEvent ->
if (id == EditorInfo.IME_NULL && keyEvent.action == KeyEvent.ACTION_DOWN) {
presenter.onSendButtonClick((findViewById(R.id.titleEditText) as EditText).text.toString(),
(findViewById(R.id.descriptionEditText) as EditText).text.toString())
Expand Down
13 changes: 11 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ android {

}


def versions = [retrofit : "2.2.0",
okhttp : "3.8.0"]

dependencies {
compile deps.appCompat
compile project(':lib')
compile project(':reporter-pivotal')

// debugCompile("com.baristav.debugartist:debugartist:0.6.1@aar") { transitive = true }
// For StethoActivity
compile 'com.facebook.stetho:stetho-okhttp3:1.5.0'
compile "com.squareup.retrofit2:retrofit:${versions.retrofit}"
compile "com.squareup.retrofit2:converter-gson:${versions.retrofit}"
compile "com.squareup.retrofit2:converter-gson:${versions.retrofit}"
compile "com.squareup.okhttp3:okhttp:${versions.okhttp}"
compile "com.squareup.okhttp3:logging-interceptor:${versions.okhttp}"

// debugCompile("com.baristav.debugartist:debugartist:0.6.1@aar") { transitive = true }
// debugCompile("com.baristav.debugartist:reporter_pivotal:0.6.1@aar") { transitive = true }
}
21 changes: 19 additions & 2 deletions sample/rules.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RXJava
## RXJava
-dontwarn java.lang.invoke.*

# To Remove the configuration keeps the entry point ... but not the descriptor class ...
Expand Down Expand Up @@ -45,4 +45,21 @@
-keep class android.support.v4.app.Fragment
-keep class android.support.v4.app.Activity
-keep class android.support.design.widget.FloatingActionButton
-keep class android.support.v7.widget.AppCompatImageHelper
-keep class android.support.v7.widget.AppCompatImageHelper

## Retrofit

# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

# OkHttp3
-dontwarn okhttp3.**
-dontwarn okio.**

-dontwarn com.squareup.picasso.**
6 changes: 3 additions & 3 deletions sample/src/debug/java/debug_artist/sample/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class BaseActivity extends AppCompatActivity
implements SpinnerItemListener, RestartListener, InputItemListener {

private DebugDrawer mDebugDrawer;
private DebugDrawer debugDrawer;

@Override
protected void onResume() {
Expand All @@ -36,7 +36,7 @@ protected void onResume() {
BaseApplication applicationInstance = BaseApplication.sInstance;

// Create debug drawer with selected features
mDebugDrawer = new DebugDrawer(applicationInstance, this)
debugDrawer = new DebugDrawer(applicationInstance, this)
.withScalpelSwitch((ScalpelFrameLayout) findViewById(R.id.scalpelLayout))
.withLeakCanarySwitch(true)
.withPicassoLogsSwitch(true)
Expand All @@ -59,7 +59,7 @@ protected void onResume() {
protected void onPause() {
super.onPause();

mDebugDrawer.release();
debugDrawer.release();
ViewServer.get(this).removeWindow(this);
}

Expand Down
10 changes: 9 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:theme="@style/AppTheme">

<activity
android:name=".MainActivity"
android:name=".LauncherActivity"
android:label="@string/sample">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -22,6 +22,14 @@

<activity android:name="debug_artist.menu.report_bug.ReportBugActivity" />

<activity
android:name=".base.BasicActivity"
android:label="Basic Sample" />

<activity
android:name=".stetho.StethoActivity"
android:label="Stetho Sample" />

</application>

</manifest>
34 changes: 34 additions & 0 deletions sample/src/main/java/debug_artist/sample/LauncherActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package debug_artist.sample;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import debug_artist.sample.base.BasicActivity;
import debug_artist.sample.stetho.StethoActivity;

public class LauncherActivity extends BaseActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

findViewById(R.id.basicButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LauncherActivity.this, BasicActivity.class));
}
});

findViewById(R.id.stethoButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LauncherActivity.this, StethoActivity.class));
}
});
}

}
20 changes: 0 additions & 20 deletions sample/src/main/java/debug_artist/sample/MainActivity.java

This file was deleted.

24 changes: 24 additions & 0 deletions sample/src/main/java/debug_artist/sample/base/BasicActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package debug_artist.sample.base;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import debug_artist.sample.BaseActivity;
import debug_artist.sample.R;

public class BasicActivity extends BaseActivity {

public static BasicActivity activityLeaked;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

// Intentionally to leak memory and see it with leak canary
activityLeaked = this;

// Check BaseActivity for Debug Artist Initialization
}
}
13 changes: 13 additions & 0 deletions sample/src/main/java/debug_artist/sample/stetho/RandomService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package debug_artist.sample.stetho;

import okhttp3.Request;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

interface RandomService {

// Just a get to google.com, we dont need to parse the results, just do a request that works forever :P
@GET("/")
Call<Request> get(@Query("format") String format);
}
Loading

0 comments on commit 9c05d15

Please # to comment.