我如何在本地添加ButterKnife(无远程依赖项)

时间:2019-03-22 09:21:06

标签: android butterknife

我已经下载了“ butterknife-8.8.1.aar”,“ butterknife-annotations-8.8.1.jar”和“ butterknife-compiler-8.8.1.jar”,并将其添加到项目中。启动该应用程序时,该视图会出现NullPointer异常。

public class MainActivity extends AppCompatActivity {

@BindView(R.id.tv_welcome)
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.bind(this);
    textView.setText("Butter knife worked!");
}
}

XML

<TextView
    android:id="@+id/tv_welcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

成绩文件

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// ButterKnife
implementation files('libs/butterknife-annotations-8.8.1.jar')
implementation files('libs/butterknife-compiler-8.8.1.jar')
implementation project(':butterknife-8.8.1')}

2 个答案:

答案 0 :(得分:0)

如果您将其正确添加到gradle中并进行构建,则该应用应将butterknife自动添加到您的应用中。 无需手动下载。

只有这样才能完成工作:

dependencies {
 implementation 'com.jakewharton:butterknife-gradle-plugin:10.1.0'
}

答案 1 :(得分:0)

要使ButterKnife起作用,我们需要添加三个模块:

  • 黄油刀注释
  • 牛刀反射
  • 黄油刀运行时

     implementation project(':butterknife-annotations')
     implementation project(':butterknife-reflect')
     implementation project(':butterknife-runtime')