問題描述:
在使用butteknife時,如果將其引用放在library包中,在主model下進(jìn)行注解會發(fā)現(xiàn)注解無效,記錄一下解決的辦法(以最新版為例)
官方給出的引用方式:
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'
}
但是實際使用中如果按照此方法進(jìn)行引用,app里面的注解會失效,但是修改起來也很簡單,只需要將annotationProcessor 改為compile 即可
親測可用,希望可以幫到你
最后推薦一個插件zelezny : Butterknife插件,在setting->plugins中搜索butterknife即可下載,下載后重啟studio,在activity或者fragment下將光標(biāo)選中xml文件的指引,右鍵選中g(shù)enerate可以快速生成注解,減少工作量
后來又遇到問題才發(fā)現(xiàn)原來是butterknife與lambda沖突導(dǎo)致的,引入java1.8就會導(dǎo)致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'
}