使用LeakCanary遇到的问题 就是不弹出来

今天楼主遇到引用LeakCanary时代码跟官网一样但是就不弹出来。楼主新建项目就可以正常使用。楼主郁闷半天,现在终于整出来了。

楼主主工程app引用module为thirdParty,本想为了整洁三方的都扔进这个thirdParty 结果导致了这个没弄出来。

1.写一个application :

 


public class BaseApplication extends Application {
    private static BaseApplication app;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private SQLiteDatabase db;
    private String TAG="BaseApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
        app = this;

        LeakCanary.install(this);
        initDB();
    }
2.写一段让程序泄露的代码:

 void startAsyncTask() {

        // This async task is an anonymous class and therefore has a hidden reference to the outer
        // class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation),
        // the activity instance will leak.
        new AsyncTask() {
            @Override
            protected Void doInBackground(Void... params) {
                // Do some slow work in background
                SystemClock.sleep(200000);
                return null;
            }
        }.execute();
        Toast.makeText(this, "请关闭这个A完成泄露", Toast.LENGTH_SHORT).show();
    }

3.添加build.gradle 由于我把所有的依赖都写在的thirdParty这个lib中

仔细观察一下这个引用不太一样:

我们平时引用都是:

compile 'com.github.lzyzsd.randomcolor:library:1.0.0'
而LeakCanary的引用是:

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

后来我又写到了主app中 结果可以使用了。。。

查了一下他们的含义:

Compile

compile是对所有的build type以及favlors都会参与编译并且打包到最终的apk文件中。

Provided

Provided是对所有的build type以及favlors只在编译时使用,类似eclipse中的external-libs,只参与编译,不打包到最终apk。

APK

只会打包到apk文件中,而不参与编译,所以不能再代码中直接调用jar中的类或方法,否则在编译时会报错

Test compile

Test compile 仅仅是针对单元测试代码的编译编译以及最终打包测试apk时有效,而对正常的debug或者release apk包不起作用。

Debug compile

Debug compile 仅仅针对debug模式的编译和最终的debug apk打包。

Release compile

Release compile 仅仅针对Release 模式的编译和最终的Release apk打包。

还不太清楚为啥。。。但是给大家提个醒。

下面转一个非常好的文章:http://droidyue.com/blog/2016/03/28/android-leakcanary/

----------------------------------------------------------------------------------------------

时隔多年新项目又一次集成leak 图标有了但是结果就是不监控。。。弄了一下午。。。

解决方案:

1.关闭instant run

2.删除手机上的app

3.as 执行clean命令

4.按照我的git 上的demo去初始化 

5.安装apk 等待leak安装监控程序。

6.再次打开,执行泄漏代码,等待黄色提示。

7.打开黄色图标,泄漏提示你一定会看到!

贴上部分重点设置:

    implementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
public class MApplication extends Application {
    @Override public void onCreate() {
        super.onCreate();
        setupLeakCanary();
    }
    public static RefWatcher getRefWatcher(Context context) {
        MApplication application = (MApplication) context.getApplicationContext();
        return application.refWatcher;
    }

    private RefWatcher refWatcher;
    protected void setupLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
//        enabledStrictMode();
        refWatcher=LeakCanary.install(this);
    }
public class HandlerActivity extends AppCompatActivity {
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // do something you want
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler);

        mHandler.sendMessageDelayed(Message.obtain(), 10000);
        //just finish this activity
        try {
            Thread.sleep(2000);
            finish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = MApplication.getRefWatcher(this);
        refWatcher.watch(this);
    }

}

还有就是提供读取权限

    
    

如果解决了您的问题,欢迎star!

https://github.com/amsterly/LeakCanary

你可能感兴趣的:(Android,Error,Solution,ThirdSupport)