在 MVP 中的 DaggerApplicationComponent 中出现错误

Getting error in DaggerApplicationComponent in MVP

我正在尝试学习 MVP 模式,并遵循我获得此源代码的教程。

应用模块class

@Module
public class ApplicationModule {
    private Application application;
    public ApplicationModule(Application application) {
        this.application = application;
    }
    @Provides
    @Singleton
    public Context provideContext() {     return application;    }
}

ApplicationComponent 接口

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent
{    void inject (MainActivity target); }

应用class

public class App extends Application {

    private ApplicationComponent component;

    @Override
    public void onCreate() {
        super.onCreate();

        //the below line is showing error
        component = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .build();
    }


    public ApplicationComponent getComponent() {
        return component;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity
{
    @Inject
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((App) getApplication()).getComponent().inject(this);
    }
}

模块级别build.gradle

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.2'
    defaultConfig {
        applicationId "com.renegens.daggerexample"
        minSdkVersion 16
        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'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })


    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'
    implementation 'com.google.dagger:dagger:2.12'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.12'
    compileOnly 'javax.annotation:jsr250-api:1.0'
}

build.gradle 项目级别

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

但是它在应用程序中的 DaggerApplicationComponent 中显示错误 class。我不得不更改一些设置以导入项目- classpath 'com.android.tools.build:gradle:3.0.0''com.android.tools.build:gradle:3.2.0' 还更改了 minSDKbuildToolcompileSDK 版本和重新更改。

为什么显示错误,我该如何解决?请帮帮我,否则我就卡住了。

更新

我在工具栏的“构建”选项卡中收到错误消息Failed to resolve: com.google.dagger:dagger:2.12.。下面是消息的屏幕截图。

使用最新的 dagger 依赖项

implementation 'com.google.dagger:dagger:2.14.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'

除此之外,向 ApplicationModule 接口添加一行,即

Context provideContext();