如何在 Android Studio 中设置 ButterKnife 插件? [英] How to setup ButterKnife plugin in Android Studio?

查看:39
本文介绍了如何在 Android Studio 中设置 ButterKnife 插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要安装插件 Butter Knife.我在哪里可以下载它?我下载了一个 .jar 插件(但如果文件是我需要的则不是),我已经安装,但是当我单击生成"选项时,不会出现使用黄油刀的选项.按照视频教程,我修改了 Gradle build 文件:我现在拥有它们,如下所示:

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

// 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 同步我收到这个错误:

Gradle to synchronize I get this error:

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

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

我做错了什么?

推荐答案

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

这篇关于如何在 Android Studio 中设置 ButterKnife 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆