AndroidX(1)androidx.core.core:1.0.0解析

前言

AndroidX出来一段时间了,从09年开始一直在做公司的quickstart和组件化的东西,基础框架搭建这一块也算是有一些成绩。从原来的功能的抽象的封装,到现在系统性质研究。

正文

新的项目启动,有利于做一些前沿技术的思想的学习和吸收。androidx问世以后,从长远角度出发,确实是android开发者的福音。谷歌终于开始布局对于开发者友好以一块的事情了。也许是长期依赖开发者对兼容包诟病很多,不管谷歌出于什么样的目的,长远看来确实是一件好事

然而,实际并没有那么简单

在开始使用androidx的时候也碰到了一些棘手的事情,比如如何将旧项目迁移到androidx上面来,还好google在as IDE上面增加了Migrate功能。虽然这个功能并不能搞定所有的事情,但是最起码是一个好的开始。修改的工作量稍微降低了一些。可能最费劲的要属material库的修改,并不能很好的支持迁移

废话不多说,今天就从core包的几个类讲起,他们分别是:

  • ContextCompat
  • IntentCompat
  • ActivityCompat
  • ActivityOptionsCompat

通过对这几个类的源码进行解读我们不难发现,这个兼容类,是为了帮我们处理不同Build.VERSION.SDK_INT 所对应的实现相同功能的不同调用方式
注意:⚠️SDK_INT 这个版本运行在硬件设备上的SDK版本,这个值不会被改变,除非硬件厂商提供OTA更新

 /**
         * The SDK version of the software currently running on this hardware
         * device. This value never changes while a device is booted, but it may
         * increase when the hardware manufacturer provides an OTA update.
         * <p>
         * Possible values are defined in {@link Build.VERSION_CODES}.
         */
        public static final int SDK_INT = SystemProperties.getInt(
                "ro.build.version.sdk", 0);

这里我们举例说明一下

ContextCompat

 /**
     * Return the handle to a system-level service by class.
     *
     * @param context Context to retrieve service from.
     * @param serviceClass The class of the desired service.
     * @return The service or null if the class is not a supported system service.
     *
     * @see Context#getSystemService(Class)
     */
    @SuppressWarnings("unchecked")
    @Nullable
    public static <T> T getSystemService(@NonNull Context context, @NonNull Class<T> serviceClass) {
        if (Build.VERSION.SDK_INT >= 23) {
            return context.getSystemService(serviceClass);
        }

        String serviceName = getSystemServiceName(context, serviceClass);
        return serviceName != null ? (T) context.getSystemService(serviceName) : null;
    }

    /**
     * Gets the name of the system-level service that is represented by the specified class.
     *
     * @param context Context to retrieve service name from.
     * @param serviceClass The class of the desired service.
     * @return The service name or null if the class is not a supported system service.
     *
     * @see Context#getSystemServiceName(Class)
     */
    @Nullable
    public static String getSystemServiceName(@NonNull Context context,
            @NonNull Class<?> serviceClass) {
        if (Build.VERSION.SDK_INT >= 23) {
            return context.getSystemServiceName(serviceClass);
        }
        return LegacyServiceMapHolder.SERVICES.get(serviceClass);
    }

上面这端代码在获取SystemSerivce的时候会进行SDK_INT的判断,选择不同的获取service的方法。
我们进入context两种获取service的方法看看代码


public abstract @Nullable Object getSystemService(@ServiceName @NonNull String name);

 public final @Nullable <T> T getSystemService(@NonNull Class<T> serviceClass) {
        // Because subclasses may override getSystemService(String) we cannot
        // perform a lookup by class alone.  We must first map the class to its
        // service name then invoke the string-based method.
        String serviceName = getSystemServiceName(serviceClass);
        return serviceName != null ? (T)getSystemService(serviceName) : null;
    }

咋一开并不是很明白,通过类获取服务的方式的最终实现还是通过类的名字进行获取的,这里我们先不去关心他为什么要这样做,只关注他的实现最终希望达到的效果你就明白了

core包下面的这些Compat类都是为了方便开发者去调用的东西,帮助开发者屏蔽掉底层实现的差异性,从而简化应用层代码的编写。

所以长远看,androidx 确实是一个不错的选择,也是google android想对这一块进行整改和优化,如今的android硬件和软件都已经可以媲美ios了,价格也便宜,甚至在竞合关系的大浪潮下取得了骄人成绩,甚至超越了苹果,打破了只能模仿从未超越的局面,成功逆袭。

好听的说太多,并不好,这里还是要并着批判的态度看待问题,我刚才说过从长远看,androidx是未来的方向和趋势,现在是个什么样子呢?有没有什么问题呢?
这里我仅仅推荐一篇文章供大家参考,后续会增加更多相关的文章进行整理,方便大家查阅和解决手上棘手的bug
总是听到有人说AndroidX,到底什么是AndroidX?

慎用 AndroidX 库

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章