title: How to use Glide with dagger2
body: <p>I am developing a music application. I want to load artist's image from LastFM so i do this this way
  1. I created a class <code>ArtistImageLoader extends BaseGlideUrlLoader</code>. 
  2. In the <code>getUrl</code> method i used retrofit2 to get the artist's image url from LastFM via <a href="http://www.last.fm/api/show/album.getInfo" rel="noreferrer">getArtistInfo</a> method. </p>
  
  <p>My problem is i didn't know how to inject the service of retrofit to make the request in <code>ArtistImageLoader</code>. I did this way but i got a NOP exception. <code>lastFmService</code> wasn't be not injected.</p>
  
  <pre><code>// GlideModule
  glide.register(MLocalArtist.class, InputStream.class, new    ArtistImageLoader.Factory());
  
  // Use it in onCreate method of ArtistsFragment
  DaggerLastFmComponent.builder().activityModule(new ActivityModule(getActivity()))
                  .netComponent(getNetComponent())
                  .build().inject(this);
  
  // use this code in onBindViewHolder method of artists recycler adapter
  Glide.with(getContext())
                  .from(MLocalArtist.class)
                  .load(localArtist)
                  .into(localArtistViewHolder.ivArtwork);
  </code></pre>
  
  <p><strong>ArtistImageLoader</strong></p>
  
  <pre><code>public class ArtistImageLoader extends BaseGlideUrlLoader&lt;MLocalArtist&gt; {
  
      @Inject
      LastfmService lastfmService;
  
      public ArtistImageLoader(Context context) {
          super(context);
      }
  
      @Override
      protected String getUrl(MLocalArtist model, int width, int height) {
          Call&lt;List&lt;MArtist&gt;&gt; call = lastfmService.getArtistInfo(model.artistName);
          try {
              List&lt;MArtist&gt; artists = call.execute().body();
              if (artists != null &amp;&amp; artists.size() &gt; 0) {
                  Timber.e(artists.get(0).toString());
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      public static class Factory implements ModelLoaderFactory&lt;MLocalArtist, InputStream&gt; {
          @Override public ModelLoader&lt;MLocalArtist, InputStream&gt; build(Context context, GenericLoaderFactory factories) {
              return new ArtistImageLoader(context);
          }
          @Override public void teardown() {
          }
      }
  }
  </code></pre>
  
  <p>Can you help me to do it? Thank you so much!</p>
  
  <p><strong>Glide Version</strong>: 3.7.0</p>
  
  <p><strong>Integration libraries</strong>: OkHttp3 + Dagger2</p>
  
  <p><strong>Device/Android Version</strong>: Android Emulator + Asus zenfone 5</p>
  
  <p><strong>EDIT 1</strong> </p>
  
  <p>ActivityComponent.java</p>
  
  <pre><code>@PerActivity
  @Component(dependencies = AppComponent.class, modules = ActivityModule.class)
  public interface ActivityComponent {
      Context context();
  }
  </code></pre>
  
  <p>AppComponent.java</p>
  
  <pre><code>@Singleton
  @Component(modules = AppModule.class)
  public interface AppComponent {
      App app();
  }
  </code></pre>
  
  <p>NetComponent.java</p>
  
  <pre><code>@Singleton
  @Component(modules = {NetModule.class, AppModule.class})
  public interface NetComponent {
      @Named("chartSoundCloud")
      Retrofit getSoundcloudChartRetrofit();
  
      @Named("searchSoundCloud")
      Retrofit getSoundcloudSearchRetrofit();
  
      @Named("lastFM")
      Retrofit getLastFmRetrofit();
  }
  </code></pre>
  
  <p>LastFmComponent.java</p>
  
  <pre><code>@PerActivity
  @Component(dependencies = NetComponent.class, modules = {LastFmModule.class, ActivityModule.class})
  public interface LastFmComponent extends ActivityComponent {
      void inject(ArtistsFragment artistsFragment);
  }
  </code></pre>
  
  <p>ActivityModule.java</p>
  
  <pre><code>@Module
  public class ActivityModule {
      private final Context mContext;
  
      public ActivityModule(Context mContext) {
          this.mContext = mContext;
      }
  
      @Provides
      @PerActivity
      Context provideActivityContext() {
          return mContext;
      }
  }
  </code></pre>
  
  <p>AppModule.java</p>
  
  <pre><code>@Module
  public class AppModule {
      private App app;
  
      public AppModule(App app){
          this.app = app;
      }
  
      @Singleton
      @Provides
      App provideApplication() {
          return app;
      }
  
      @Singleton
      @Provides @Named("applicationContext")
      Context provideApplicationContext(){
          return app;
      }
  }
  </code></pre>
  
  <p>LastFmModule.java</p>
  
  <pre><code>@Module
  public class LastFmModule {
  
      @Provides
      @PerActivity
      LastfmService provideLastFmService(@Named("lastFM") Retrofit retrofit) {
          return retrofit.create(LastfmService.class);
      }
  
  }
  </code></pre>
  
  <p>NetModule.java</p>
  
  <pre><code>@Module
  public class NetModule {
      static final int DISK_CACHE_SIZE = (int) MEGABYTES.toBytes(50);
  
      @Provides
      @Singleton
      Cache provideOkHttpCache(@Named("applicationContext") Context application) {
          Cache cache = new Cache(application.getCacheDir(), DISK_CACHE_SIZE);
          return cache;
      }
  
      @Provides
      @Singleton
      ScdClientIdInterceptor provideScdClientIdInterceptor() {
          return new ScdClientIdInterceptor();
      }
  
      @Provides
      @Singleton
      LastFMInterceptor provideLastFmInterceptor() {
          return new LastFMInterceptor();
      }
  
      @Provides
      @Singleton
      HttpLoggingInterceptor provideHttpLoggingInterceptor() {
          return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
      }
  
      @Provides
      @Singleton
      @Named("soundcloud-Http")
      OkHttpClient provideOkHttpSoundCloudClient(Cache cache,  ScdClientIdInterceptor clientIdInterceptor, HttpLoggingInterceptor httpLoggingInterceptor) {
          return createOkHttpClient(cache, clientIdInterceptor, httpLoggingInterceptor);
      }
  
      @Provides
      @Singleton
      @Named("lastFM-Http")
      OkHttpClient provideOkHttpLastFmClient(Cache cache, LastFMInterceptor clientIdInterceptor, HttpLoggingInterceptor httpLoggingInterceptor) {
          return createOkHttpClient(cache, clientIdInterceptor, httpLoggingInterceptor);
      }
  
      private OkHttpClient createOkHttpClient(Cache cache,  Interceptor clientIdInterceptor, HttpLoggingInterceptor httpLoggingInterceptor) {
          OkHttpClient okHttpClient = new OkHttpClient.Builder()
                  .cache(cache)
                  .addInterceptor(clientIdInterceptor)
                  .addInterceptor(httpLoggingInterceptor)
                  .connectTimeout(30, TimeUnit.SECONDS)
                  .readTimeout(30, TimeUnit.SECONDS)
                  .writeTimeout(30, TimeUnit.SECONDS)
                  .build();
  
          return okHttpClient;
      }
  
      @Provides
      @Singleton
      Gson provideGson() {
          return GsonFactory.create();
      }
  
      @Provides
      @Singleton
      @Named("searchSoundCloud")
      Retrofit provideSearchSoundCloudRetrofit(Gson gson, @Named("soundcloud-Http") OkHttpClient okHttpClient) {
          Retrofit searchRetrofit = new Retrofit.Builder()
                  .baseUrl(Constants.BASE_SOUNDCLOUD_API_URL)
                  .client(okHttpClient)
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .build();
          return searchRetrofit;
      }
  
      @Provides
      @Singleton
      @Named("chartSoundCloud")
      Retrofit provideChartSoundCloudRetrofit(Gson gson, @Named("soundcloud-Http") OkHttpClient okHttpClient) {
          Retrofit chartRetrofit = new Retrofit.Builder()
                  .baseUrl(Constants.BASE_SOUNDCLOUD_API_V2_URL)
                  .client(okHttpClient)
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .build();
          return chartRetrofit;
      }
  
      @Provides
      @Singleton
      @Named("lastFM")
      Retrofit provideLastFmRetrofit(Gson gson, @Named("lastFM-Http") OkHttpClient okHttpClient) {
          Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl(Constants.LASTFM_API_URL)
                  .client(okHttpClient)
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .build();
          return retrofit;
      }
  }
  </code></pre>