当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




  • 2021-01-08 16:47:37

    nodejs如何使用fetch

    node 中没有实现 fetch,你可以使用 node-fetch,使得在 node 中也可以使用 fetch.

  • 2021-01-08 16:49:59

    CommonJs 与 ESModule区别

    node中模块导入require是一个内置的函数,因此只有在运行后我们才可以得知模块导出内容,无法做静态分析

  • 2021-01-08 16:54:08

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

    注:第1、2种方法均是借助 babel,需要注意的是文章使用的babel版本 < 7。从 babel 7.X 版本开始,部分包名、用法发生了些许变化,大体与7之前的用法类似,详细请到官方手册学习 7.X 版本的改动(Babel 踩坑总结(三) —— 7.X 版本升级是我对 7.X 版本三大改动的总结)