# Glide

The integration with Glide can be achieved by importing Codavel’s Glide Module or integrating Codavel SDK into an existing Glide Module, using OkHttp (opens new window) as the HTTP engine.

Android minimum versions

  • SDK: 16
  • Gradle: 4.8.1

# Add repository

To add the Codavel SDK repository to your project, simply open the app or module's build.gradle and add the following block of code:

repositories {
  (...)
  maven { url 'https://artifact.codavel.com:8081/artifactory/android'}
  (...)
}
1
2
3
4
5

WARNING

Depending on your app organization, the Codavel SDK repository should be added differently. For example, your settings.gradle file can set specific dependency rules. Thus Codavel SDK repository needs to be added in settings.gradle file. Double-check that the repository and its dependencies were added correctly.

# Add SDK

Add the Codavel SDK and OkHttp as dependencies of the project and add the <APP_ID> and <APP_SECRET> to the app’s build.gradle script:

android {
  (...)
  defaultConfig {
    (...)
    resValue "string", "BOLINA_DEPLOY_ID", "<APP_ID>"
    resValue "string", "BOLINA_SECRET_ID", "<APP_SECRET>"
    (...)
  }
  (...)
}
dependencies {
  (...)
  implementation 'com.github.bumptech.glide:okhttp3-integration:<GLIDE_VERSION>'
  implementation 'com.codavel.bolina:interceptor_okhttp3:0.10.36'
  (...)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# Enable other dependencies

To enable all functionalities of Codavel’s monitoring solution, Codavel SDK has the following dependencies:

  • In case you have not migrated your app to androidx library artifacts, include the Work Manager by adding the following block of code to the app’s build.gradle script:
dependencies {
    (...)
    implementation 'androidx.work:work-runtime:2.2.0'
    (...)
}
1
2
3
4
5

The Work Manager is used to dump relevant statistics on background jobs, thus reducing its impact in your app's behavior.

Additionally, add the following lines to your gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true
1
2
  • Bolina also requires the following permissions, that should be added to your app’s manifest:
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>
1
2
3
4

WARNING

If your app already defines a network security config, please check this instructions on how to add Codavel SDK support

# Start interceptor

Make sure that, before performing any HTTP/HTTPS request, you import and start the interceptor:

  • Initialize the Bolina Configuration and set the Bolina Configuration domain name:
  • Java
  • Kotlin
BolinaConfiguration config = new BolinaConfiguration();
config.setDomainName("<BOLINA_SERVER_URL>");
1
2
  • [optional] Configure your Origin:
  • Java
  • Kotlin
config.addBolinaURLReplacement("<your.cdn.com>", "<your.origin.com>");
1

With this configuration, all requests intercepted by Bolina that would originally be sent to <your.cdn.com>, will be sent to a Bolina Server, that then will fetch the resource from <your.origin.com>. In case the Bolina transfer fails, the requests will be sent to <your.cdn.com>, as before. You can add multiple entries to be replaced. Otherwise, without this configuration, Codavel Performance Service servers will perform the same request as your HTTP/HTTPS clients would, to your current CDN or Origin.

  • Start the Bolina interceptor:
  • Java
  • Kotlin
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Runnable() {
    @Override
    public void run() {
        InterceptorSingleton.startInterceptor(getApplication(), config);
    }
});
1
2
3
4
5
6
7

WARNING

Check the interceptor lifecycle for more details on how to do it

  • Create a new Glide module class called CodavelGlideModule in your app project. This module will register an OkHttpClient with Bolina Interceptor (other options are available here) and register it in your Glide:
import android.content.Context;
import androidx.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.codavel.bolina.interceptor.okhttp3.CvlOkHttp3;
import java.io.InputStream;

import okhttp3.OkHttpClient;

@GlideModule
public final class CodavelGlideModule extends AppGlideModule {
    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
        (...)
        OkHttpClient okHttpClient = CvlOkHttp3.clientBuilderWithBolina().build();
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
        (...)
    }
    (...)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

From now on, all Glide requests will be transfered via Codavel CDN.

# Stop interceptor

  • Make sure you stop the interceptor whenever you don’t need it:
  • Java
  • Kotlin
try {
    InterceptorSingleton.getInstance().stopInterceptor();
} catch (BolinaDeployException e) {
    // handle the exception
    e.printStackTrace();
}
1
2
3
4
5
6

WARNING

Check the interceptor lifecycle for more details on how to do it