前言
我希望我的文章不要弯弯绕绕,直接贴代码希望大家可以看得懂,看完文章可以达到学习的第一个里程碑,至于后面原理慢慢的补上就是了,毕竟业务和解决问题优先嘛。
按照官方文档说明 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 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