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

时间:2016-06-10 18:15:25

标签: android android-studio butterknife

我需要安装插件Butter Knife。我在哪里可以下载?我下载了一个.jar插件(但是如果文件是我需要的那个,则没有),我已经安装但是当我点击选项"生成"不会出现使用butterknife的选项。在视频教程之后,我修改了文件Gradle build:我现在拥有它们如下:

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

// 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我收到此错误:

  

" android android-library或插件必须应用于项目"   错误(1.0)

我做错了什么?

2 个答案:

答案 0 :(得分:44)

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

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

然后,您可以同步项目!

更新1

我刚注意到自从上次我使用它以来,沃顿已经更新了库!现在,您似乎必须添加到两个不同的位置:

项目级 build.gradle文件中:

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

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

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文件的顶部非常重要;最有可能低于第一行:

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

我刚试过这个并且适合我。祝你好运!

更新2

Jake Wharton刚刚发布了对该库的更新,以下是您需要做的事情:

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

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

where to add 我希望这可以帮助你!

更新3:

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

尝试,如果你想要它至少达到SDK级别15到现在为止 你将需要使用版本来使它在我的情况下工作这些设置 到目前为止,SDK 15至少是可以兼容的。

这种情况我只将这些放在模块应用级别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'
}

答案 1 :(得分:3)

在Android Studio中,您可以通过以下方式执行此操作:

  1. 右键单击您的模块
  2. 点击“打开模块设置'
  3. 单击“依赖关系选项卡”
  4. 点击绿色加号图标
  5. 选择'图书馆依赖'来自下拉列表
  6. 输入" butterknife"在搜索字段中,然后单击搜索按钮
  7. 选择库后,Android Studio会将依赖项添加到您的模块中。

    由于butterknife进行注释处理,你必须在butterknife的编译语句之后将它添加到模块的build.gradle中:

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

    最后的版本号应该与你的版本的奶油刀相匹配。