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);
    }
}
yennsarah
  • 5,467
  • 2
  • 27
  • 48
mbpatel
  • 501
  • 1
  • 5
  • 19
  • Have you click on make project? Click on make project then you go you will get DaggerAppComponant. – Vasudev Vyas May 03 '17 at 09:26
  • Its same effect Not resloved – mbpatel May 03 '17 at 09:48
  • I am not much aware about this , i am aslo beginner to folow this structure but you can refer http://saulmm.github.io/when-Thor-and-Hulk-meet-dagger2-rxjava-1 and another blog : https://blog.mindorks.com/android-amazing-open-source-apps-e44f520593cc – Vasudev Vyas May 03 '17 at 10:07
  • What is the output in the gradle console? That will tell you where the problem is – David Rawson May 04 '17 at 00:25
  • 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. – mbpatel May 04 '17 at 08:52
  • @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 question – David Rawson May 15 '17 at 08:08
  • its seems like above error – mbpatel May 15 '17 at 16:25
  • check this answer maybe helped you. https://stackoverflow.com/a/54901939/6401241 – Radesh Feb 27 '19 at 09:37

12 Answers12

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.

Julian Sanchez
  • 567
  • 6
  • 6
  • 1
    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. – Harkal Mar 07 '19 at 22:54
  • 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 again – Harkal Mar 07 '19 at 23:03
  • 1
    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. – joe Nov 20 '19 at 19:22
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"
Fio
  • 3,088
  • 2
  • 13
  • 23
9

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

Ardit
  • 168
  • 1
  • 5
5

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

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
...
}
  • Yes. If you have declared any of your providesMethods in the module singleton, then it is mandatory to add @Singleton annotation to the appcomponent. – Varun A M May 14 '19 at 06:38
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.

NightFury
  • 13,436
  • 6
  • 71
  • 120
1

Have you tried this?

@ActivityScope
@Component(modules = ActivityModule.class, dependencies = 
AppComponent.class)
public interface ActivityComponent extends AppComponent {
    void inject(SignInActivity activity);
}
AndroidRocks
  • 292
  • 4
  • 16
  • 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. – CJR Dec 20 '18 at 21:50
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
1

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

implementation "com.google.dagger:dagger:2.35.1

IAmCoder
  • 3,179
  • 2
  • 27
  • 49
  • https://stackoverflow.com/questions/68230812/unable-to-use-in-local-variable-bindviews-not-applicable-to-local-variable-bu pls check this question – anshul raj Jul 04 '21 at 17:42
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.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • https://stackoverflow.com/questions/68230812/unable-to-use-in-local-variable-bindviews-not-applicable-to-local-variable-bu pls check this question – anshul raj Jul 04 '21 at 17:43
0

I added below in gradle.properties:-

-Pandroid.incrementalJavaCompile=false

It worked for me

sharma_kunal
  • 2,152
  • 1
  • 28
  • 28
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.

mike.kamau
  • 36
  • 7
  • kapt is important if you are using kotlin. – androidStud Nov 19 '19 at 05:29
  • @androidStud yes. I am using both Java and Kotlin. My observation is that everything works fine when I just use `annotationProcessor` instead of `kapt` – mike.kamau Nov 19 '19 at 07:15
  • I really recommend you looking at this thread https://stackoverflow.com/questions/51066583/should-i-change-all-annotationprocessor-to-kapt-in-kotlin-project. It is always good to go by book. – androidStud Nov 21 '19 at 01:52
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.