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

标签 android android-studio butterknife

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

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

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

我做错了什么?

最佳答案

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

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

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

更新 1

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

在您的项目级别 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'
}

关于android - 如何在 Android Studio 中设置 ButterKnife 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37754805/

相关文章:

android - 如何获取编辑文本相关的TextInputLayout?

android - 当你没有使用 unbind() 时究竟会发生什么?

android - 由于 android-apt 插件,Android Studio 3.0 上的 Butterknife gradle 错误

java - C :/android-ndk"is not found in PATH in android

安卓错误 :Unresolved reference: utils

c# - 从移动应用程序技术启动移动应用程序

android - OpenGL ES 2.0 方法的 Javadoc 注释源文件

java - 如何正确地将图像下载到我的相机胶卷中?

android - 保留 Google Play 商店的星级评定和评论

java - 如何在 Android 应用程序中编辑默认布局文件