Butter Knife github地址:https://github.com/JakeWharton/butterknife/
官方配置方法(有几个坑):
android {
...
// Butterknife requires Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
}
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:10.1.0'
}
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
Android Gradle plugin 3.2.1 must not be applied to project 'E:\Android_studio_workspace2\gongxiangdanche\app' since version 3.2.1 was already applied to this project
Open File
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id 'com.android.library']
我通过注释掉app gradle里的apply plugin: 'com.android.library’解决了
Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
Suggestion: add 'tools:replace="android:appComponentFactory"' to element at AndroidManifest.xml:5:5-47:19 to override.
里面有建议怎么做,试了一下,嗯,不行。
问题的原因是Android Studio3(我的是3.2)配置butterknife对版本有要求,需要9.0.0-rc2以上(我试过8.8.0、8.4.0的都报错),但butterknife10.0.0以上只支持Androidx,如果没有使用androidx配置butterknife10.0.0以上会报这个异常,这里的配置没有使用androidx,是android,所以把butterknife版本换为9.0.0-r2。
参考:https://blog.csdn.net/Night_X/article/details/86653715
终于可以用了!
如果出现错误:Error: Static interface methods are only supported starting with Android N (–min-api 24): void butterknife.Unbinder.lambda$static$0()
在app gradle脚本中的android或defaultConfig中添加以下内容,或可解除错误:
// Butterknife requires Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
apply plugin: 'com.android.application'
//apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
android {
compileSdkVersion 28
defaultConfig {
applicationId ""
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// Butterknife requires Java 8.
// compileOptions {
// sourceCompatibility JavaVersion.VERSION_1_8
// targetCompatibility JavaVersion.VERSION_1_8
// }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
//ProgressBar动画
implementation 'com.lsjwzh:materialloadingprogressbar:0.5.8-RELEASE'
//圆形图像
implementation 'de.hdodenhof:circleimageview:3.0.0'
//Butter Knife注解
implementation 'com.jakewharton:butterknife:9.0.0-rc2'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc2'
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'
}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
//Butter Knife注解
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 {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
是可以用了,但是generate自动生成代码里面没有自动生成ButterKnife的选择啊。
解决方法:file->Settings->Plugins->Browse Repositories->搜索ButterKnife->选择Android ButterKnife Zelezny->install->restart android studio。
重启后点击资源id R.layout.activity_main里的activity_main,然后alt+Insert弹出