当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"
复制代码

对于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 { }
}
复制代码

Kotlin重构系列分享教程

Kotlin环境搭建 Kotlin重构初体验之告别FindViewById Kotlin重构如何兼容原先的ButterKnife、EventBus3.1

请关注我(分享日常开发)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值