Android Studio使用ButterKnife各种错误解决Plugin with id 'android-apt' not found.

ButterKnife可以帮助我们查找控件,添加事件绑定等等,可以减少很多代码,但是在使用过程中对新手来说,往往会碰到很多坑。
AndroidStudio常见报错如下:
1、Gradle DSL method not found: 'apt()'
2、Plugin with id 'android-apt' not found.
3、Error:Could not find com.android.tools.build:gradle:1.2.3.
Searched in the following locations:
如何使用ButterKnife:
1、首先我们要使用,就需要引入相关的依赖包:
引入依赖包只需要在module的build.gradle文件的dependencies中添加下面两句:
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
2、然后我们点击Sync project with Gradle Files按钮进行同步,然后就报错了,错误如下:
E:\workspace\androidStudio\Project_YZ\LvBuBike_source\app\build.gradle
Error:(47, 0) Gradle DSL method not found: 'apt()'
Possible causes:
  • The project 'LvBuBike_source' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file
  • The build file may be missing a Gradle plugin.
  • 意思是没找到apt方法,我们只要在mobule的build.gradle文件的顶部添加这一句就行了:
    apply plugin : 'android-apt'
    3、接着我们再进行同步,又报了如下错误:
    E:\workspace\androidStudio\Project_YZ\LvBuBike_source\app\build.gradle
    Error:(2, 0) Plugin with id 'android-apt' not found.
    解决办法,在mobule的build.gradle文件中添加这样一段代码就ok了
    buildscript {
    repositories {
    jcenter()
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' //Added line
    }
    }
    4、如果在buildscript中只添加了dependencies,而没有添加repositores的话,会报如下错误:
    Gradle 'LvBuBike_source' project refresh failed
    Error:Could not find com.android.tools.build:gradle:1.2.3.
    Searched in the following locations:
    file:/D:/software/developer/AS_/setlocation/as/gradle/m2repository/com/android/tools/build/gradle/1.2.3/gradle-1.2.3.pom
    file:/D:/software/developer/AS_/setlocation/as/gradle/m2repository/com/android/tools/build/gradle/1.2.3/gradle-1.2.3.jar
    Required by:
    LvBuBike_source:app:unspecified
    5、总结来说不要忘了这三个地方
    apply plugin : 'android-apt'
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
    buildscript {
    repositories {
    jcenter()
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' //Added line
    }
    }
    Android Studio使用ButterKnife各种错误解决Plugin with id 'android-apt' not found._第1张图片

    你可能感兴趣的:(Android,Tools)