Gradle 未找到 DSL 方法:'apt()'

Gradle DSL method not found: 'apt()'

我正在尝试添加最新版本的 butterknife,但我从 gradle 收到此错误:

Error:(31, 0) Gradle DSL method not found: 'apt()' Possible causes:

  • The project 'MyProject' may be using a version of Gradle that does not contain the method. Gradle settings
  • The build file may be missing a Gradle plugin. Apply Gradle plugin
  • 我的gradlemobilebuild.gradle是:

    plugins {
        id "net.ltgt.apt" version "0.6"
    }
    
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
    
        defaultConfig {
            applicationId "com.mynamspace.myproject"
            minSdkVersion 19
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        wearApp project(':wear')
        testCompile 'junit:junit:4.12'
        compile 'com.jakewharton:butterknife:8.0.0'
        apt 'com.jakewharton:butterknife-compiler:8.0.0'
        compile 'com.android.support:appcompat-v7:23.3.0'
        compile 'com.google.android.gms:play-services:8.4.0'
        compile 'com.android.support:design:23.3.0'
        compile 'com.android.support:support-v4:23.3.0'
        compile 'com.android.support:recyclerview-v7:23.3.0'
    }
    

    gradle-apt-plugin 有什么问题?

    完全有可能让您的 plugins 正常工作。考虑到你的错误,我会首先关注 ButterKnife 项目使用的内容,让它工作,然后看看是否有你正在尝试的方法。

    首先,在your top-level build.gradle file中,在buildscript中包含classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'dependencies,如:

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

    然后,在 your module's build.gradle file 中,将 apply plugin: 'com.neenbedankt.android-apt' 添加到顶部。

    链接指向来自 ButterKnife GitHub 存储库、项目和专用示例应用程序的相关文件。

    而不是:

        plugins {id "net.ltgt.apt" version "0.6"}
    

    尝试:

        apply plugin: 'android-apt'
    

    在我的案例中帮助了: 添加到您的build.gradle(不是主要的,而是项目一):

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

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

    apt 已过时,将 apt 更改为新格式:

    改变

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

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

    https://bitbucket.org/hvisser/android-apt/wiki/Migration

    http://jakewharton.github.io/butterknife/

    将您的 apt 代码添加到应用 build.gradle NOT in main build.gradle.

    dependencies {
        apt group: 'group name here', name: 'artifact name here', version:'version here'
    }
    

    当然,您必须在主程序中添加以下代码 build.gradle

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