ButterKnife 8.4添加使用
16lz
2021-01-26
ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及 setOnClickListener代码,可视化一键生成
和其他依赖有些不一样,记录一下。
官网和github都有导入步骤。
官网:http://jakewharton.github.io/butterknife/
github:https://github.com/JakeWharton/butterknife
1、Library Dependency 搜索添加butterknife
或 compile 'com.jakewharton:butterknife:8.4.0'
2、 项目的Gradle文件
dependencies { classpath 'com.android.tools.build:gradle:2.1.2' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' //增加这一句 // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
3、 app的Gradle文件
``` apply plugin: 'com.neenbedankt.android-apt'//增加这一句dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.0.0' compile 'com.jakewharton:butterknife:8.4.0'//buttetknife apt 'com.jakewharton:butterknife-compiler:8.4.0'//添加这一句 } ```
4、使用
public class MainActivity extends AppCompatActivity { @BindView(R.id.tv01) TextView tv01; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); tv01.setText("Hello,world!"); }}
注意:
- 需要在 setContentView() 或 inflate() 之后调用bind,申明的所有对象才会创建出来
- View变量声明的时候不能为private或者static.
- 除了Activity之外,你可以提供其他的View Root,来获取对象(执行注入).
详细使用方法:http://blog.csdn.net/skeeing/article/details/52386438
5、插件:Android Butterknife Zelezny
注意:需要在ID上右击或者alt+insert才能出来生成选项。其它地方无效
6、添加混淆
-keep class butterknife.** { *; } -dontwarn butterknife.internal.** -keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * { @butterknife.* ; } -keepclasseswithmembernames class * { @butterknife.* ; }
更多相关文章
- 我的Android进阶之旅------>解决错误:You need to use a Theme.Ap
- android studio 中 java.lang.NoClassDefFoundError:retrofit2.R
- umeng android 统计类部署
- Android(安卓)JNI使用(Android(安卓)Studio)
- Android(安卓)添加Library出现错误Error:(2, 0) Plugin with id
- JSONObject与JSONArray的使用 jsonobject里面包含jsonarray
- Android用纯Java来实现选项菜单中菜单、子菜单的布局设计以及简
- 使用安卓SerialManagerService
- Litepal的简单使用