解決ButterKnife在library中引用時主model(app)無法使用(注解無效)

問題描述:

  在使用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'
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容