cannot find symbol class DaggerAppComponent or cannot find symbol class DaggerActivityComponent

一个人想着一个人 提交于 2019-12-18 12:58:11

问题


I get this error after adding inject on my class then it gives me a compilation error. If I remove

@Inject static ApiService mApiService;

it's working fine

And I'm using 2 Application class those are extended MultidexApplication because I have merge 2 application first is using dagger2 and second application is butterknife and both directory structure are differnet and both application interdependently working fine but after merge the code application not compile and give DaggerAppComponent error!

Please help us to resolve my query

I'm follow the below structure


@ActivityScope
@Component(dependencies = AppComponent.class)
public interface ActivityComponent extends AppComponent {
    void inject(SignInActivity activity);
}

@Singleton
@Component(modules = {ApplicationModule.class, ApiModule.class})
public interface AppComponent {
    Context appContext();
    Config config();
    ApiService apiService();    
}

@Module
public class ActivityModule {
    private final Activity mActivity;

    public ActivityModule(Activity activity){
        mActivity = activity;
    }

    @Provides
    public Context activityContext(){
        return mActivity;
    }
}

@Module
public class ApiModule {
    @Provides
    @Singleton
    public ApiService apiService(){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,false);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        return new Retrofit.Builder()
                .baseUrl(Config.API_URL)
                .addConverterFactory(JacksonConverterFactory.create(mapper))
                .client(client)
                .build()
                .create(ApiService.class);
    }
}

@Module
public class ApplicationModule {
    private final App mApp;

    public ApplicationModule(App app) {
        mApp = app;
    }

    @Provides
    @Singleton
    public Context appContext() {
        return mApp;
    }

    @Provides
    @Singleton
    public Config config() {
        return new Config(mApp);
    }    
}

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}

public class App extends BrowserApp {
    private AppComponent mAppComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppComponent = DaggerAppComponent.builder().applicationModule(new ApplicationModule(this)).build();

    }

    public AppComponent getAppAppComponent() {
        return mAppComponent;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

回答1:


In my case I tried:

  • invalidating the cache and cleaning the project
  • changing the version of dagger
  • adding -Pandroid.incrementalJavaCompile = false in gradle.properties
  • much more.

The only thing that helped me in my case was to open the console "build", click on "Toggle View" (to the left of the console and below the hammer) and fix the errors that appear on that screen.




回答2:


Just to help the people seeking the solution to this problem like me, include below lines in your app's build.gradle module (Replace the existing dagger2 dependencies)

// Dagger 2
implementation "com.google.dagger:dagger-android-support:2.11"
implementation "com.google.dagger:dagger:2.11"
annotationProcessor "com.google.dagger:dagger-compiler:2.11"



回答3:


If cannot find symbol class DaggerAppComponent Go to: \app\build\generated folder and delete it, than rebuild the project.




回答4:


Had this problem. In my case i forgot to put @Singelton above all component classes.

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



回答5:


Have you tried this?

@ActivityScope
@Component(modules = ActivityModule.class, dependencies = 
AppComponent.class)
public interface ActivityComponent extends AppComponent {
    void inject(SignInActivity activity);
}



回答6:


In my case, DaggerAppComponent was not generated, since I had included dagger2 dependencies in my submodule. I removed dependencies from there and them in main app module solved the problem.




回答7:


In my case I added to SomeModule something like this to Kotlin module:

@SomeScope
@Provides
internal fun providesSomeService(activity: MainActivity): SomeServiceProvider {
    return SomeServiceProviderImpl(activity)
}

Removed it and got another error, then fixed it. So, use Git to understand what went wrong. In Kotlin you have to separate @Binds and @Provides methods to different modules.




回答8:


I added below in gradle.properties:-

-Pandroid.incrementalJavaCompile=false

It worked for me




回答9:


I got a similar error when I introduced kotlin-kapt to my project.

Removing kotlin-kapt from build.gradle and any kapt ... dependencies got me going.



来源:https://stackoverflow.com/questions/43755753/cannot-find-symbol-class-daggerappcomponent-or-cannot-find-symbol-class-daggerac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!