当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




  • 2020-03-01 19:00:46

    触发onclick事件元素的获取

    自动生成元素的onclick事件 event.target返回触发事件的元素 event.currentTarget返回绑定事件的元素

  • 2020-03-03 09:46:42

    JS实现HTML标签转义及反转义

    简单说一下业务场景,前台用户通过input输入内容,在离开焦点时,将内容在div中显示。 这时遇到一个问题,如果用户输入了html标签,则在div显示中,标签被解析。 由于是纯前端操作,不涉及后端,因此需要通过js对输入内容进行转义。

  • 2020-03-03 09:51:38

    写一个可插入自定义标签的 Textarea 组件

    为了实现这个功能,我最先想的是改造一个 <textarea> 然后我想到了 contenteditable (链接指向 mozilla.org) 这一属性 这是一个 html5 的属性,可以让元素内容可编辑

  • 2020-03-03 20:18:21

    Vuepress如何做到在 Markdown 中使用 Vue 语法

    在 vuepress 刚出时,我就觉得这是个很值得追更的开源项目。果不其然,里面众多的前端编程技巧让我受益良多。 于是在周末这种日子里,人家追剧我追码。 今天,我就和大家分享下 vuepress 是如何做到在 Markdown 中使用 Vue 语法的。