DaggerApplicationComponent未编译

问题描述 投票:1回答:6

我正在使用android studio 3的最新测试版(目前是beta 4),我似乎无法生成所需的匕首类。

在我身边,我创建了一个空项目。然后我重命名了活动以匹配匕首笔记,YourActivity。请参阅https://google.github.io/dagger/android.html中的“注入活动对象”,其中显示了以下内容:

@Subcomponent(modules = ...)
public interface YourActivitySubcomponent extends AndroidInjector<YourActivity> {
  @Subcomponent.Builder
  public abstract class Builder extends AndroidInjector.Builder<YourActivity> {}
}

@Module(subcomponents = YourActivitySubcomponent.class)
abstract class YourActivityModule {
  @Binds
  @IntoMap
  @ActivityKey(YourActivity.class)
  abstract AndroidInjector.Factory<? extends Activity>
  bindYourActivityInjectorFactory(YourActivitySubcomponent.Builder builder);
}

@Component(modules = {..., YourActivityModule.class})
interface YourApplicationComponent {}

@ActivityScope
@ContributesAndroidInjector(modules = { /* modules to install into the subcomponent */ })
abstract YourActivity contributeYourActivityInjector();

public class YourApplication extends Application implements HasActivityInjector {
  @Inject DispatchingAndroidInjector<Activity> dispatchingActivityInjector;

  @Override
  public void onCreate() {
    super.onCreate();
    DaggerYourApplicationComponent.create()
        .inject(this);
  }

  @Override
  public AndroidInjector<Activity> activityInjector() {
    return dispatchingActivityInjector;
  }
}

//the renamed activity
public class YourActivity extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    //added this line
    AndroidInjection.inject(this);

    super.onCreate(savedInstanceState);
  }
}

我还在该页面中为gradle build app文件添加了以下内容:

dependencies {
  compile 'com.google.dagger:dagger-android:2.11'
  compile 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
  annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
}

因为我还没有看到任何东西我尝试更新编译到实现仍然没有运气,如果你很好奇,那看起来像:

dependencies {
  implementation 'com.google.dagger:dagger-android:2.11'
  implementation 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
  annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
}

另外,我找到了默认设置,我可以打开默认的Build - > Compiler - > Annotation Processors来启用它。我在创建新项目之前就这样做了。

毕竟,似乎没有任何作用。这是在android studio 3.x打破?如果不是,你是如何得到它的?

谢谢,凯莉

android android-gradle dagger-2 android-studio-3.0 dagger
6个回答
5
投票

啊哈哈!我发现了这个问题。看来我需要另一个annotationProcessor行作为gradle app文件

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

虽然他们的例子不太正常,但至少我现在看到了DaggerYourApplicationComponent

完整的gradle部分:

dependencies {
  compile 'com.google.dagger:dagger-android:2.11'
  compile 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
  annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
  annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
}

注意:我没有尝试用compile替换implementation,但我很高兴这很有用。

希望这也有助于其他人


1
投票

当我升级到gradle 3.0.0时,我开始看到问题。对我来说,解决方案是将实现匕首线更改为api:

api 'com.google.dagger:dagger-android:2.11'

0
投票

像编译时依赖注入工具这样的Dagger需要一次运行应用程序。然后,如果没有错误,它将自动创建。


0
投票

可能为时已晚。但implementation也在使用匕首2.11

implementation 'com.google.dagger:dagger-android:2.11'
implementation 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

我做了一个简单的hello world项目,使用Android Studio 3.0 beta-2实现dagger 2.11。希望这可能有助于初学者(像我一样)有一个良好的开端,因为谷歌文档没有提供明确的说明

MyDaggerExample


0
投票

在gradle中为依赖于下面给出的所有5个库添加依赖项

implementation 'com.google.dagger:dagger:2.11'
annotationProcessor "com.google.dagger:dagger-compiler:2.11"
implementation "com.google.dagger:dagger-android:2.11"
implementation 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'

并添加存储库mavenCenteral()

allprojects {
repositories {
    google()
    jcenter()
    mavenCentral()

0
投票

您可能忘记将apply plugin: 'kotlin-kapt'添加到应用程序级别build.gradle

// dependency injection
implementation "com.google.dagger:dagger:$dagger2_version"
kapt "com.google.dagger:dagger-compiler:$dagger2_version"
kapt "com.google.dagger:dagger-android-processor:$dagger2_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger2_version"
© www.soinside.com 2019 - 2024. All rights reserved.