首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dagger2没有生成Dagger*类

Dagger2没有生成Dagger*类
EN

Stack Overflow用户
提问于 2017-01-31 07:08:36
回答 2查看 2.8K关注 0票数 3

正如标题所说,Dagger2并没有为我的Android项目生成Dagger*前缀类。我查看了我能找到的所有其他类似的帖子,但都没有帮助。我正在尝试将它添加到一个现有的项目中,最初我遇到了一些让它很好地处理数据绑定的问题,但我似乎已经解决了这个问题,即数据绑定没有编译错误,它会为它生成代码。我还下载了几个运行良好的示例项目。

我的顶级graddle文件有

代码语言:javascript
复制
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

而我的构建级别有

代码语言:javascript
复制
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"
}

组件文件非常简单

代码语言:javascript
复制
@Singleton
@Component(modules = {
        AndroidModule.class
})

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

AndroidModule文件包含

代码语言:javascript
复制
@Module
public class AndroidModule {
    private final Context application;

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

而Application类具有

代码语言:javascript
复制
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;
    }
}

我得到的输出是

代码语言:javascript
复制
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生成任何代码。如果有人能指出问题所在,我将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2017-02-01 01:50:20

您的apt依赖项应该是

代码语言:javascript
复制
apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"

您缺少工件id的-compiler部分。

这个链接也有一些有用的信息。https://github.com/google/dagger#android-gradle

票数 5
EN

Stack Overflow用户

发布于 2017-10-03 13:10:36

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

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

在文件的顶部。并替换

代码语言:javascript
复制
apt 'com.google.dagger:dagger-compiler:2.11'

使用

代码语言:javascript
复制
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

在此之后,重建您的项目,您将能够导入您的Dagger前缀类。希望它能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41947043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档