Android

Requirements

Minimum version is Android 4.0 (Ice Cream Sandwich). Full feature support only available for 4.3+ (Jelly Bean MR2).

Installing the SDK

The Cloudflare Mobile SDK is available through EITHER of the two ways below. Please choose the most convenient option to you. You can add the SDK as a gradle dependency OR you can manually add the JAR file to your Android project.

Please choose between the options provided.

Installation via Gradle

Add the following code between ADD START and ADD END to your root-level build.gradle file:

repositories {
    // ...

    // ADD START
    maven { url "https://storage.googleapis.com/cloudflare-maven/public/" }
    // ADD END
}

In your module Gradle file (ex. app/build.gradle), add the cloudflare SDK dependency:

android {
    // ...
}

dependencies {
    // ...

    // ADD START
    implementation "com.cloudflare:cloudflare-mobile-sdk:[email protected]"
    // ADD END
}

Installation via JAR

Download the JAR here.

In Android Studio,

  1. Download the Cloudflare Mobile SDK JAR file on the portal page. You can find under a download link under your App settings.

  2. Copy the Cloudflare JAR file to your libs folder

  3. Right click the JAR file and click Add as library.

Add the following permission to your AndroidManifest.xml file.

// Allows Cloudflare to determine optimal mobile configurations
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Third Party Library Support

If you are using OkHttp you’ll need to follow the integration setup documents below. For other libraries, please see the Compatibility section at the bottom. 3rd party library support is available through EITHER of the 2 ways below.

You can instrument 3rd party objects through APIs available in the Cloudflare SDK OR use the cfandroid gradle plugin.

The Cloudflare gradle plugin is currently a beta product. It is designed to instrument networking libraries. In addition to convenience, another advantage of using the gradle plugin is visibility over requests from 3rd party dependencies using OkHttp. Using the gradle plugin results in a slight increase in build time dependent on the size of your application.

Instrumentation via APIs

To manually connect Cloudflare with the below 3rd party libraries, please follow the documents below.

Instrumentation via Gradle (BETA)

Add the following code between ADD START and ADD END to your root-level build.gradle file:

buildscript {
    repositories {
        // ...

        // ADD START
        maven { url "https://storage.googleapis.com/cloudflare-maven/public/" }
        // ADD END
    }

    dependencies {
        // ...

        // ADD START
        classpath "com.cloudflare.cfandroidservices:cfandroid-perf:1.0.4-beta"
        // ADD END
    }
}

In your module Gradle file (ex. app/build.gradle), add apply plugin line to the bottom of the file:

apply plugin: "com.android.application"

android {
    // ...
}

dependencies {
    // ...
}

// ADD START
apply plugin: "com.cloudflare.cfandroid"
// ADD END

Clean the project, then build!

Initializing Cloudflare

Initialization is the process of modifying your application in order to communicate with Cloudflare. Initialize Cloudflare Mobile SDK only once on the main thread at the beginning of your onCreate activity.

CFMobile.initialize(getApplicationContext(),CLOUDFLARE_CLIENT_KEY");

If you are adding the Cloudflare import manually, use

import com.cloudflare.api.CFMobile;

Cloudflare is now integrated with your Android application! The State for the app version will be ON and this can be changed by clicking the settings button under Action, toggling the switch in the upper right corner of the following screen, and then the Apply button at the bottom.

Verifying Integration

To check that Cloudflare is initialized you can add a Runnable parameter that will execute after initialize is completed.

isInitialized returns a boolean indicating Cloudflare is enabled.

Here’s an example of how you might verify Cloudflare initialization

CFMobile.initialize(getApplicationContext(),CLOUDFLARE_CLIENT_KEY", new Runnable() {
    @Override
    public void run() {
        if (CFMobile.isInitialized()) {
            // Cloudflare is ON.
            ...
        } else {
            // Cloudflare is OFF. Change log settings for more details.
            ...
        }
    }
});

We do not recommend executing your own initialization code inside the runnable block. Also note that the runnable is not executed on the UI thread.

Disabling Cloudflare

If for any reason you are looking to disable Cloudflare, navigate to the portal to your app settings and select the combination of application versions and/or Cloudflare SDK versions that should be enabled. Once disabled, Cloudflare will not initialize on the client device.

Compatibility

The Cloudflare Android SDK has been verified with the following Android libraries:

  • OkHttp
  • Volley
  • VolleyPlus
  • Retrofit2
  • Glide
  • Universal Image Loader
  • Picasso
  • Fresco

The Cloudflare Android SDK does not currently support:

  • React Native
  • Cordova

Considerations

  1. For applications using proguard, please follow the instructions here.

  2. The Cloudflare Android SDK does not currently support webviews.

  3. The Cloudflare Android SDK will not initialize on certain architectures like mips. This does not prevent an application from running normally on devices of those architectures despite possible warnings about platform limitations. Support for architectures include arm64-v8a, armeabi, armeabi-v7a, x86, x86_64 (fallback to x86). See hardware stats here.

Contact

Please reach out to [email protected] for any questions, comments, or concerns.