未创建 Dagger2 组件

Dagger2 component is not created

这是代码....

build.gradle

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

主要活动

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import javax.inject.Inject;

public class MainActivity extends AppCompatActivity {

    @Inject
    SampleModule sampleModule;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((SampleApp)getApplication()).getSampleComponent().inject(this);
        sampleModule.simpleModel.setX(10);
    }
}

示例应用程序

import android.app.Application;

/**
 * Created by pavan on 4/17/2017.
 */

public class SampleApp extends Application {
    private SampleComponent sampleComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        sampleComponent = DaggerSampleComponent.builder()
                .sampleModule(new SampleModule(this))
                .build();
    }

    public SampleComponent getSampleComponent(){
        return  sampleComponent;
    }
}

示例组件

import javax.inject.Singleton;

import dagger.Component;

/**
 * Created by pavan on 4/17/2017.
 */
@Singleton
@Component(modules = {SampleModule.class})
public interface SampleComponent {
    void inject(MainActivity activity);
}

示例模块

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

/**
 * Created by pavan on 4/17/2017.
 */
@Module
public class SampleModule {
    SimpleModel simpleModel;
    SampleApp sampleApp;

    public SampleModule(SampleApp sampleApp){
        this.sampleApp = sampleApp;
    }

    @Provides
    @Singleton
    public SampleApp provideApplication(){
        return sampleApp;
    }

    @Provides
    @Singleton
    public SimpleModel provideSimpleModelObj() {
        return new SimpleModel();
    }

}

简单模型

public class SimpleModel {
    private int x;
    private int y;

    public int getX() {
        return x;
    }
public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

}

Gradle日志

Error:(13, 10) error: com.uvr.organizer.myfilesorganizer.FileOrganizerModule cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
com.uvr.organizer.myfilesorganizer.FileOrganizerModule is injected at
com.uvr.organizer.myfilesorganizer.LoginActivity.appScope
com.uvr.organizer.myfilesorganizer.LoginActivity is injected at
com.uvr.organizer.myfilesorganizer.AppComponent.inject(loginActivity)
Error:(14, 10) error: com.uvr.organizer.myfilesorganizer.FileOrganizerModule cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
com.uvr.organizer.myfilesorganizer.FileOrganizerModule is injected at
com.uvr.organizer.myfilesorganizer.MainActivity.appScope
com.uvr.organizer.myfilesorganizer.MainActivity is injected at
com.uvr.organizer.myfilesorganizer.AppComponent.inject(mainActivity)
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 8.136 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console

我所有的问题都是关于文件 "DaggerSampleComponent" 的,除非我删除界面中的所有注入,否则根本不会创建该文件。

Java 版本:1.8

相同的代码似乎在我的办公室 Mac 上有效,但在我的 Windows 中却无效。为此付出了很多努力。 谁能帮帮我!!!

提前致谢。

当您的 Dagger 2 设置出现问题时,当您尝试构建时,您将在 Gradle 控制台中收到一条编译时消息(它位于 Android Studio 的右下角).该消息将告诉您出了什么问题,并为您提供修复方法的线索。

在你的情况下,你的 LoginActivity:

中似乎有这样的东西
@Inject FileOrganiserModule fileOrganiserModule;

protected void onCreate(Bundle savedInstanceState) {

Dagger 2 模块和组件就像脚手架一样,可以帮助您请求注入项目中的依赖项。您通常不应该通过在它们上放置 @Inject 注释来请求注入它们。

如果您必须使用当前 Activity 的引用创建模块,您通常只需使用构造函数创建模块的实例:

void injectMembers() {
    DaggerLoginComponent.builder().loginModule(new LoginModule(this));
}

或者您可以使用新的 dagger.android 类 为您做到这一点。 Google Android Architecture Blueprints repo here

中有一个很好的示例项目供您参考