您的位置:首页 > Web前端

ButterKnife 8.4添加使用

2016-08-31 13:23 316 查看
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.* <fields>;
}

-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  butterknif Android