By default, the image pipeline uses the HttpURLConnection which is included in the Android framework. However, if needed by the app a custom network layer can be used. Fresco already contains one alternative network layer that is based on OkHttp.

Using OkHttp

OkHttp is a popular open-source networking library.

1. Gradle setup

In order to use it, the dependencies section of your build.gradle file needs to be changed. Along with the Gradle dependencies given on the Getting started page, add just one of these:

For OkHttp2:

1
2
3
4
dependencies {
  // your project's other dependencies
  implementation "com.facebook.fresco:imagepipeline-okhttp:3.5.0"
}

For OkHttp3:

1
2
3
4
dependencies {
  // your project's other dependencies
  implementation "com.facebook.fresco:imagepipeline-okhttp3:3.5.0"
}

2. Configuring the image pipeline to use OkHttp

You must also configure the image pipeline. Instead of using ImagePipelineConfig.newBuilder, use OkHttpImagePipelineConfigFactory:

1
2
3
4
5
6
7
8
Context context;
OkHttpClient okHttpClient; // build on your own
ImagePipelineConfig config = OkHttpImagePipelineConfigFactory
    .newBuilder(context, okHttpClient)
    . // other setters
    . // setNetworkFetcher is already called for you
    .build();
Fresco.initialize(context, config);

For a more detailed example of this, see how this if configured in the Fresco showcase app.

Handling sessions and cookies correctly

The OkHttpClient you pass to Fresco in the above step should be set up with interceptors needed to handle authentications to your servers. See this bug and the solutions outlined there for some problems that have occurred with cookies.

Using your own network fetcher (optional)

For complete control on how the networking layer should behave, you can provide one for your app. You must subclass NetworkFetcher, which controls communications to the network. You can also optionally subclass FetchState, which is a data structure for request-specific information.

Our implementation for OkHttp 3 can be used as an example. See its source code.

You must pass your network producer to the image pipeline when configuring it:

1
2
3
4
5
ImagePipelineConfig config = ImagePipelineConfig.newBuilder()
  .setNetworkFetcher(myNetworkFetcher);
  . // other setters
  .build();
Fresco.initialize(context, config);