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
在这里插入图片描述

发布了272 篇原创文章 · 获赞 165 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_32534441/article/details/105092521