This page explains how to integrate Google Cloud Fraud Defense in your Android app.
The SDK uses reflection and dynamic code to allow modification and refinement of the detection system in the existing deployed applications/SDKs. The set of classes that are available in the system are restricted to a controlled list to avoid interference with the application.
Before you begin
Create a reCAPTCHA Key for the Android app platform.
Alternatively, you can copy the ID of an existing reCAPTCHA key for Android by performing one of the following steps:
To copy the ID of an existing key from the Google Cloud console, do the following:
Go to the Google Cloud Fraud Defense page.
- Select the Keys tab.
- In the reCAPTCHA keys list, find the key you want to copy, and then click .
- To copy the ID of an existing key using the REST API, use the projects.keys.list method.
- To copy the ID of an existing key using the gcloud CLI, use the gcloud recaptcha keys list command.
Prepare your Android environment
Native Android
Prepare your development environment by downloading and installing the latest version of Android Studio.
Ensure that you have an app with the minimum Android SDK value set to API 23: Android 6.0 (Marshmallow).
If you are creating a new mobile app, create a test application by starting a new Android Studio project:
- Select Empty Activity. If you want to use Jetpack Compose on your app choose Empty Compose Activity.
- Set the language to kotlin.
- Set the minimum SDK value to API 23: Android 6.0 (Marshmallow).
Ensure that Google's Maven repository
google()is in the list of repositories in the project-levelbuild.gradlefile as shown in the following snippet:allprojects { repositories { google() } }For more information, see Google's Maven repository.
To add the reCAPTCHA API dependency, add the following build rule to the
dependenciessection of your app-levelbuild.gradlefile.implementation 'com.google.android.recaptcha:recaptcha:18.9.2'For more information about adding dependencies in Android apps, see Add build dependencies.
Add internet permission between the first
<manifest>tag and the first<application>tag in your application's manifest (for example,AndroidManifest.xml). This permission is required because the reCAPTCHA API involves network operations.<manifest ...> <uses-permission android:name="android.permission.INTERNET" /> <application ...> ... </application> </manifest>If you want to use
AndroidXlibraries in your new project, compile the SDK to Android 9.0 or higher and add the following code snippet to yourgradle.properties.android.useAndroidX=true android.enableJetifier=trueFor more information, see Migrating to AndroidX.
Flutter
For detailed instructions about using reCAPTCHA through Flutter, see the Flutter documentation.
React Native
For detailed instructions about using reCAPTCHA through React Native, see the React Native documentation.
Integrate reCAPTCHA with your Android app
Instantiate a client by using the reCAPTCHA key (KEY_ID) that you created for your Android app.
Kotlin with
fetchClientThe
fetchClientmethod returns a client immediately and starts initializing the SDK in the background. It retries the communication with the reCAPTCHA server on network failures.class CustomApplication : Application() { private lateinit var recaptchaClient: RecaptchaClient // we recommend initializing in a ViewModel private val recaptchaScope = CoroutineScope(Dispatchers.IO) override fun onCreate() { super.onCreate() initializeRecaptchaClient() } private fun initializeRecaptchaClient() { recaptchaScope.launch { try { recaptchaClient = Recaptcha.fetchClient(this@CustomApplication, "KEY_ID") } catch(e: RecaptchaException) { // Handle errors ... // See "Handle errors" section } } }