Android ButterKnife Zelezny 1.6.0 安装使用心得

butterknife8.5.1配android-butterknife-zelezny1.6.0

github地址(里面有安装方法):

https://github.com/JakeWharton/butterknife

https://github.com/avast/android-butterknife-zelezny

butterknife用注解的方式代替findViewById和OnClick等.

android-butterknife-zelezny是自动生成这些代码的一个插件.

  1. butterknife安装
    按照github上面的教程就行,我直接代码.
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.application.imooc_butter_knife"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:recyclerview-v7:26.0.2'
}

2个地方 一个是最上面

apply plugin: 'com.jakewharton.butterknife'

一个是下面依赖里

    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

然后在项目的那个build.gradle里

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta5'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

在依赖里添加一行:

classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'

ok,完成了.
在MainActivity里测试

    @BindView(R.id.a1)
    TextView mTv;
    @BindView(R.id.a2)
    Button mBtn1;
    @BindView(R.id.a3)
    Button mBtn2;
  1. android-butterknife-zelezny 在as的插件里,如图

《Android ButterKnife Zelezny 1.6.0 安装使用心得》 image.png

下载最新的就行,但是装完了,让我重启,重启完了发现插件没了.我上网查了一下,发现是as是从system下找插件,而我修改了这个路径,所以找不到了.

Android Studio安装插件 重启后保存不上的问题

这个帖子里说的很清楚了.只要不修改插件的路径即可.

idea.plugins.path=${idea.system.path}/plugins

《Android ButterKnife Zelezny 1.6.0 安装使用心得》 image.png

修改完重启,进入Mainactivity测试.光标一定要在id上右键generate或者ait+insert

《Android ButterKnife Zelezny 1.6.0 安装使用心得》 image.png

在adapter上试验下,在红框上alt+insert,自动生成ViewHolder,完美 :

《Android ButterKnife Zelezny 1.6.0 安装使用心得》 image.png

static class ViewHolder {
        @BindView(R.id.i1)
        TextView mTextView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

最后说一下版本的问题,Zelezny最新是1.6,配ButterKnife最新8.8.1出现错误,然后往下调版本,调到8.5.1没问题.

    原文作者:会灰的大飞狼
    原文地址: https://www.jianshu.com/p/cc6c771a1a48
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞