当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-19 17:12:40

    百度地图放大覆盖物消失

    产生问题的原因是因为我们用的普通的点数组生成的多边形,我们应该用百度的点数组生成就没问题了。

  • 2020-03-19 19:15:47

    vue中methods watch和compute的区别和联系

    首先要说,methods,watch和computed都是以函数为基础的,但各自却都不同 而从作用机制和性质上看,methods和watch/computed不太一样,所以我接下来的介绍主要有两个对比: 1.methods和(watch/computed)的对比

  • 2020-03-19 19:50:31

    用vue做的跟随鼠标移动的div

    随鼠标移动的动画效果,之前一直使用angular和react,没怎么接触过vue,先做一个vue的简单例子,然后再整合。

  • 2020-03-20 13:35:55

    随便想到,群聊天的数据库简单设计

    拆分成两个表,一个是消息的流水表,一个是每个人的配置表。 记录每个群下面的这个用户的最后读取的消息last_msg_id,然后在计算消息未读数据。 这样优化之后数据将减少好多,数量是 m+n条数据。不在是成倍增长了。

  • 2020-03-20 13:39:50

    类似与微信朋友圈功能数据库如何实现

    每次发圈子的时候,给关注我的每个uuid,发一个内容id。 大概表的设计就是 uuid,idlist 这样的,idlist是按照时间顺序的。 然后定期删除idlist过多的老圈子。

  • 2020-03-21 00:11:38

    Android卡片布局(圆角阴影)的几种实现及分析

    在开发中,为了界面美观,圆角view和阴影效果是开发中经常遇到的UI场景,比如银行卡效果,卡片式itemView布局,Banner图等,开发中我们通过各种方式实现了这种效果,但是哪种方案最好呢,接下来本文将比较几种常见的圆角阴影布局实现,并从内存占用角度分析它们的优缺点.

  • 2020-03-21 12:05:24

    android 自定义组件,使用AttributeSet

    首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:

  • 2020-03-21 12:09:59

    Android使用AttributeSet自定义控件的方法

    我们可以在attrs.xml中声明自己控件的属性,在布局xml文档中声明自己的命名空间,这时就可以对设置自己想要的值了,然后在AttributeSet这个属性中获取对应的值。好了不多说,我们来看下代码,一切尽在不言中: