Mike Gray
Mike Gray

Reputation: 53

Dagger2 is not generating Dagger* classes

As the title says, Dagger2 is not generating the Dagger* prefixed classes for my Android project. I looked at all other similar posts I could find but nothing helps. I'm trying to add it to an existing project, and I had some initial problems getting it to play nicely with data binding, but I seem to have sorted that out i.e. no compile errors on the data binding, and it generates code for it. I have also downloaded several example projects which work fine.

My top level graddle file has

    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

while my build level has

apply plugin: 'com.neenbedankt.android-apt'

....
final DAGGER_VERSION = '2.8'
....

    compile 'javax.inject:javax.inject:1'
    compile 'javax.annotation:javax.annotation-api:1.2'

    apt "com.google.dagger:dagger:$DAGGER_VERSION"
    compile "com.google.dagger:dagger:$DAGGER_VERSION"
}

The component file is extremely simple

@Singleton
@Component(modules = {
        AndroidModule.class
})

public interface ApplicationComponent {
    void inject(MainActivity activity);
}

The AndroidModule file contains

@Module
public class AndroidModule {
    private final Context application;

    public AndroidModule(Application application) {
        this.application = application;
    }
}

and the Application class has

import com.rockwellcollins.fsl.dagger.DaggerApplicationComponent;
...

public class FslApplication extends Application {

    private static ApplicationComponent component;

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

        component = DaggerApplicationComponent.builder()
                .androidModule(new AndroidModule(this))
                .build();
    }

    public static void inject(MainActivity target) {
        component.inject(target);
    }

    public ApplicationComponent getComponent() {
        return component;
    }
}

and the output I get is

Information:Gradle tasks [clean, :android-sliding-layer-lib:Library:generateDebugSources, :android-sliding-layer-lib:Library:mockableAndroidJar, :android-sliding-layer-lib:Library:prepareDebugUnitTestDependencies, :android-sliding-layer-lib:Library:generateDebugAndroidTestSources, :android-sliding-layer-lib:Library:compileDebugSources, :android-sliding-layer-lib:Library:compileDebugUnitTestSources, :android-sliding-layer-lib:Library:compileDebugAndroidTestSources, :mobile:generateDebugSources, :mobile:mockableAndroidJar, :mobile:prepareDebugUnitTestDependencies, :mobile:generateDebugAndroidTestSources, :mobile:compileDebugSources, :mobile:compileDebugUnitTestSources, :mobile:compileDebugAndroidTestSources, :wear:generateDebugSources, :wear:generateDebugAndroidTestSources, :wear:mockableAndroidJar, :wear:prepareDebugUnitTestDependencies, :wear:compileDebugSources, :wear:compileDebugAndroidTestSources, :wear:compileDebugUnitTestSources]

Warning:WARNING: Dependency org.json:json:20131018 is ignored for debug as     it may be conflicting with the internal version provided by Android.
Warning:WARNING: Dependency org.json:json:20131018 is ignored for release as it may be conflicting with the internal version provided by Android.
Warning:View field aircraftList collides with a variable or import
Warning:View field groundList collides with a variable or import

C:\Git\android\mobile\src\main\java\com\rockwellcollins\fsl\FslApplication.java
Error:(7, 38) error: cannot find symbol class DaggerApplicationComponent
Error:org.gradle.api.internal.tasks.compile.CompilationFailedException: 
Compilation failed; see the compiler error output for details.
Information:BUILD FAILED

And as I said, no code gets generated at all for the Dagger stuff. If anyone can point out what the problem is, I would be very grateful.

Upvotes: 3

Views: 2904

Answers (2)

Rahul Sharma
Rahul Sharma

Reputation: 13633

I was facing the same problem and spent a lot of time over stack overflow. At last I go through this and able to find solution. Briefly, You have to make some changes in your module level Gradle file. Please remove

apply plugin: 'com.neenbedankt.android-apt'

at the top of the file. And replace

apt 'com.google.dagger:dagger-compiler:2.11'

with

annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

After that rebuild your project and you will be able to import your Dagger prefix classes. Hopw it will help you out.

Upvotes: 0

ootinii
ootinii

Reputation: 1815

Your apt dependency should be

apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"

You're missing the -compiler part of the artifact id.

This link also has some useful information. https://github.com/google/dagger#android-gradle

Upvotes: 5

Related Questions