How to setup ButterKnife plugin in Android Studio?
Asked Answered
R

2

11

I need to install the plugin Butter Knife. Where can I download it? I downloaded a .jar plugin (but not if the file is the one I need), I have installed but when I click on the option "generate" not the option to use butterknife appears. following a video tutorial I modified files Gradle build: I have them now as follows:

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


android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "calcursoedxleccion0.cal"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$rootProject.ext.supportLibraryVersion"
    compile "com.android.support:support-v4:$rootProject.ext.supportLibraryVersion"
    compile "com.android.support:design:$rootProject.ext.supportLibraryVersion"

   // compile "com.jakewharton:butterknife:$rootProject.ext.butterKnifeVersion"
    compile "com.jakewharton:butterknife:$rootProject.ext.butterKnifeVersion"


   apt "com.jakewharton:butterknife-compiler:8.0.1"
}

and

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ext{

    minSdkVersion = 16
    targetSdkVersion = 23
    compileSdkVersion = 23
    buildToolsVersion = '23.0.3'

    supportLibraryVersion = '23.3.0'
    butterKnifeVersion = '8.0.1'
}

Gradle to synchronize I get this error:

"The android android-library or plugin must be applied to the project" error (1.0)

What am I doing wrong?

Rabid answered 10/6, 2016 at 18:15 Comment(2)
Why are you defining ext{ ...}? Place that attributes directly in your app build.gradleFlaxman
how do you know its correlated to Butterknife? It could be that your ext values are not being imported properlyKirbie
S
46

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

where to add 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'
}
Starter answered 10/6, 2016 at 18:23 Comment(6)
If it is working for you, would you accept this as the answer? ThanksStarter
Accepting it means checking the box below the upvote so that others will find it useful. It is a Tick symbolStarter
i can't accept because i am not post question.. the person who is post the question. he should be accepted this as checkbox.Kirchner
Oh Sorry I thought it was you; no problem Waqar! Happy codingStarter
No need to do this by editing gradle files. Please take a look at my answer.Myrta
Did someone just downvoted an answer that has been used by so many?Starter
M
3

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.

Myrta answered 17/3, 2017 at 13:59 Comment(1)
As someone still fairly new to Android Studio, this was the best solution for me. Multiple attempts to manually add lines to the files kept throwing warnings, and not completely working. This method accomplished it cleanly and resolved all butterknife dependencies. Thanks!Opening

© 2022 - 2024 — McMap. All rights reserved.