一文详解无痕埋点在Android中的实现

作者:小宝一号 时间:2022-06-30 07:53:18 

前言

本篇技术实现主要是运行是代理,不涉及到插桩技术,不引入插件,对业务影响点最小

技术难点

1. 如何拦截到所有的view的点击事件

view有个setAccessibilityDelegate方法可以通过自定义一个全局的AccessibilityDelegate对象来监听view的点击事件

object EventTrackerAccessibilityDelegate : View.AccessibilityDelegate() {

override fun sendAccessibilityEvent(host: View?, eventType: Int) {
       super.sendAccessibilityEvent(host, eventType)
       if (eventType == AccessibilityEvent.TYPE_VIEW_CLICKED) {
           host?.let {
               // 统一做埋点
           }
       }
   }
}

通过给每个View设置上述单例对象,这样每当View被点击时,View.performClick内部就会触发上述方法。这样就能够拦截view的点击事件,而不用修改业务层代码。

2. 如何对app所有的view设置setAccessibilityDelegate

解决这个问题,就得拦截到app中view的创建。我们先要对Android中View的创建流程需要明白,对于android中的view创建,我们先从AppCompatActivity.onCreate方法入手

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
 final AppCompatDelegate delegate = getDelegate();
 delegate.installViewFactory(); //重点
 delegate.onCreate(savedInstanceState);
 super.onCreate(savedInstanceState);
}

我们重点看installViewFactory方法,delegate返回的实际类型为AppCompatDelegateImpl,它继承了AppCompatDelegate抽象类

// AppCompatDelegateImpl.java
@Override
public void installViewFactory() {
 LayoutInflater layoutInflater = LayoutInflater.from(mContext);
 if (layoutInflater.getFactory() == null) {
   LayoutInflaterCompat.setFactory2(layoutInflater, this);
 } else {
   if (!(layoutInflater.getFactory2() instanceof AppCompatDelegateImpl)) {
     Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed"
           + " so we can not install AppCompat's");
   }
 }
}

这里面可以看到内部调用了LayoutInflaterCompat**.setFactory2方法,第二个参数传入了this;其实可以理解view的创建托管给了AppCompatDelegateImpl.onCreateView了;我们继续看onCreateView**内部做了什么

public View createView(View parent, final String name, @NonNull Context context,
           @NonNull AttributeSet attrs) {

if (mAppCompatViewInflater == null) {
           TypedArray a = mContext.obtainStyledAttributes(R.styleable.AppCompatTheme);
           String viewInflaterClassName =
                   a.getString(R.styleable.AppCompatTheme_viewInflaterClass);
           // 读取当前活动theme中是否声明了viewInflaterClass属性,
           // 如果没有就创建一个AppCompatViewInflater对象,否则使用自定义属性对象
           if ((viewInflaterClassName == null)
                   || AppCompatViewInflater.class.getName().equals(viewInflaterClassName)) {
               // Either default class name or set explicitly to null. In both cases
               // create the base inflater (no reflection)
               mAppCompatViewInflater = new AppCompatViewInflater();
           } else {
               try {
                   Class<?> viewInflaterClass = Class.forName(viewInflaterClassName);
                   mAppCompatViewInflater =
                           (AppCompatViewInflater) viewInflaterClass.getDeclaredConstructor()
                                   .newInstance();
               } catch (Throwable t) {
                   Log.i(TAG, "Failed to instantiate custom view inflater "
                           + viewInflaterClassName + ". Falling back to default.", t);
                   mAppCompatViewInflater = new AppCompatViewInflater();
               }
           }
       }

...
       // 返回view

return mAppCompatViewInflater.createView(parent, name, context, attrs, inheritContext,
               IS_PRE_LOLLIPOP, /* Only read android:theme pre-L (L+ handles this anyway) */
               true, /* Read read app:theme as a fallback at all times for legacy reasons */
               VectorEnabledTintResources.shouldBeUsed() /* Only tint wrap the context if enabled */
       );
   }

从上述代码可以看到负责view的创建的其实是mAppCompatViewInflater对象;思路来了,我们可以通过自定义主题样式中viewInflaterClass属性,来接管view的创建

Style.xml中添加配置

<!-- Base application theme. -->
   <style name="AppTheme" parent="AppThemeBase" >
       ...
       <item name="viewInflaterClass">com.dbs.module.framework.event.tracker.DBSAppCompatViewInflater</item>
   </style>

view创建

@Keep
class DBSAppCompatViewInflater : AppCompatViewInflater() {

private val mViewCreateHelper by lazy { ViewCreateHelper() }

override fun createView(context: Context?, name: String?, attrs: AttributeSet?): View? {
       return when (name) {
               try {
                   mViewCreateHelper.createViewFromTag(context, name, attrs)
               } catch (e: Exception) {
                   // noNeed throw exception, just return null
                   null
               }
       }
   }
}

ViewCreateHelper主要是通过全路径名以反射形式创建view;你可以参考AppCompatViewInflater类中实现

DBSAppCompatViewInflater方法我们实现了自定义view的方法;(但它只是view创建的一部分,所以此处没有对view设置EventTrackerAccessibilityDelegate),外部调用的只是AppCompatViewInflater.createView

所以为了拦截所有view的创建,我们需要对activity中getDelagate方法做包装; 有人可能会想能不能自定义Delegate,自己实现AppCompatDelegate抽象类吗?;答案是不行(抽象类中声明了私有方法,子类直接继承编译报错)也不建议这样做,自定义类去做需要实现许多方法,稳定性太差;能不能直接继承AppCompatDelegateImpl类呢?答案也是不行

@RestrictTo(LIBRARY)
class AppCompatDelegateImpl extends AppCompatDelegate
       implements MenuBuilder.Callback, LayoutInflater.Factory2 {
}

从源码可以看出compat包中对AppCompatDelegateImpl类做了限制,只能用在那个库中LIBRARY中使用

Restrict usage to code within the same library (e.g. the same gradle group ID and artifact ID).

所以我们只能对Delegate增加一层包装,delegate现在已经拥有创建view的能力,我们只要在install之前对LayoutInflater设置Factory2中方法,在方法中直接引用delegate对象创建view就可以了;

实现一个LayoutIInflater.Factory2接口

class AppLayoutInflaterFactory2Proxy(private val delegate: AppCompatDelegate)
   : LayoutInflater.Factory2 {

override fun onCreateView(parent: View?, name: String?, context: Context?, attrs: AttributeSet): View? {
       context ?: return null
       delegate.createView(parent, name, context, attrs)?.apply {
               // 无痕埋点启用,则绑定,否则不做处理
               if (EventAutoTrackerCfg.enable) {
                   if (ViewCompat.getAccessibilityDelegate(this) == null) {
                       accessibilityDelegate = EventTrackerAccessibilityDelegate
                   }
               }
           }
   }

override fun onCreateView(name: String?, context: Context?, attrs: AttributeSet): View? {
       return onCreateView(null, name, context, attrs)
   }
}

Activity基类中复写getDelegate方法

override fun getDelegate(): AppCompatDelegate {
       val delegate =  super.getDelegate()
       try {
           val inflater = LayoutInflater.from(this)
           // avoid throw exception when invoking method multiple times
           if (inflater.factory == null) {
               LayoutInflaterCompat.setFactory2(inflater, MKAppLayoutInflaterFactory2Proxy(delegate) )
           }
       } catch (e: Exception) {
           // do nothing
       }
       return delegate
   }

这样整个无痕埋点技术实现方案已经完成了

可以优化的点

当前技术实现中需要在Style.xml中添加相关viewInflaterClass配置,有些耦合

优化技术实现方案:可以通过插桩方式修改viewInflaterClassName的值,对于我们自己业务类(通过context判断)设置我们自定义的InflaterClassName,第三方sdk可以控制保持不变

一文详解无痕埋点在Android中的实现

总结 

来源:https://blog.csdn.net/dbs1215/article/details/128857690

标签:android,无痕埋点
0
投稿

猜你喜欢

  • C#中使用XmlDocument类来创建和修改XML格式的数据文件

    2023-09-19 16:33:45
  • 使用 C# 动态编译代码和执行的代码

    2023-07-22 23:46:27
  • Swagger注解-@ApiModel和@ApiModelProperty的用法

    2023-02-05 23:57:48
  • SpringBoot使用WebSocket的方法实例详解

    2022-12-26 03:19:25
  • c# AcceptEx与完成端口(IOCP)结合的示例

    2023-07-29 01:46:24
  • C#实现一键清空控件值的示例代码

    2021-11-12 12:10:15
  • java的arraylist排序示例(arraylist用法)

    2023-01-15 06:55:37
  • spring boot项目没有mainClass如何实现打包运行

    2021-10-24 11:29:20
  • java实现通过绑定邮箱找回密码功能

    2021-12-17 00:16:48
  • C#从命令行读取参数的方法

    2023-07-12 15:23:11
  • 解决WPF附加属性的Set函数不调用的问题

    2022-09-27 09:52:38
  • SpringCloud全面解析@FeignClient标识接口的过程

    2023-08-05 12:34:44
  • C++编程中用put输出单个字符和cin输入流的用法

    2023-05-28 06:14:49
  • 浅谈Spring Boot 开发REST接口最佳实践

    2021-10-08 12:24:35
  • SpringBoot MongoDB 索引冲突分析及解决方法

    2023-01-23 06:28:05
  • java AOP原理以及实例用法总结

    2022-11-05 03:30:41
  • Android 类似UC浏览器的效果:向上滑动地址栏隐藏功能

    2023-01-29 05:01:41
  • 基于java实现斗地主代码实例解析

    2023-09-07 00:31:15
  • IDEA插件EasyCode及MyBatis最优配置步骤详解

    2023-11-09 03:19:19
  • spring boot 实现配置多个DispatcherServlet最简单方式

    2023-12-18 03:24:46
  • asp之家 软件编程 m.aspxhome.com