简体   繁体   中英

Dagger2 component is not created

Here is the code....

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
}

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

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.

Java version : 1.8

The same code seems working on my office Mac but not in my 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). 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 :

@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. You normally shouldn't request injection of them by putting @Inject annotations on them.

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:

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

Or you can use the new dagger.android classes to do that for you. A good example project for you to follow is in the Google Android Architecture Blueprints repo here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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