当ButterKnife8.8.1碰到AndroidX怎么办

2019-11-26 03:48:10

参考地址 当ButterKnife8.8.1碰到AndroidX怎么办


前言

我希望我的文章不要弯弯绕绕,直接贴代码希望大家可以看得懂,看完文章可以达到学习的第一个里程碑,至于后面原理慢慢的补上就是了,毕竟业务和解决问题优先嘛。


按照官方文档说明 androidx 是对 android.support.xxx 包的整理后产物。由于之前的support包过于混乱,所以,google推出了 androidX。


环境以及工具

AndroidStudio3.3


报错展示

Caused by: java.lang.RuntimeException: Failed to transform ‘C:\Users\admin.gradle\caches\modules-2\files-2.1\com.jakewharton\butterknife-compiler\8.8.1\b2f4505a1babb7b7c11abbbf8ea4c90b18c3aeac\butterknife-compiler-8.8.1.jar’ using Jetifier. Reason: The given artifact contains a string literal with a package reference ‘android.support.v4.content’ that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx… (Run with --stacktrace for more details.)


Caused by: java.lang.IllegalArgumentException: The given artifact contains a string literal with a package reference ‘android.support.v4.content’ that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.


问题分析

报错看重点

butterknife-compiler-8.8.1.jar’ using Jetifier


need to be updated manually to add support for androidx


The given artifact contains a string literal with a package reference ‘android.support.v4.content’ that cannot be safely rewritten.


既然定位到ButterKnife的问题,那就直接去JakeWharton的butterknife提问中找答案,猜测应该有人踩过坑。




在githup JakeWharton的项目中一搜,果然,前辈还是没让我失望的。






ClassName.get(“android.support.v4.content”, “ContextCompat”);由于在Androidx项目已经找不到android.support.v4.content包了,所以get()就报错了。


整体步骤

哈哈…隔,上面都在吹逼了。其实解决很简单的,就是更新下JakeWharton的butterknife就ok了。


app build.gradle


    //引入ButterKnife

    implementation "com.jakewharton:butterknife:10.0.0"

    kapt "com.jakewharton:butterknife-compiler:10.0.0"

1

2

3

对于kapt陌生的,可以自行看下

Kotlin重构如何兼容原先的ButterKnife、EventBus3.1


开开心心的将项目运行一下…又报错了


Caused by: com.android.tools.r8.utils.AbortException: Error: Static interface methods are only supported starting with Android N (–min-api 24): void butterknife.Unbinder.lambda $ static $0()


小问题,看到lambda语法,哦,那就将compileOptions中的java版本改下就ok了



android {

    compileSdkVersion 28

    compileOptions {

        sourceCompatibility JavaVersion.VERSION_1_8

        targetCompatibility JavaVersion.VERSION_1_8

    }

    defaultConfig { }

}

1

2

3

4

5

6

7

8

9

Kotlin重构系列分享教程

Kotlin环境搭建

Kotlin重构初体验之告别FindViewById

Kotlin重构如何兼容原先的ButterKnife、EventBus3.1




  • 2019-08-06 15:30:08

    小程序展示富文本

    然而rich-text有个问题,它不能够给内联img设置宽度100%,这样图片就容易溢出屏幕。我们只能在后台返回富文本的时候对图片的img标签进行格式化。或者前端进行格式化也行,我赞成使用后端,因为很多种情况我们不一定都能想得到。

  • 2019-08-07 09:07:32

    最全的Service Worker讲解

    Service Worker 最主要的特点是:在页面中注册并安装成功后,运行于浏览器后台,不受页面刷新的影响,可以监听和截拦作用域范围内所有页面的 HTTP 请求。 基于 Service Worker API 的特性,结合 Fetch API、Cache API、Push API、postMessage API 和 Notification API,可以在基于浏览器的 web 应用中实现如离线缓存、消息推送、静默更新等 native 应用常见的功能,以给 web 应用提供更好更丰富的使用体验。

  • 2019-08-07 09:09:19

    windows系统下定时关闭程序

    其中xxx.exe是你要关闭的进程中运行的exe,可以ctrl+alt+del打开任务管理器,进到详细信息查看 然后把.txt文件后缀改成.bat(此时要在查看一栏勾上文件拓展名,要不还是txt文档)

  • 2019-08-07 09:16:43

    一个比较完美的PWA例子

    但就目前来讲,PWA是Google主推的一项技术标准,FireFox,Chrome以及一些基于Blink的浏览器已经支持渐进式Web应用了,Edge上对渐进式Web应用的支持还在开发。Apple公司也表示会考虑在自己Safari支持PWA。然而这项功能已经进入了WebKit内核的五年计划中。长期来看,对浏览器兼容性的支持方面应该已经不算太大问题了。况且在现阶段,在不支持渐进式Web应用的浏览器中,你的应用也只是无法使用渐进式Web应用的离线功能而已,除此之外的功能均可以正常使用。

  • 2019-08-07 09:57:48

    spring data jpa 实体类中字段不与数据库表映射

    当我们使用spring data jpa开发的时候,会将实体类中的成员变量与表中的字段一一对应,当我们在实体类中加上一个不与数据库表一一对应的成员变量的时候,此时我们只要在这个成员变量上加上注解@Transient @

  • 2019-08-07 17:16:53

    如何在 Node.js 中使用 import / export 的三种方法

    因为一些历史原因,虽然 Node.js 已经实现了 99% 的 ES6 新特性,不过截止 2018.8.10,How To Enable ES6 Imports in Node.JS 仍然是老大难问题,下面我来介绍三种方法可以让我们在 Node.js 中使用 import/export 。

  • 2019-08-13 08:56:46

    nuxtjs组合element

    添加elementUI 插件,plugins->ele.js,代码如下

  • 2019-08-13 20:06:42

    修改 Nginx 进程最大可打开文件数(worker_processes和worker_connections)

    worker_processes:操作系统启动多少个工作进程运行Nginx。注意是工作进程,不是有多少个nginx工程。在Nginx运行的时候,会启动两种进程,一种是主进程master process;一种是工作进程worker process。例如我在配置文件中将worker_processes设置为4,启动Nginx后,使用进程查看命令观察名字叫做nginx的进程信息,我会看到如下结果: