kapt构建失败,使用Dagger Android Processor。
kapt构建失败,使用Dagger Android Processor。
我试图在我的项目中使用Kotlin注解处理工具(kapt)来包含Dagger Android Processor(文档链接为这里)。
我在我的build.gradle文件中包含了正确的依赖项:
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.example.app" minSdkVersion 16 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } sourceSets { main.java.srcDirs += 'src/main/kotlin' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.google.code.findbugs', module: 'jsr305' }) // Support libraries compile "com.android.support:appcompat-v7:25.3.1" compile "com.android.support:design:25.3.1" compile "com.android.support.constraint:constraint-layout:1.0.2" // Google libraries compile "com.google.dagger:dagger-android:2.10" compile "com.google.dagger:dagger-android-support:2.10" kapt "com.google.dagger:dagger-android-processor:2.10" // ReactiveX libraries compile "io.reactivex.rxjava2:rxandroid:2.0.1" compile "io.reactivex.rxjava2:rxjava:2.0.9" // Kotlin libraries compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.2" // Test instrumentation testCompile "junit:junit:4.12" }
但是当我构建项目时,我得到以下的GradleException错误信息:
:app:kaptDebugKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Internal compiler error. See log for more details
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin'.
...
Caused by: org.gradle.api.GradleException: Internal compiler error. See log for more details
...
根据Kotlin 1.0.4的发布日志:
你只有在默认的kapt注解处理实现无法解决问题时才应该启用generateStubs。
我尝试过这样做,但没有成功。
根build.gradle文件:
buildscript { repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2" classpath "com.android.tools.build:gradle:2.3.1" } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
在使用Dagger Android Processor时,出现了"kapt Build Fails With Dagger Android Processor"的错误。这个错误的原因是在创建AndroidInjector.Factory时,我使用了"in"变量注解,而应该使用"out"注解。
解决这个问题的方法是将AndroidInjector.Factory的注解从"in"改为"out"。具体来说,在MainActivityModule类中的bindMainActivityInjectorFactory方法中,将AndroidInjector.Factory
Java中的通配符可以是无界的,也可以是上界或下界的,分别使用没有关键字、"extends"关键字和"super"关键字。这些关键字对应于Kotlin中的"in"和"out"变异注解。错误提示说明我在这里使用了"extends",而应该使用"super";换成Kotlin的说法,就是我应该使用"in"而不是"out"。
如果想要生成相同的错误日志,可以直接复制上面的MainActivityModule代码,但是使用"in"变异(AndroidInjector.Factory
希望"kap"错误能够出现在标准的Studio错误日志窗格中。
在使用kapt构建Dagger Android处理器时,可能会遇到构建失败的问题。出现这个问题的原因是在build.gradle文件中的配置有误。解决方法如下:
1. 首先,需要删除以下两行配置:
- `apply plugin: 'kotlin-kapt'`
- `kapt 'groupId:artifactId:$kapt_version'`
2. 然后,添加以下配置(在apply plugin后面添加):
kapt {
generateStubs = true
}
需要注意的是,如果已经应用了`kotlin-kapt`插件,则不需要添加`generateStubs = true`配置,因为这是为旧版本的kapt准备的。Gradle会对此进行警告提示。
希望这篇文章能够帮助到其他人解决类似的问题。