简体   繁体   English

未创建Dagger2组件

[英]Dagger2 component is not created

Here is the code.... 这是代码...

build.gradle 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 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
}

MainActivity 主要活动

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);
    }
}

SampleApp SampleApp

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;
    }
}

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);
}

SampleModule 样品模块

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();
    }

}

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 Log 摇篮日志

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

All my problem is about the file "DaggerSampleComponent" which is not at all getting created unless i delete all inject in interface. 我所有的问题是关于文件“ DaggerSampleComponent”的,除非我删除接口中的所有注入,否则根本不会创建该文件。

Java version : 1.8 Java版本:1.8

The same code seems working on my office Mac but not in my Windows. 相同的代码似乎可以在我的Office Mac上运行,但不能在Windows上运行。 struggling a lot for this. 为此苦苦挣扎。 Can someone help me!!! 有人能帮我吗!!!

Thanks in advance. 提前致谢。

When something goes wrong with your Dagger 2 setup, you will get a compile-time message in the Gradle console when you try and build (it's in the bottom right corner of Android Studio). 当Dagger 2设置出现问题时,您尝试进行构建时会在Gradle控制台中收到一条编译时消息(位于Android Studio的右下角)。 The message will tell you what is wrong and give you a clue for how to fix it. 该消息将告诉您出了什么问题,并为您提供了解决方法的线索。

In your case, it looks like you have something like this inside your LoginActivity : 就您而言,您的LoginActivity内看起来像这样:

@Inject FileOrganiserModule fileOrganiserModule;

protected void onCreate(Bundle savedInstanceState) {

Dagger 2 modules and components are like scaffolding that helps you to request injection for the dependencies in your project. Dagger 2模块和组件就像脚手架一样,可以帮助您请求注入项目中的依赖项。 You normally shouldn't request injection of them by putting @Inject annotations on them. 通常,您不应通过在其上放置@Inject注释来请求注入它们。

If you have to create your module with a reference of the current Activity, you normally just create an instance of the module using the constructor: 如果必须使用对当前Activity的引用来创建模块,则通常只需使用构造函数创建模块的实例:

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

Or you can use the new dagger.android classes to do that for you. 或者,您可以使用新的dagger.android类为您完成此操作。 A good example project for you to follow is in the Google Android Architecture Blueprints repo here 您可以遵循的一个很好的示例项目在此处的Google Android体系结构蓝图仓库中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM