我试图在我自己的项目中遵循教程here,但无论我做什么,我都会得到
“无法找到符号变量DaggerAppComponent”
我已尝试在StackOverflow中发布的所有解决方案,使缓存无效,清理和重建多次,但错误仍然存在。这是我的build.gradle
project.ext {
appcompat = "26.1.0"
arch = "1.1.0"
retrofit = "2.0.2"
constraintLayout = "1.0.2"
daggerVer = "2.15"
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.imincode.sps"
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
}
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation "com.google.dagger:dagger:$project.daggerVer"
implementation "com.google.dagger:dagger-android:$project.daggerVer"
implementation "com.google.dagger:dagger-android-support:$project.daggerVer"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVer"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer"
implementation "com.android.support:appcompat-v7:$project.appcompat"
implementation "com.android.support.constraint:constraint-layout:$project.constraintLayout"
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'
implementation "com.squareup.retrofit2:retrofit:$project.retrofit"
implementation "com.squareup.retrofit2:converter-gson:$project.retrofit"
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
implementation "android.arch.lifecycle:runtime:$project.arch"
implementation "android.arch.lifecycle:extensions:$project.arch"
}
这是试图实现DaggerAppComponent
的类public class AppInjector {
private AppInjector(){}
public static void init(SPSApplication spsApplication){
DaggerAppComponent.builder().application(spsApplication).build().inject(spsApplication);
}
}
这是我的AppModule
@Module(subcomponents = ViewModelSubComponent.class)
class AppModule {
@Singleton @Provides
WebService provideWebService(){
return new Retrofit.Builder()
.baseUrl(WebService.SERVER_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WebService.class);
}
@Singleton @Provides
ViewModelProvider.Factory provideViewModelFactory(ViewModelSubComponent.Builder viewModelSubComponent){
return new ProjectViewModelFactory(viewModelSubComponent.build());
}
}
和SubComponent
@Subcomponent
public interface ViewModelSubComponent {
@Subcomponent.Builder
interface Builder{
ViewModelSubComponent build();
}
LoginViewModel loginViewModel();
}
如果有帮助,我使用Android Studio 3.0.1及以下版本是我构建代码的方式..