LeakCanary2的免写 初始化代码 原理

栏目: IOS · Android · 发布时间: 5年前

内容简介:最近按照以前的使用流程,一般我们都是在dependencies 加入依赖接着在我们的application里面加入初始化的逻辑。

最近 LeakCanary 做了升级,发布了2.0版本,带了了很多性能上的优化,不过一个很吸引我的点在于,他居然不像以前一样,需要手动初始化了。

按照以前的使用流程,一般我们都是在dependencies 加入依赖

dependencies {
  debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.3'
  releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
  // Optional, if you use support library fragments:
  debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3'
}	
复制代码

接着在我们的application里面加入初始化的逻辑。

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...
	  }
	}
复制代码

但是,新版本的 LeakCanary 2.0居然可以不再需要写这个操作,只需要在代码里面加入这么一句依赖就可以了

dependencies {
  debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-1'
}
复制代码

我有点怀疑自己的眼睛,重新看了下他们的 Readme ,是不是真的

Getting started Add LeakCanary to your build.gradle:

dependencies { debugImplementation com.squareup.leakcanary:leakcanary-android:2.0-alpha-1' }

You're good to go!LeakCanary will automatically show a notification when an activity or fragment memory leak is detected in your debug build.

好吧,确实是这样,那么到底怎么做到的?很神奇啊,这怎么也会有一个地方会需要初始化的,到底换到那里去了?

在经过对源码的解读后,发现了一个骚操作,感觉传开后,以后的sdk库都可能这么做,教坏小朋友了。

ContentProvider

在经过对源码的阅读后,发现其实人家是基于CP这个对于绝大数开发来说,基本没用到的四大组件之一来做的,真的是服了哈哈。查看他的 leakcanary-leaksentry 模块的 AndroidManifest.xml 文件,可以看到下面的内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.squareup.leakcanary.leaksentry"
    >

  <application>
    <provider
        android:name="leakcanary.internal.LeakSentryInstaller"
        android:authorities="${applicationId}.leak-sentry-installer"
        android:exported="false"/>
  </application>
</manifest>
复制代码

接着我们去看下那 LeakSentryInstaller 这个类到底做了什么。

internal class LeakSentryInstaller : ContentProvider() {

  override fun onCreate(): Boolean {
    CanaryLog.logger = DefaultCanaryLog()
    val application = context!!.applicationContext as Application
    InternalLeakSentry.install(application)  
    //骚操作在这里,利用系统自动调用CP的onCreate方法来做初始化
    return true
  }

  override fun query(
    uri: Uri,
    strings: Array<String>?,
    s: String?,
    strings1: Array<String>?,
    s1: String?
  ): Cursor? {
    return null
  }

  override fun getType(uri: Uri): String? {
    return null
  }

  override fun insert(
    uri: Uri,
    contentValues: ContentValues?
  ): Uri? {
    return null
  }

  override fun delete(
    uri: Uri,
    s: String?,
    strings: Array<String>?
  ): Int {
    return 0
  }

  override fun update(
    uri: Uri,
    contentValues: ContentValues?,
    s: String?,
    strings: Array<String>?
  ): Int {
    return 0
  }
}
复制代码

我们看到这个CP类,没做任何的CURD操作,全是空的,就纯粹利用系统会回调这个接口来做初始化,帮助开发偷懒,省去调用写初始化逻辑。

个人看待这个,觉得得分两部门 好处:确实带来了 "免侵入" ,不需要业务人员写任何代码。一般启动的顺序是 Application->attachBaseContext =====>ContentProvider->onCreate =====>Application->onCreate =====>Activity->onCreate 所以对于大多数场景,写在CP的初始化的实际是足够优先了!!

坏处:这有点把CP给用歪了。以后所有人都这么弄,接入的sdk都这么写的话,那就真的可爱了。


以上所述就是小编给大家介绍的《LeakCanary2的免写 初始化代码 原理》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

大连接

大连接

[美] 尼古拉斯•克里斯塔基斯(Nicholas A. Christakis)、[美] 詹姆斯•富勒(James H. Fowler) / 简学 / 中国人民大学出版社 / 2013-1 / 59.90元

[内容简介] 1. 本书是继《六度分隔》之后,社会科学领域最重要的作品。作者发现:相距三度之内是强连接,强连接可以引发行为;相聚超过三度是弱连接,弱连接只能传递信息。 2. 本书讲述了社会网络是如何形成的以及对人类现实行为的影响,如对人类的情绪、亲密关系、健康、经济的运行和政治的影响等,并特别指出,三度影响力(即朋友的朋友的朋友也能影响到你)是社会化网络的强连接原则,决定着社会化网络的......一起来看看 《大连接》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试