当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-10-14 21:18:57

    Comparable 的 使用

    要做这个呢,我们也是用到了Arrays.sort 这个排序的方法!但不同的是,我们之前用的是int数组,现在我们用的是这个UserBean数组。如果你想对这个UserBean数组进行排序,你要多做一件事,就是让这个 UserBean类去 实现Comparable 的接口,并重写 里面  comparaTo 的方法。注意,这个接口是可以提供泛型的 ———————————————— 版权声明:本文为CSDN博主「sdn_bt496」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明

  • 2019-10-15 05:53:20

    xUtils 里的DbUtils使用心得

    使用xUtils做Android数据库开发非常简便和得心应手,而且它本身还支持很多查询功能,比如一对多,select count和自定义sql查询等,并且支持事务(默认关闭) 下面是官方sample给的代码和我的一些使用心得 首先是两个实体类,对应两张表,这两张表中有一对多的关系