【发布时间】:2018-03-14 17:20:02
【问题描述】:
我已经面临这个问题 3 个小时了。我找不到任何合乎逻辑的理由来解释正在发生的事情。问题是,只有当我使用生成的 Dagger 类,并且在 Gradle 执行 Build 之后,对这些类的每个引用都变红了,我得到了错误:
错误:未解析的引用:DaggerAppComponent
但是,如果我注释掉 App 类中的每一行,请引用这些生成的 Dagger 类中的任何一个。构建过程成功完成。我试着做干净/重建/制作项目。但它不起作用。
关于我的环境的一些信息:Build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
applicationId "xxxxx"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "android.arch.lifecycle:viewmodel:1.1.0"
implementation "android.arch.lifecycle:livedata:1.1.0"
implementation "android.arch.persistence.room:runtime:1.0.0"
implementation "android.arch.lifecycle:runtime:1.1.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:26.1.0'
kapt "android.arch.lifecycle:compiler:1.1.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
kapt "com.google.dagger:dagger-compiler:2.8"
compile "com.google.dagger:dagger:2.8"
compile "com.google.dagger:dagger-android:2.8"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Kotlin 版本:ext.kotlin_version = '1.1.51'
Android Studio 版本:3.0.1
Build.Gradle 文件中项目(不是应用模块)的依赖项部分:
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
App类:
class App:Application() {
private val component:AppComponent by lazy {
DaggerAppComponent.builder()
.build()
}
override fun onCreate() {
super.onCreate()
component.inject(this)
}
}
如果有人能找出问题所在,将不胜感激。
附言我仍在学习Dagger2,而且我对Kotlin 和Native 的开发总体上是新手。我来自C# 和Xamarin 背景。
【问题讨论】: