what is butterknife?
butterknife is a plugin that simplifies your binding to view components. For instance, instead of
public class MainActivity extends AppCompatActivity {
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(...);
// ...
you can simply do it in this way
public class MainActivity extends AppCompatActivity {
@BindView(R.id.fab) FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
fab.setOnClickListener(...);
// ...
there are more features in this plugin. you can view it in this page https://github.com/JakeWharton/butterknife
接下來樓主懶得用英文了感覺還是中文好讀
安裝時可能遇到的問題
Error:(3, 0) Android Gradle plugin 3.0.0 must not be applied to project since version 3.0.0 was already applied to this project
官方文檔讓我們在module的gradle頂部加這兩句
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
我通過刪掉下面這句垄提,解決了問題
apply plugin: 'com.android.library'
Gradle sync failed: Cause: com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;
樓主寫文章時, 最新版本是8.8.1, 我通過改成使用8.4.0版本解決了問題罩扇。
在項目的gradle中改為如下
buildscript {
repositories {
// ...
}
dependencies {
// ...
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
}
}