Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can dagger injection done in static method?

I have this network module . I want to inject Network module in static method of ErrorUtils.

@Module
public class NetworkModule {
    private final String END_POINT = "https://www.myurl.com/";

    @Provides
    @Singleton
    public OkHttpClient getOkHttpClient() {
            OkHttpClient okHttpClient = builder.build();
            return okHttpClient;
       }

    @Provides
    @Singleton
    public GsonConverterFactory getGsonConverterFactory() {
        return GsonConverterFactory.create();
    }

    @Provides
    @Singleton
    public Retrofit getRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory) {
        return new Retrofit.Builder()
                .baseUrl(END_POINT)
                .client(okHttpClient)
                .addConverterFactory(gsonConverterFactory)
                .build();
    }

    @Provides
    @Singleton
    public RetrofitService getRetrofitService(Retrofit retrofit) {
        return retrofit.create(RetrofitService.class);
    }

And I want to inject this module in static method as :

public class ErrorUtils {
    @Inject
    static Retrofit retrofit;

    public static RestError parseError(Response<?> response) {

        **//showing error while writing this line**

        MyApplication.getComponent().inject(ErrorUtils.class);
        Converter<ResponseBody, RestError> converter = retrofit.responseBodyConverter(RestError.class, new Annotation[0]);

        RestError error;

        try {
            error = converter.convert(response.errorBody());
        } catch (IOException e) {
            return new RestError();
        }

        return error;
    }
}

How can we inject module in static method ,any suggestion ?

like image 450
Rajesh Khadka Avatar asked May 01 '16 07:05

Rajesh Khadka


People also ask

How does dagger injection work?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.

What is static injection?

A static web injection, also known as a redirect attack, is a technique used to redirect an infected victims' traffic to a malicious server that hosts a replica of the original website that was requested.

What does inject mean in dagger?

Dependency Injection, or DI in short, is a design pattern that allows to delegate the creation of objects and their dependencies to another object or framework. It is gaining a lot of interest in Android application development. This post shows how to inject objects using Dagger 2 through a console app.


1 Answers

As can be seen in Migrating from Dagger 1

Dagger 2 does not support static injection.

Static methods and variables are generally a bad idea. In your case you could just make your ErrorUtils an object, e.g. with @Singleton scope. Then you could properly inject the service and also properly inject your errorUtils without the use of static calls.

If that is not an option, you can just provide a getter to your component

@Component interface MyComponent {
    Retrofit getRetrofit();
}

And then use that method to set your static variable.

like image 148
David Medenjak Avatar answered Sep 24 '22 19:09

David Medenjak