ButterKnife 8.0.1版本在AndroidStudio中的配置

今天在AndroidStudio中使用ButterKnife时出现了问题,总是出现空指针,最终从网上找到解决办法。

1.Project的build.gradle文件中增加classpath

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

2.在Module的build.gradle文件中增加plugin

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

3.在Dependencies中增加下面两句

compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'



文/码农仔(简书作者)
原文链接:http://www.jianshu.com/p/bf9018c1a7f6
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

ButterKnife 8.0.1在使用方式上较上个版本有了较大的变化:增加了资源绑定,并且将 compiler 分离了出来,引用方式做了改变。

Butter Knife地址:http://jakewharton.github.io/butterknife

首先我们看Butter Knife官网的教程:

 

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. buildscript {  
  2.   repositories {  
  3.     mavenCentral()  
  4.    }  
  5.   dependencies {  
  6.     classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'  
  7.   }  
  8. }  
  9.   
  10. apply plugin: 'com.neenbedankt.android-apt'  
  11.   
  12. dependencies {  
  13.   compile 'com.jakewharton:butterknife:8.0.1'  
  14.   apt 'com.jakewharton:butterknife-compiler:8.0.1'  
  15. }  

 

按照上述配置,你可能会出现编译上的问题,如找不到com.neenbedankt.Android-apt等问题。

经过测试,总算搞清楚了问题。

首先看下面的工程目录:

 

有两个build.gradle文件,分别是:RetrofitDemo/build.gradle和app/build.gradle,他们的区别详见:http://stormzhang.com/devtools/2014/12/18/android-studio-tutorial4/

下面进入正题。

RetrofitDemo/build.gradle配置文件中添加:

 

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. buildscript {  
  2.     repositories {  
  3.         jcenter()  
  4.     }  
  5.     dependencies {  
  6.         classpath 'com.android.tools.build:gradle:2.1.0'  
  7.         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'  
  8.     }  
  9. }  

也就是我们修改gradle版本的那个build文件。

 

 

app/build.gradle配置文件中如下:

 

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. apply plugin: 'com.android.application'  
  2. apply plugin: 'com.neenbedankt.android-apt'  
  3.   
  4. android {  
  5.     compileSdkVersion 23  
  6.     buildToolsVersion "23.0.3"  
  7.   
  8.     defaultConfig {  
  9.         applicationId "com.baidu.retrofitdemo"  
  10.         minSdkVersion 16  
  11.         targetSdkVersion 23  
  12.         versionCode 1  
  13.         versionName "1.0"  
  14.     }  
  15.     buildTypes {  
  16.         release {  
  17.             minifyEnabled false  
  18.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
  19.         }  
  20.     }  
  21.   
  22.   
  23. }  
  24.   
  25. dependencies {  
  26.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  27.     testCompile 'junit:junit:4.12'  
  28.     compile 'com.android.support:design:23.3.0'  
  29.     compile 'com.android.support:appcompat-v7:23.3.0'  
  30.     compile 'com.squareup.okhttp3:okhttp:3.2.0'  
  31.     compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'  
  32.     compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'  
  33.     compile 'com.jakewharton:butterknife:8.0.1'  
  34.     apt 'com.jakewharton:butterknife-compiler:8.0.1'  
  35. }  

猜你喜欢

转载自ch-kexin.iteye.com/blog/2299108