Dagger 2 “Dagger” prefix component not able to compile? auto generated class

前端 未结 8 3701
悲哀的现实
悲哀的现实 2021-02-20 18:44

Im trying to use Dagger 2 on android. I previously had it working and i had an appModule injecting dependencies into specific classes in the app. My Issue is that iam getting th

8条回答
  •  广开言路
    2021-02-20 19:36

    I had the same problem on my setup, Android Studio 2.2 within the following application class:

    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;
        }
    }
    

    I'm using the following gradle declaration for dagger 2:

    //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'
    

    I could solve the problem by rebuilding the complete project (with errors) and then adding the import of the DaggerNetComponent that was missing before.

提交回复
热议问题