Android Studio适配butterknife遇到的坑
升级Android Studio到Android Studio Narwhal 3 Feature Drop | 2025.1.3
适配butterknife遇到了各种各样的问题,差点都要放弃了,关关难过关关过
1.@BindView(R.id.xxx)无法识别
解决方案:整理项目配置
a.工程项目根路径build.gradle增加:
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
b.主项目路径下build.gradle增加:
apply plugin: 'com.jakewharton.butterknife' configuration.dependencies.add(getProject().dependencies.create('com.jakewharton:butterknife:10.1.0'))kapt 'com.jakewharton:butterknife-compiler:10.2.3' implementation 'com.jakewharton:butterknife:10.2.3'
2.Incorrectly typed data found for annotation element public abstract int butterknife.BindView.value() (Found data of type R2.id)
解决方案:在AndroidManifest.xml增加packagename
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="xxx.xxx.xxx"xmlns:tools="http://schemas.android.com/tools">
3.onClick方法中使用switch case报R.id.xxx不是常量
解决方案:工程项目根路径gradle.properties增加:
android.nonFinalResIds=false
4.无法生成BuildConfig类:
解决方案:主项目路径下build.gradle增加:
buildFeatures {buildConfig true }
赠人玫瑰手有余香,欢迎大家一起交流讨论,让码农的工作越来越简单!