This page shows you how to enableApp Checkin an Android app, using[your customApp Checkprovider](https://firebase.google.com/docs/app-check/android/custom-provider). When you enableApp Check, you help ensure that only your app can access your project's Firebase resources.

If you want to useApp Checkwith the default Play Integrity provider, see[EnableApp Checkwith Play Integrity on Android](https://firebase.google.com/docs/app-check/android/play-integrity-provider).

## Before you begin

- [Add Firebase to your Android project](https://firebase.google.com/docs/android/setup)if you haven't already done so.

- [Implement your customApp Checkprovider's server-side logic](https://firebase.google.com/docs/app-check/custom-provider).

## 1. Add theApp Checklibrary to your app

In your**module (app-level) Gradle file** (usually`<project>/<app-module>/build.gradle.kts`or`<project>/<app-module>/build.gradle`), add the dependency for theApp Checklibrary for Android. We recommend using the[Firebase Android BoM](https://firebase.google.com/docs/android/learn-more#bom)to control library versioning.

<br />

```carbon
dependencies {
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:34.6.0"))

    // Add the dependency for the App Check library
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-appcheck")
}
```

By using the[Firebase Android BoM](https://firebase.google.com/docs/android/learn-more#bom), your app will always use compatible versions of Firebase Android libraries.
*(Alternative)* Add Firebase library dependencies*without* using theBoM

If you choose not to use theFirebase BoM, you must specify each Firebase library version in its dependency line.

**Note that if you use*multiple* Firebase libraries in your app, we strongly recommend using theBoMto manage library versions, which ensures that all versions are compatible.**  

```groovy
dependencies {
    // Add the dependency for the App Check library
    // When NOT using the BoM, you must specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-appcheck:19.0.1")
}
```

<br />

## 2. Implement theApp Checkinterfaces

First, you need to create classes that implement the`AppCheckProvider`and`AppCheckProviderFactory`interfaces.

Your`AppCheckProvider`class must have a`getToken()`method, which collects whatever information your customApp Checkprovider requires as proof of authenticity, and sends it to your token acquisition service in exchange for anApp Checktoken. TheApp CheckSDK handles token caching, so always get a new token in your implementation of`getToken()`.  

### Kotlin

```kotlin
class YourCustomAppCheckToken(
    private val token: String,
    private val expiration: Long,
) : AppCheckToken() {
    override fun getToken(): String = token
    override fun getExpireTimeMillis(): Long = expiration
}

class YourCustomAppCheckProvider(firebaseApp: FirebaseApp) : AppCheckProvider {
    override fun getToken(): Task<AppCheckToken> {
        // Logic to exchange proof of authenticity for an App Check token and
        //   expiration time.
        // ...

        // Refresh the token early to handle clock skew.
        val expMillis = expirationFromServer * 1000L - 60000L

        // Create AppCheckToken object.
        val appCheckToken: AppCheckToken = YourCustomAppCheckToken(tokenFromServer, expMillis)
        return Tasks.forResult(appCheckToken)
    }
}https://github.com/firebase/snippets-android/blob/294e0edf389e04ad85622081afb988aed37c20fb/appcheck/app/src/main/java/com/google/firebase/example/appcheck/kotlin/CustomProvider.kt#L16-L40
```

### Java

```java
public class YourCustomAppCheckToken extends AppCheckToken {
    private String token;
    private long expiration;

    YourCustomAppCheckToken(String token, long expiration) {
        this.token = token;
        this.expiration = expiration;
    }

    @NonNull
    @Override
    public String getToken() {
        return token;
    }

    @Override
    public long getExpireTimeMillis() {
        return expiration;
    }
}

public class YourCustomAppCheckProvider implements AppCheckProvider {
    public YourCustomAppCheckProvider(FirebaseApp firebaseApp) {
        // ...
    }

    @NonNull
    @Override
    public Task<AppCheckToken> getToken() {
        // Logic to exchange proof of authenticity for an App Check token and
        //   expiration time.
        // ...

        // Refresh the token early to handle clock skew.
        long expMillis = expirationFromServer * 1000L - 60000L;

        // Create AppCheckToken object.
        AppCheckToken appCheckToken =
                new YourCustomAppCheckToken(tokenFromServer, expMillis);

        return Tasks.forResult(appCheckToken);
    }
}https://github.com/firebase/snippets-android/blob/294e0edf389e04ad85622081afb988aed37c20fb/appcheck/app/src/main/java/com/google/firebase/example/appcheck/CustomProvider.java#L17-L62
```

Also, implement a`AppCheckProviderFactory`class that creates instances of your`AppCheckProvider`implementation:  

### Kotlin

```kotlin
class YourCustomAppCheckProviderFactory : AppCheckProviderFactory {
    override fun create(firebaseApp: FirebaseApp): AppCheckProvider {
        // Create and return an AppCheckProvider object.
        return YourCustomAppCheckProvider(firebaseApp)
    }
}https://github.com/firebase/snippets-android/blob/294e0edf389e04ad85622081afb988aed37c20fb/appcheck/app/src/main/java/com/google/firebase/example/appcheck/kotlin/CustomProvider.kt#L44-L49
```

### Java

```java
public class YourCustomAppCheckProviderFactory implements AppCheckProviderFactory {
    @NonNull
    @Override
    public AppCheckProvider create(@NonNull FirebaseApp firebaseApp) {
        // Create and return an AppCheckProvider object.
        return new YourCustomAppCheckProvider(firebaseApp);
    }
}https://github.com/firebase/snippets-android/blob/294e0edf389e04ad85622081afb988aed37c20fb/appcheck/app/src/main/java/com/google/firebase/example/appcheck/CustomProvider.java#L66-L73
```

## 3. InitializeApp Check

Add the following initialization code to your app so that it runs before you use any other Firebase SDKs:  

### Kotlin

```kotlin
Firebase.initialize(context)
Firebase.appCheck.installAppCheckProviderFactory(
    YourCustomAppCheckProviderFactory(),
)https://github.com/firebase/snippets-android/blob/294e0edf389e04ad85622081afb988aed37c20fb/appcheck/app/src/main/java/com/google/firebase/example/appcheck/kotlin/CustomProvider.kt#L54-L57
```

### Java

```java
FirebaseApp.initializeApp(/*context=*/ context);
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
firebaseAppCheck.installAppCheckProviderFactory(
        new YourCustomAppCheckProviderFactory());https://github.com/firebase/snippets-android/blob/294e0edf389e04ad85622081afb988aed37c20fb/appcheck/app/src/main/java/com/google/firebase/example/appcheck/CustomProvider.java#L78-L81
```

## Next steps

Once theApp Checklibrary is installed in your app, start distributing the updated app to your users.

The updated client app will begin sendingApp Checktokens along with every request it makes to Firebase, but Firebase products will not require the tokens to be valid until you enable enforcement in theApp Checksection of the Firebase console.

### Monitor metrics and enable enforcement

Before you enable enforcement, however, you should make sure that doing so won't disrupt your existing legitimate users. On the other hand, if you're seeing suspicious use of your app resources, you might want to enable enforcement sooner.

To help make this decision, you can look atApp Checkmetrics for the services you use:

- [MonitorApp Checkrequest metrics](https://firebase.google.com/docs/app-check/monitor-metrics)forFirebase AI Logic,Data Connect,Realtime Database,Cloud Firestore,Cloud Storage,Authentication, Google Identity for iOS, Maps JavaScript API, and Places API (New).
- [MonitorApp Checkrequest metrics forCloud Functions](https://firebase.google.com/docs/app-check/monitor-functions-metrics).

### EnableApp Checkenforcement

When you understand howApp Checkwill affect your users and you're ready to proceed, you can enableApp Checkenforcement:

- [EnableApp Checkenforcement](https://firebase.google.com/docs/app-check/enable-enforcement)forFirebase AI Logic,Data Connect,Realtime Database,Cloud Firestore,Cloud Storage,Authentication, Google Identity for iOS, Maps JavaScript API, and Places API (New).
- [EnableApp Checkenforcement forCloud Functions](https://firebase.google.com/docs/app-check/cloud-functions).

### UseApp Checkin debug environments

If, after you have registered your app forApp Check, you want to run your app in an environment thatApp Checkwould normally not classify as valid, such as an emulator during development, or from a continuous integration (CI) environment, you can create a debug build of your app that uses theApp Checkdebug provider instead of a real attestation provider.

See[UseApp Checkwith the debug provider on Android](https://firebase.google.com/docs/app-check/android/debug-provider).