ButterKnife10.0.0环境搭建和插件使用

介绍

ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象,有了ButterKnife可以很轻松的省去这些步骤。使用ButterKnife对性能基本没有损失,因为ButterKnife用到的注解并不是在运行时反射的,而是在编译的时候生成新的class。

GitHub地址:https://github.com/JakeWharton/butterknife

原理

利用了IOC的(Inverse of Controll)控制反转结构,2004年后改名为DI(dependency injection)依赖注入。目的是为了使类与类之间解耦合,提高系统的可扩展性和可维护性。越来越趋向于后端开发了。

配置

书上教程为:
https://blog.csdn.net/anyong8888/article/details/101827415
会报错:Error:android-apt plugin is incompatible with the Android Gradle plugin. Please use 'annotationProce
解决:https://blog.csdn.net/l403040463/article/details/79166342
再报错:AS 3.3 The given artifact contains a string literal with a package reference 'android.support.v4.con
解决:https://blog.csdn.net/RichieZhu/article/details/86530472
(产生这个错误的原因是由于项目引用了AndroidX的依赖包,但是下面这两个依赖的内部是引用之前的 support 包实现的,因此产生矛盾。)
再报错:Static interface methods are only supported starting with Android N (–min-api 24)
解决:
在app module的build.gradle中添加:

android {
  // ...
  compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
 }
// ... 
}

(因为静态接口需要在Java 8 下才支持使用,所以我们要使用静态接口,就需要在app的build.gradle文件中配置声明,使用Java 8编译。)


汇总:

步骤1.
在app module的build.gradle中添加:

android {
  // ...
  compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
 }
// ... 
}

步骤2.
在app module的build.gradle中添加:

    implementation 'com.jakewharton:butterknife:10.0.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'

更详细使用方式:
Butterknife 10.2.0版本组件化、模块化使用教程:
https://www.jianshu.com/p/b6990f643422


插件安装

在这里插入图片描述

使用方法:

右键单击所需布局参数(例如,活动或片段中的R.layout.main),然后
Generate->Generate ButterKnife Injections
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值