Blog

Third party image libraries

How-To: Using MAP SDK with Third-Party Image Libraries

July 24, 2018 · by Rahul Sharma ·
Categories:

The Akamai Mobile App Performance software development kit (MAP SDK) helps developers build the best possible mobile experience, even when connection speeds are slow or unavailable. MAP SDK supports the most commonly used, third-party image-loading libraries like Glide, Android Universal Image Loader, Picasso, and SDWebImage.

In this blog post, I’ll walk you through the steps required to pass image traffic through MAP SDK with these third-party image libraries on both Android and iOS.

Android

Glide

Default Glide (without any additional networking client)

By default, Glide uses HttpUrlFetcher.java, which then internally leverages HttpUrlConnection as a networking client. MAP SDK by default intercepts traffic going from HttpUrlConnection. Hence, for this scenario you don’t need to use any wrapper for intercepting image traffic. MAP SDK will automatically intercept the traffic and provide desired feature functionality.

Glide with OkHttp3

If Glideis used with OkHttp3, then Glide internally uses OkHttp3Stack and OkHttpClient as the networking client to fetch images from the server. In this scenario, OkHttpClient is used as the networking client by Glide library, and therefore MAP SDK will not intercept image traffic. Hence, we will need to use an interceptor to capture image traffic through MAP SDK. To do this, use AkaOkHttpInterceptor with OkHttpClient while registering the component for Glide in AppGlideModule

Here is the code snippet to do that:

@GlideModule
public final class MapSdkGlideModule extends AppGlideModule {
   @Override
   public void registerComponents(@NonNull Context context, @NonNull Glide glide,
                             @NonNull Registry registry) {
  OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(new AkaOkHttpInterceptor()).build();
   registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
 }
}

Android Universal Image Loader

By default, Android Universal Image Loader internally uses HttpUrlConnection as a networking client. MAP SDK by default intercepts traffic going from HttpUrlConnection. Hence, for this scenario you don’t need to use any wrapper for intercepting image traffic. MAP SDK will automatically intercept the traffic and provide desired feature functionality.

Picasso

Picasso is the most commonly used image loading library in Android. Let’s look at how to make MAP SDK work with Picasso.

Picasso version 2.5x and below

For intercepting Picasso traffic through MAP SDK, you need to add AkaPicassoDownloader while building the singleton Picasso object.

Here is the code snippet to do that:

Picasso picasso = new Picasso.Builder(getContext())
             .downloader( new AkaPicassoDownloader(getContext()))
             .build();
Picasso. setSingletonInstance (picasso);

Picasso versions above 2.5x

For Picasso 2.6x and above, Okhttp3 is used as the default client. MAP SDK has its own AkaOkHttpInterceptor for OkHttpClient to intercept image traffic, so you’ll need to add AkaOkHttpInterceptor in your OkHttpClient to intercept image traffic with MAP SDK.

Here is the code snippet to do that:

OkHttpClient client = new OkHttpClient.Builder()
          .addInterceptor(new AkaOkHttpInterceptor())
          .build();
Picasso picasso new Picasso.Builder(this)
          .downloader(new OkHttp3Downloader(client))
          .build();
Picasso.setSingletonInstance(picasso);

iOS

SDWebImage

SDWebImage has its own initializer for NSURLSessionConfiguration; due to this, the MAP SDK initializer for NSURLSessionConfiguration doesn’t get initiated and thus image traffic doesn’t get intercepted by MAP SDK.

Before calling any SDWebImage calls, you need to do custom initialization of NSURLSessionConfiguration with a MAP SDK session configuration.

Here is the code snippet to do that:

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];

[VocServiceFactory setupSessionConfiguration:sessionConfig];

[[SDWebImageDownloader sharedDownloader] createNewSessionWithConfiguration:sessionConfig];

Rahul Sharma is a solutions architect at Akamai Technologies.