问题描述:
在使用butteknife时,如果将其引用放在library包中,在主model下进行注解会发现注解无效,记录一下解决的办法(以最新版为例)
官方给出的引用方式:
project:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
}
}
model(在本文中即是library的model):
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
dependencies {
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
但是实际使用中如果按照此方法进行引用,app里面的注解会失效,但是修改起来也很简单,只需要将annotationProcessor 改为compile 即可
亲测可用,希望可以帮到你
最后推荐一个插件zelezny : Butterknife插件,在setting->plugins中搜索butterknife即可下载,下载后重启studio,在activity或者fragment下将光标选中xml文件的指引,右键选中generate可以快速生成注解,减少工作量
后来又遇到问题才发现原来是butterknife与lambda冲突导致的,引入java1.8就会导致butterknife失效,找了半天才找到解决办法,就是在project下添加
classpath 'me.tatarka:gradle-retrolambda:3.4.0'
然后在model中添加
apply plugin: 'me.tatarka.retrolambda'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
就可以正常使用lambda和butterknife了,最后附上完成配置代码:
project:
apply plugin: 'kotlin'
buildscript {
ext.kotlin_version = '1.1.4'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'me.tatarka:gradle-retrolambda:3.4.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
model:
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.jakewharton:butterknife:8.8.1'
compile 'com.jakewharton:butterknife-compiler:8.8.1'
}