【问题标题】:Dagger2 is not generating Dagger* classesDagger2 没有生成 Dagger* 类
【发布时间】:2017-01-30 23:08:36
【问题描述】:

正如标题所说,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 的东西生成任何代码。如果有人能指出问题所在,我将不胜感激。

【问题讨论】:

标签: android dagger-2


【解决方案1】:

你的 apt 依赖应该是

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

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

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

【讨论】:

    【解决方案2】:

    我也面临同样的问题,并且在堆栈溢出方面花费了很多时间。最后我通过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 前缀类。希望它能帮到你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多