经大神同事介绍,了解到一款使用依赖注入方式实例化安卓空间的第三方library,然而初次使用遇到诸多问题,通过搜索引擎和同事的指导下,记录一下问题的解决方案。
首先,开发环境:
1.Windows 7 64位操作系统
2.Android Studio 3.2,SDK版本Level 28
3.安卓终端测试时有两个,分别是 android 6.0 和 android 5.1.1的终端操作系统
先把配置文件的资源地址放上来(已测试编译通过):资源链接
首先我是找到的网上入门指导教程中,教程中使用的版本是 butterknife 8.8.1.于是,照猫画虎配置一番
a.在Project级别的build.gradle文件中配置butterknife插件,如下所示
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
//下面一行添加插件
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
b.在app级别的build.gradle配置文件添加插件引用,如下
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife' //添加这一行
c.添加library依赖,如下所示
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'
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:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
到这里,理论上就应该结束了,然而,我的安卓工程遇到报错,如下:
Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)</li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)</li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
解决方案:
一·使用版本9.0.0-SNAPSHOT
将butterknife版本更换为9.0.0-SNAPSHOT,修改代码如下
// 1. project级别的build.gradle,首先修改插件版本
//classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT'
// 2. app级别的build.gradle修改引用依赖的版本
//implementation 'com.jakewharton:butterknife:8.8.1'
//annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT'
//3.project级别的build.gradle配置文件新增maven仓库的snapshots地址
repositories {
...
maven {url "https://oss.sonatype.org/content/repositories/snapshots/"}
}
repositories {
....
maven {url "https://oss.sonatype.org/content/repositories/snapshots/"}
}
//4.还有最后一步
走完前三步之后,编译之后还会报错,报错信息如下所示
Error: Static interface methods are only supported starting with Android N (--min-api 24): void butterknife.Unbinder.lambda$static$0()
所以最后一步需要配置jdk环境,在app级别的build.gradle配置文件中新增配置项,如下:
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
ok,butterknife:9.0.0-SNAPSHOT版本的环境搭建到此成功,编译运行成功。
ps:参考链接:https://github.com/JakeWharton/butterknife/issues/1125
方案二·使用 butterknife:9.0.0-rc2,这是最简单的引用方式
提醒一下,使用这个版本,就不需要其他的配置了,说明如下
//在使用butterknife:9.0.0-rc2的环境中,如果有以下的配置,全部删除
project级别的build.gradle文件
//maven {url "https://oss.sonatype.org/content/repositories/snapshots/"} //删除
//classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1' //删除
//classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT' //删除
app级别的build.gradle文件
//apply plugin: 'com.jakewharton.butterknife' //删除
app级别中的依赖版本修改为9.0.0-rc2
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'
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:8.8.1'
// annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
// implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT'
// annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT'
implementation 'com.jakewharton:butterknife:9.0.0-rc2'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc2'
}
方案三·如果有强迫症晚期患者,就是非要用8.8.1版本的怎么办呢?笔者瞎猫碰见死耗子还真找到了办法,如下
apply plugin: 'com.android.application'
//apply plugin: 'com.jakewharton.butterknife' //app级别的build.gradle中把这行注释掉
就是在方案一的基础上,注释掉那一行代码就行了,至于为什么?奈何小弟才疏学浅,真的不知道,如有大神屈尊读到本博客,还希望能够给小弟解惑。还有,只是8.8.1版本测试过,其他版本不敢保证这个方法能行的通哦。
本博客参考链接:
2.https://github.com/JakeWharton/butterknife/issues/1125