【发布时间】: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 的东西生成任何代码。如果有人能指出问题所在,我将不胜感激。
【问题讨论】:
-
如果您使用数据绑定,您可能不会看到构建中的所有错误消息。看看这个问题,看看它是否有帮助:stackoverflow.com/questions/37912127/…