How to setup ButterKnife plugin in Android Studio?

若如初见. 提交于 2019-11-27 19:54:25

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!

UPDATE 1

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:

In your project-level build.gradle file:

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

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

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!

UPDATE 2

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

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

I hope this helps you!

UPDATE 3:

Here is versions released till now in case you have conflict with other libraries and want it to work on older API levels

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.

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

In Android Studio you can do this by:

  1. Right-click your module
  2. Click 'Open Module Settings'
  3. Click Dependencies Tab
  4. Click the green plus icon
  5. Select 'Library Dependency' from the dropdown list
  6. Enter "butterknife" in the search field and click the search button

After selecting the library, Android Studio adds the dependency to your module.

Since butterknife does annotation processing, you have to add this to the build.gradle of your module right after the compile statement for butterknife:

annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

where the version number at the end should match your version of butterknife.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!