# OkHttp

Android minimum versions

  • SDK: 16
  • Gradle: 4.8.1

OkHTTP minimum versions

  • v4.9.0
  • v3.11.0

# 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 (we recommend replacing <OkHTTP_VERSION> with version 3.11.0 for OkHTTP 3 or 4.9.0 or higher for OkHTTP 4) 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.codavel.bolina:interceptor_okhttp3:0.10.36'
  implementation 'com.squareup.okhttp3:okhttp:<OkHTTP_VERSION>'
  (...)
}
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 an OkHttpClient with Bolina Interceptor (other options are available here):
  • Java
  • Kotlin
OkHttpClient okHttpClient = CvlOkHttp3.clientBuilderWithBolina().build();
1
  • Make the request that you desire to make
  • Java
  • Kotlin
Request request = new Request.Builder().url(url).build();

Callback callback = new Callback() {
    @Override
    public void onFailure(@NonNull Call call, @NonNull IOException e) {
        (...)
    }

    @Override
    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
        (...)
    }
};

okHttpClient.newCall(request).enqueue(callback);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 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

# Other options to integrate

We provide three different methods to integrate Bolina into a OkHttp Client:

  • Use a OkHttp client created and managed by Bolina
  • Java
  • Kotlin
OkHttpClient okHttpClient = CvlOkHttp3.clientBuilderWithBolina().build();
1
  • Register Bolina interceptor into your OkHttp client
  • Java
  • Kotlin
okHttpClient = CvlOkHttp3.registerBolina(okHttpClient);
1
  • Manually add our interceptor into your OkHttp client, along with Codavel’s network event listener to enable our monitoring solution
  • Java
  • Kotlin
CvlOkHttp3Interceptor cvlOkHttp3Interceptor = new CvlOkHttp3Interceptor();

OkHttpClient client = new OkHttpClient.Builder()
        (...)
        .eventListenerFactory(new NetworkEventsListener.NetworkEventsListenerFactory(cvlOkHttp3Interceptor))
        .addInterceptor(cvlOkHttp3Interceptor)
        .build();
1
2
3
4
5
6
7

WARNING

If your app already relies upon third party interceptors, make sure that Codavel SDK interceptor is the last interceptor added to the OkHttpClient