简体   繁体   中英

Injecting OkHttpClient using Dagger2

I'd like to be able to inject a new OkHttpClient using Dagger2 but am having a hard time. Most of the tutorials and instructions are written for applications and I'm building an aar (android library).

here's my Component:

@Singleton
@Component(modules = {NetworkModule.class})
public interface AndroidSdkComponent {
    void inject(OkHttpClient httpClient);
}

here's my module:

@Module
public class NetworkModule {

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient(Context context) {
        final OkHttpClient client = new OkHttpClient();
        if (Configuration.isAlphaBuild(context)) {
            client.networkInterceptors().add(new StethoInterceptor());
        }
        return client;
    }
}

At an entry point of my library I build the component like so:

DaggerAndroidSdkComponent.builder().networkModule(new NetworkModule())
                                            .build();

but later when I'm trying to @Inject OkHttpClient okHttpClient it appears to always be null. What am I doing wrong?

What I'm I doing wrong?

void inject(OkHttpClient httpClient);

This will inject an OkHttpClient with the dependencies it needs...But since you neither call it, nor have access to the fields of the okhttp client anyways this method is redundant and useless.

DaggerAndroidSdkComponent.builder().networkModule(new NetworkModule())
                                        .build();

This will build a component. It will build it. You still need to use it. eg call inject() and inject the object you want to inject.


What you need to do is update your interface to actually inject the object where you want the OkHttpClient in . You don't provide any information of where you want that client to be used, but you should have SomeClass like the following, where you already build the component with the code you provided above:

class SomeClass {
    @Inject
    OkHttpClient mClient(); // THIS is how you mark a field for injection

    void someMethod() {
        AndroidSdkComponent component = DaggerAndroidSdkComponent.builder().networkModule(new NetworkModule())
                .build();

        component.inject(this); // THIS is how you inject a field (okhttp!)
    }
}

Then you need to update the component to know about SomeClass that you want to be injected:

@Singleton
@Component(modules = {NetworkModule.class})
public interface AndroidSdkComponent {
    void inject(SomeClass someclass); // THIS will inject okhttp to your someclass object, as seen above
}

Please read some more tutorials about dagger and try playing with it, since it seems that you don't really understand how it works or what the different classes do. The User's Guide is a good place to start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM