【问题标题】:Dagger 2 "Dagger" prefix component not able to compile? auto generated classDagger 2“Dagger”前缀组件无法编译?自动生成的类
【发布时间】:2018-02-09 16:50:50
【问题描述】:

我正在尝试在 Android 上使用 Dagger 2。我以前让它工作,我有一个 appModule 将依赖项注入应用程序中的特定类。我的问题是我收到错误

Error:(14, 55) error: cannot find symbol class DaggerAppComponent

正在尝试导入。这是一个自动生成的类

以下是我的 build.gradle 文件中的 Dagger 特定依赖项

 compile 'com.google.dagger:dagger-compiler:2.0.2'
 compile 'com.google.dagger:dagger:2.0.2'
 provided 'javax.annotation:jsr250-api:1.0'

我多次尝试清理和重建应用程序,但课程不会生成。我也尝试过使用

 compile 'org.glassfish:javax.annotation:10.0-b28'

对于我的注释,但我仍然没有运气?如果有人可以帮助我,我将不胜感激。很难确切地看到我目前正在发生什么?谢谢

编辑:组件代码 这是以前的工作,我只是添加了 1 个额外的类来注入?

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

    void inject(RegHelper reghelper);
    void inject(headerFooterRecViewAdapter headadapter);
    void inject(SectionListExampleActivity seclistactivity);

}

【问题讨论】:

  • 显示你的组件代码。
  • 使用 apt 而不是使用 dagger-compiler 依赖进行编译。
  • 如果它之前工作并且在添加注入之后没有工作,那么可能会有更多的错误输出。如果生成失败,dagger 不会输出任何类,而是输出详细的错误描述,你应该再看看错误 after cannot find symbol
  • 我在错误后收到此错误:任务 ':app:compileDebugJavaWithJavac' 的执行失败。 > 编译失败;有关详细信息,请参阅编译器错误输出。
  • 它非常不合适,我真的很想用匕首 2。真可惜

标签: java android dagger-2


【解决方案1】:

这对我来说是(当前)最新的匕首依赖项。

`dependencies{
...
compile 'com.google.dagger:dagger:2.11'
compile 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor "com.google.dagger:dagger-compiler:2.11"
}`

【讨论】:

  • 完美解决方案。除 2.11 版外,无需修改任何内容。非常感谢。
  • 好。但是你能解释更多吗?谢谢:)
【解决方案2】:

请补充

apt 'com.google.dagger:dagger-compiler:2.x'

到您的应用 build.gradle 文件

【讨论】:

    【解决方案3】:

    在 Android Studio 2.3 中设置了一个独立的项目,我更新了默认的 gradle 文件如下,以获取生成的 Component 文件。添加的行有注释// dagger2 addition

    PROJECT build.gradle:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.3.1'
    
            // dagger2 addition
            classpath 'com.android.tools.build:gradle:1.0.0'
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+' 
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            jcenter()
    
            // dagger2 addition
            mavenCentral()
            maven{
                url 'https://oss.sonatype.org/content/repositories/snapshots/'
            }
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    APP MODULE build.gradle:

    apply plugin: 'com.android.application'
    apply plugin: 'com.neenbedankt.android-apt'   // dagger2 addition
    
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        defaultConfig {
            applicationId "com.demo.dagger2demo"
            minSdkVersion 15
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.3.0'
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        testCompile 'junit:junit:4.12'
    
        // dagger2 addition
        compile 'com.google.dagger:dagger:2.+'
        apt "com.google.dagger:dagger-compiler:2.+"
    }
    

    【讨论】:

      【解决方案4】:

      您需要安装此插件https://bitbucket.org/hvisser/android-apt 才能让 Android Studio 看到 Dagger 组件。

      【讨论】:

      • 我有那个插件 classpath 'com.android.tools.build:gradle:1.5.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 并应用插件:' com.android.application' 应用插件:'com.neenbedankt.android-apt'
      【解决方案5】:

      我在使用 Dagger 2 时遇到了类似的问题。我有一个 AppComponent 和一个 ActivityComponent(作为子组件)。一旦我在 AppComponent 中添加一个新的 inject() 函数,我就会得到上述错误。

      除了“找不到符号”错误之外还有更多错误,但它们非常模糊,我无法调试我的问题。在挖掘和研究了 stackoverflow 和不同的教程之后,我意识到我错误地使用了 Dagger。特别是我的 AppComponent 和 ActivityComponent 的设置方式。

      我假设我可以用我的 AppComponent 和 ActivityComponent 注入我的“Activity”或“Fragment”。事实证明这是错误的,至少我发现这不是使用 Dagger 的正确方法。

      我的解决方案:

      应用组件

      @Singleton
      @Component(modules = {AppModule.class})
      public interface AppComponent {
      
          void inject(MyApp application);
          void inject(ContextHelper contextHelper);
      
          // for exports
          MyApp application();
          PrefsHelper prefsHelper();
      }
      

      应用模块

      @Module
      public class AppModule {
      
          private final MyApp application;
      
          public AppModule(MyApp application) {
      
              this.application = application;
          }
      
          @Provides @Singleton
          public MyApp application() {
      
              return this.application;
          }
      
          @Provides @Singleton
          public PrefsHelper providePrefsHelper() {
      
              PrefsHelper prefsHelper = new PrefsHelper(application);
              return prefsHelper;
          }
      }
      

      活动组件

      @ActivityScope
      @Component (dependencies = {AppComponent.class}, modules = {ActivityModule.class})
      public interface ActivityComponent {
      
          void inject(MainActivity activity);
          void inject(OtherActivity activity);
          void inject(SomeFragment fragment);
      }
      

      活动模块

      @Module
      public class ActivityModule {
      
          private final MyActivity activity;
      
          public ActivityModule(MyActivity activity) {
      
              this.activity = activity;
          }
      
          @Provides @ActivityScope
          public ContextHelper provideContextHelper(MyApp application) {
      
              // My ContextHelper depends on certain things from AppModule 
              // So I call appComponent.inject(contextHelper)
      
              AppComponent appComponent = application.getAppComponent();
              ContextHelper contextHelper = new ContextHelper(activity);
              appComponent.inject(contextHelper);
              return contextHelper;
          }
      }
      

      应用

      public class MyApp extends Application {
      
          private AppComponent appComponent;
      
          @Override
          public void onCreate() {
      
              super.onCreate();
              initializeDepInj();
          }
      
          private void initializeDepInj() {
      
              appComponent = DaggerAppComponent.builder()
                      .appModule(new AppModule(this))
                      .build();
              appComponent.inject(this);
          }
      
          public LockAppComponent getAppComponent() {
      
              return appComponent;
          }
      }
      

      活动

      public class MainActivity extends AppCompatActivity {
      
          // I get it from ActivityModule
          @Inject
          ContextHelper contextHelper;
      
          // I get it from AppModule
          @Inject
          PrefsHelper prefsHelper;
      
          ActivityComponent component;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
      
              super.onCreate(savedInstanceState);
              setupInjection();
          }
      
          protected void setupInjection() {
      
              MyApp application = (MyApp) getApplication();
              component = DaggerActivityComponent.builder()
                      .appComponent(application.getAppComponent())
                      .activityModule(new ActivityModule(this))
                      .build();
              component.inject(this);
      
              // I was also doing the following snippet
              // but it's not the correct way since the ActivityComponent depends
              // on AppComponent and therefore ActivityComponent is the only 
              // component that I should inject() with and I'll still get classes
              // that are provided by the AppComponent/AppModule
      
              // application.getAppComponent().inject(this); // this is unneccessary
          }
      
          public ContextHelper getContextHelper() {
      
              return contextHelper;
          }
      }
      

      我不知道它是否直接解决了您的问题,但它至少应该对如何正确使用 Dagger 有所启发。

      希望对你有帮助。

      【讨论】:

        【解决方案6】:

        我在以下应用程序类中的 Android Studio 2.2 设置中遇到了同样的问题:

        public class NetApp extends Application {
        
            private NetComponent mNetComponent;
        
            @Override
            public void onCreate() {
                super.onCreate();
        
                // Dagger%COMPONENT_NAME%
                mNetComponent = DaggerNetComponent.builder()
                        // list of modules that are part of this component need to be created here too
                        .appModule(new AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
                        .netModule(new NetModule("https://api.github.com"))
                        .build();
        
                // If a Dagger 2 component does not have any constructor arguments for any of its modules,
                // then we can use .create() as a shortcut instead:
                //  mNetComponent = com.codepath.dagger.components.DaggerNetComponent.create();
            }
        
            public NetComponent getNetComponent() {
                return mNetComponent;
            }
        }
        

        我正在为 dagger 2 使用以下 gradle 声明:

        //Dagger 2
        // apt command comes from the android-apt plugin
        apt 'com.google.dagger:dagger-compiler:2.7'
        compile 'com.google.dagger:dagger:2.7'
        provided 'javax.annotation:jsr250-api:1.0'
        

        我可以通过重建完整的项目(有错误)然后添加之前缺少的 DaggerNetComponent 的导入来解决问题。

        【讨论】:

          【解决方案7】:

          只需将 @Module 添加到 Api 类并重建项目。

          【讨论】:

            【解决方案8】:

            希望你们一切都好。 我遇到了同样的问题,并且在堆栈溢出上花了很多时间。最后我通过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 前缀类。希望它能帮到你。

            【讨论】:

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