Dagger2没有生成Dagger *类

时间:2017-01-30 23:08:36

标签: android dagger-2

正如标题所说,Dagger2没有为我的Android项目生成Dagger *前缀类。我查看了我能找到的所有其他类似帖子,但没有任何帮助。我正在尝试将它添加到现有项目中,并且我遇到了一些初始问题,使其能够很好地处理数据绑定,但我似乎已经对其进行了排序,即数据绑定没有编译错误,并且它为它生成代码。我还下载了几个可以正常工作的示例项目。

我的顶级graddle文件有

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

而我的构建级别

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

组件文件非常简单

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

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

AndroidModule文件包含

@Module
public class AndroidModule {
    private final Context application;

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

,Application类有

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

我得到的输出是

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

正如我所说,Dagger的东西根本没有代码生成。如果有人能够指出问题是什么,我将非常感激。

2 个答案:

答案 0 :(得分:5)

你的依赖性应该是

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

您错过了工件ID的-compiler部分。

此链接还包含一些有用的信息。 https://github.com/google/dagger#android-gradle

答案 1 :(得分:0)

我遇到了同样的问题,花了很多时间在堆栈溢出上。最后,我通过this并找到了解决方案。简而言之,您必须在模块级Gradle文件中进行一些更改。请删除

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

位于文件顶部。并替换

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

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

在重建项目之后,您将能够导入Dagger前缀类。它会帮助你。