Android ButterKnife导入使用出错解决

气死我了,本来用ButterKnife这个插件是为了不用写FindViewById的代码

一直出错让我绝望

先说下ButterKnife的用法

  • 在所使用的布局 ID 上点击右键 (例如 R.layout.activity_main ), 然后选择 Generate -> Generate ButterKnife Injections
  • 在对话框中选择需要注入的 View, 还有个选项可以给 Adapter 创建一个 ViewHolder。
  • 点击 Confirm , 代码自动生成!

不要自己去下  会出现一堆问题

project的build.gradle

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




buildscript {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven{ url 'https://maven.aliyun.com/repository/jcenter'}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
        classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven{ url 'https://maven.aliyun.com/repository/jcenter'}
    }
}

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


module的build.gradle

apply plugin: 'com.android.application'
//apply plugin: 'com.jakewharton.butterknife'
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.a3"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    //androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.jakewharton:butterknife:9.0.0-rc1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
    //implementation 'org.etby:butterknife:10.1.0'
    //implementation 'com.jakewharton:butterknife:9.0.0'
    //compile 'com.jakewharton:butterknife:10.1.0'
    //annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    //apt 'com.jakewharton:butterknife-compiler:10.1.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.test:runner:1.0.2'
}

不要在ProjectStructure的Plugins下载ButterKnife,他是10.1.0版本的,会跟v4包冲突,你想用10.1版本必须用androidx但v4包的东西就用不了了。对我来说没有v4包项目就死了,于是我退回了9.0版本

com.jakewharton:butterknife:9.0.0-rc1

然后又出错了!

原本gradle里正常的

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.test:runner:1.0.2'

也给我飘红!说什么版本要一致

我解决方法是在gradle里注释掉它们 

再打开File---ProjectStructure---Dependencies---点+号---点Library

重新搜com.android.support:appcompat-v7:28.0.0和com.android.support.test:runner:1.0.2加进去

 

另外使用RecyclerView方法

在build.gradle(Module:app)中添加依赖
implementation 'com.android.support:recyclerview-v7:28.0.0'
注:版本号应该与implementation 'com.android.support:appcompat-v7:28.0.0'中的版本号相同

 

你可能感兴趣的:(Android)