cannot find symbol class DaggerAppComponent or cannot find symbol class DaggerActivityComponent
Asked Answered
B

12

20

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);
    }
}
Brieta answered 3/5, 2017 at 9:11 Comment(8)
Have you click on make project? Click on make project then you go you will get DaggerAppComponant.Eserine
Its same effect Not reslovedBrieta
I am not much aware about this , i am aslo beginner to folow this structure but you can refer saulmm.github.io/when-Thor-and-Hulk-meet-dagger2-rxjava-1 and another blog : blog.mindorks.com/android-amazing-open-source-apps-e44f520593ccEserine
What is the output in the gradle console? That will tell you where the problem isRecto
its seems like /home/mehul/Mehul/AndroidStudioProjects/OneTouchProtect/app/src/main/java/com/onetouchprotect/util/FontCache.java:67: warning: Application namespace for attribute bind:font will be ignored. public static void setFont(TextView textView, String fontName){ ^ 100 errors 1 warning FAILED :app:buildInfoGeneratorOtpPlusDebug FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileOtpPlusDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.Brieta
@user1841264 you need to edit your post to include the compiler error output. It will tell you where the problem is with your setup. Otherwise it's very hard to answer this questionRecto
its seems like above errorBrieta
check this answer maybe helped you. https://mcmap.net/q/272951/-dagger-2-sometimes-on-compiling-i-get-quot-cannot-find-symbol-class-daggerapplicationcomponent-quotCoagulum
C
25

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.

Cato answered 8/2, 2019 at 4:17 Comment(3)
i wasted almost 4 hrs searching through web, blogs, tuts, gits but no luck what you have told is not just the solution of this problem. now i will apply this to all the problems i have in future. thank you man.Hostess
well my problem was not related to dagger instead my problem was that : i was try to insert an unknown custom data type to android room and it didn't give me error but i found out about it in that section as you mentioned. thank you once againHostess
in my case, nothing would work -- after a git merge there was a variable being defined twice in the same class. android studio didnt complain about that, but instead would crash and blame dagger and glide.Gillard
A
10

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"
Agustin answered 30/1, 2018 at 19:23 Comment(0)
E
9

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

Eddie answered 1/6, 2018 at 15:54 Comment(0)
C
5

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

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
...
}
Costard answered 7/9, 2017 at 23:44 Comment(1)
Yes. If you have declared any of your providesMethods in the module singleton, then it is mandatory to add @Singleton annotation to the appcomponent.Lozoya
D
2

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.

Danndanna answered 13/9, 2017 at 10:2 Comment(0)
S
1

Have you tried this?

@ActivityScope
@Component(modules = ActivityModule.class, dependencies = 
AppComponent.class)
public interface ActivityComponent extends AppComponent {
    void inject(SignInActivity activity);
}
Salts answered 22/7, 2017 at 23:24 Comment(1)
Thanks for this reply, it helped me to solve my problem. Do you have any idea on how this works and why we have to do this? Btw, in my case, I added applicationscope.Russon
T
1

I had such a problem, due to mixed code with kotlin and java. Try adding a dependency for kotlin

 //dagger
    implementation 'com.google.dagger:dagger:2.31'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.31'
    kapt 'com.google.dagger:dagger-compiler:2.31' //for Kotlin
Tonettetoney answered 16/1, 2021 at 22:17 Comment(1)
This shouldn't be correct, kapt designed to take care of the Java files, no need annotationProcessorDolce
M
1

Updating to the latest version of Dagger did the trick for me:

implementation "com.google.dagger:dagger:2.35.1

Marnie answered 25/5, 2021 at 14:9 Comment(1)
#68231312 pls check this questionPositron
A
0

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.

About answered 13/6, 2018 at 9:0 Comment(1)
#68231312 pls check this questionPositron
S
0

I added below in gradle.properties:-

-Pandroid.incrementalJavaCompile=false

It worked for me

Sympathin answered 21/10, 2018 at 8:28 Comment(0)
K
0

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.

Kenishakenison answered 19/11, 2019 at 4:3 Comment(3)
kapt is important if you are using kotlin.Renie
@Renie yes. I am using both Java and Kotlin. My observation is that everything works fine when I just use annotationProcessor instead of kaptKenishakenison
I really recommend you looking at this thread #51067083. It is always good to go by book.Renie
A
0

I faced the same problem. The solution which worked for me was :

I had 2 component files ,

@Singleton
@Component(modules = {RoomModule.class})
public interface RoomComponent {
    void inject(SampleScreen sampleScreen);
}

@Singleton
@Component(modules = {AppModule.class,RetrofitModule.class})
public interface RetrofitComponent {
    void inject(SampleScreen sampleScreen);
}

and i was only injecting one via

DaggerRetrofitComponent.builder().retrofitModule(new RetrofitModule(URL2)).build().inject(this);

So i commented out the other one(RoomComponent) inject declaration and then it worked.

Americano answered 5/3, 2021 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.