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

How i can add ButterKnife Locally (No remote dependencies)

我已下载 '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" />

Gradle 文件

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')}

如果您在 gradle 中正确添加它并构建应用程序,它应该会自动将 butterknife 添加到您的应用程序中。 无需手动下载。

只有这样才能完成工作:

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

为了让 ButterKnife 正常工作,我们需要添加三个模块:

  • butterknife-注释
  • butterknife-reflect
  • butterknife-runtime

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