前言

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")@Nullablepublic 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)*/@Nullablepublic 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 库

AndroidX(1)androidx.core.core:1.0.0解析相关推荐

  1. android迁移androidx、极光推送(v4.9.0)配置

    一.版本配置 编译正常配置: buildToolsVersion       : "27.1.1", minSdkVersion           : 19, classpath ...

  2. Orchard Core 1.0.0 正式发布!

    James: Orchard 最早是微软的员工创造的开源项目,使用的技术架构可以说是非常优秀,源码值得学习.功能也非常强大,支持模块化.多租户.工作流等等功能,可以说是 .NET 世界的 WordPr ...

  3. could not resolve dependency: npm err! peer react@“^16.8.0 || ^17.0.0“ from @material-ui/core@4.12.4

    问题 在执行 npm install 时看到以下错误: could not resolve dependency: npm err! peer react@"^16.8.0 || ^17.0 ...

  4. 【SqlSugar 】SqlSugar 连接操作MySQL数据库+ASP.NET Core Web API 6.0

    SqlSugar 连接操作数据库+ASP.NET Core Web API 6.0 教程链接 Nuget 安装 连接mysql 新建数据库,表,获取数据库数据 教程链接 参考学习博客地址 sqlSug ...

  5. .NET5.0和SDK 5.0.100以及ASP.NET Core Runtime 5.0.0下载方法

    .net framework 5是一款非常实用的语言开发软件,这款软件的作用就是那个轻松的保障你的电脑软件简单运行正常,而这次带来的是.net framework 5版本,增加了非常多的功能和特色,一 ...

  6. Could not load file or assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5

    java 中调外部工具   做word转pdf ,没有生成pdf, log中出现 [开始转换文档... finish [WINWORD] convert. Could not load file or ...

  7. 成功解决xgboost.core.XGBoostError: b‘value 0 for Parameter num_class should be greater equal to 1‘

    成功解决xgboost.core.XGBoostError: b'value 0 for Parameter num_class should be greater equal to 1' 目录 解决 ...

  8. 【ASP.NET Core Web API 6.0 基础学习】

    ASP.NET Core Web API 6.0 基础学习 半夏创建Web Api 6.0教程 返回时间格式化 Swagger注释和版本控制 使用appsetting.json的数据 IOC注入,使用 ...

  9. 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal Contain

    tomcat 启动报错: 20-Aug-2019 11:23:42.807 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core. ...

  10. 解决Android studio遇见Could not find common.jar (android.arch.core:common:1.0.0).错误

    Error:Could not find common.jar (android.arch.core:common:1.0.0). Searched in the following location ...

最新文章

  1. 使用两个ThreadPool
  2. 云炬随笔20161223
  3. Java基础(三十二)JDBC(2)连接数据库
  4. Linux系统文件与目录权限管理
  5. 分析Java核心转储
  6. Python数据类型与运算符号
  7. linux屏幕怎么放大_02|初始Linux——Windows与Linux区别
  8. 摩托罗拉Edge真机谍照曝光:挖孔瀑布屏+骁龙765
  9. 加密SD卡的新型身份认证方案
  10. Android开发:Handler Runnable和Thread之间的区别和联系 应用--------------------看完本篇,从此一览无余!...
  11. pycharm2019安装包以及汉化文件
  12. PDF转CAD图纸,该如何转换呢?
  13. K8S 1.23 metrics-server及cadvisor 杂记
  14. 【有利可图网】PS实战系列:PS制作人像印章效果
  15. 关于谷歌浏览器全线崩溃的原因及几种解决办法(疑难杂症篇)
  16. python的dict
  17. 计算机网络技术动态路由配置,计算机网络实验六动态路由的配置
  18. 马士兵内部共享—1658页《Java面试突击核心讲》,面试神技
  19. python将中文转换成utf8_如何在python中从unicode转换成utf8?
  20. QGIS添加常用的数据源

热门文章

  1. 粒子群在小车机械手臂的应用
  2. Spring Security - 21 记住我功能
  3. Sklearn聚类算法之Affinity Propagation
  4. 一万年太久,只争朝夕,男儿有泪不轻弹,只是未到伤心处!!!!!!
  5. 蓝牙技术|伦茨科技智能语音遥控器方案简介
  6. AI新技术:利用神经网络对图片进行超级压缩
  7. 苹果手机解压缩软件_Mac用户必备的几款压缩与解压缩软件!滴!滴!滴!
  8. Orientation模块管理设备的方向信息,包括alpha、beta、gamma三个方向信息,通过plus.orientation可获取设备方向管理对象
  9. Morris 中序遍历
  10. 如何拟合幂率分布的幂率