且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Android Studio 中设置 ButterKnife 插件?

更新时间:2021-07-03 00:52:42

使用 ButterKnife 库的最简单方法是将这一行添加到您的模块级 build.gradle 依赖项列表:>

The easiest way to use ButterKnife library is to add this single line to your module-level build.gradle dependencies list:

dependencies {
  implementation 'com.jakewharton:butterknife:7.0.1'
}

然后您就可以同步您的项目了!

You can then synchronize your project!

更新 1

我刚刚注意到 Jake the Wharton 自从我上次使用它以来更新了它!现在,您似乎必须添加到两个不同的地方:

I just noticed Jake the Wharton has updated the library since the last time I used it! Now, it appears you have to add to two separate places:

在您的项目级 build.gradle 文件中:

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

最后,在您的模块级 build.gradle 文件中:

And lastly, in your module-level build.gradle file:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
   implementation 'com.jakewharton:butterknife:8.0.1'
   apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

apply plugin: 'android-apt' 添加到 module-level build.gradle 文件的顶部很重要;很可能在第一行下方,如下所示:

It is important that you add apply plugin: 'android-apt' to top of the module-level build.gradle file; most likely below the first line like this:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

我刚刚测试了这个并且对我有用.祝你好运!

I have just tested this and works for me. Good luck!

更新 2

Jake Wharton 刚刚发布了库的更新,您需要执行以下操作才能使用它:

Jake Wharton just released an update to the library and here is what you need to do in order to use it:

因此,在您的 build.gradle (app-level) 中,将以下内容添加到您的依赖项中!

So, inside your build.gradle (app-level), add the following to your dependencies!

dependencies {
   compile 'com.jakewharton:butterknife:8.8.1'
   annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}

希望对你有帮助!

更新 3:

这里是迄今为止发布的版本,以防您与其他库发生冲突并想要它在较旧的 API 级别上工作

尝试后,如果您想要它的最低 SDK 级别为 15,直到现在你需要使用版本来让它在我的情况下工作这些设置目前至少与 SDK 15 兼容.

With trying , in case you want it min SDK level 15 till now you will need to play with versions to get it working in my case these set are compatable so far with SDK 15 as minimum.

在这种情况下,我只将这些放在模块应用级别 build.gradle 中.

This case i only put these in module app level build.gradle.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:24.2.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    implementation group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.3.0'

    implementation 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

    implementation 'io.reactivex:rxjava:1.1.6'
    implementation 'io.reactivex:rxandroid:1.2.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

    implementation 'com.android.support:recyclerview-v7:24.2.1'
}