> >

Overview

GeeTest captcha Android SDK supports for native Android.

Development environment

Item Description
Target Android 5.0+
Development environment Android Studio 4.1.0
Compile tool gradle (for ant compile, extract SDK, jar package and resource file access)
System dependency None
Third-party dependency OKHTTP
Dependency in demo OKHTTP

Resources

tem Description
Data communication flow chart Data communication flow chart , Interaction process
SDK API reference Check gt3-android-docs or the comments in examples
Custom API documentation Document download
Error code Error code

Installation

Download SDK

Remote dependency acquisition

The code is hosted in Maven Central. Check if the mavenCentral() configuration is declared in the main project build.gradle file before using it

# Compatible androidx
implementation 'com.geetest.sensebot:sensebot:4.4.2.1'

Manual download fetch

Please click the link below to download the latest SDK (in .zip format) (Compatible with androidx).

gt3-android-sdk

Import SDK(If there is a demand on the Google Store, please use the manual download integration method)

Except using the remote repository, you can also drag and drop the .aar file into libs folder.

import

After adding the .aar file into the libs folder, please check if .aar has been successfully imported into Library. Please insert the following code into the build.gradle of your project.

repositories {
flatDir {
dirs 'libs'
}
}

Then, add dependency for .aar package manually (AAR does not pass third-party dependencies by default, you need to manually add!).

implementation(name: 'geetest_sensebot_android_vx.y.z_date', ext: 'aar')
implementation 'com.squareup.okhttp3:okhttp:{latestVersion}'

libraries

Configure API

Android sdk provide the following APIs.

  1. captcha initialization
  2. Start captcha verification
  3. Get the verification callback data

Please firstly integrate server SDK, configure the id and key (get from GeeTest dashboard), then implement API1 and API2 in the captcha initialization method.

Please refer to the following code example for integration.

Compile and run your project

Compile your project and test GeeTest captcha.

build

Two integration options

  1. Bind GeeTest captcha to your own button

    sample1

  2. Integrate with GeeTest captcha button

    sample2

Example code

File configuration

  1. Set permissions

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  2. Proguard rules

GeeTest SDK has already been obfuscated. Obfuscating one more time may lead to unexpected error. By default the aar package already contains a proguard consumer rules file which can prevent duplicate obfuscating, therefore there should be no additional operation required. If you need to extract aar to separate jar and resource files, please copy the content of proguard.txt to your application’s proguard rule.

  1. If using resource file obfuscation tools, add the following rules to the allowlist:

    "R.drawable.gt3*",
    "R.mipmap.gt3*",
    "R.layout.gt3*",
    "R.string.gt3*",
    "R.style.gt3*",
    "R.id.gt3*",
    "R.id.*geetest*",

Bind GeeTest captcha to a custom button

  1. Create a captcha object

    Create the object in onCreate() method

    // Please initialize it in the onCreate method to get enough gesture data to ensure a successful first round of validation
    GT3GeetestUtils gt3GeetestUtils = new GT3GeetestUtils(this);
  1. Configure parameters for captcha initialization

    Configure the paratmers either in onCreate() method or when the object is called

    // Configure the bean file
    gt3ConfigBean = new GT3ConfigBean();
    // Set how captcha is presented,1:bind,2:unbind
    gt3ConfigBean.setPattern(1);
    // The default is false
    gt3ConfigBean.setCanceledOnTouchOutside(false);
    // Set language. Use system default language if null
    gt3ConfigBean.setLang(null);
    // Set the timeout for loading webview static files
    gt3ConfigBean.setTimeout(10000);
    // Set the timeout for webview request after user finishing the CAPTCHA verification. The default is 10,000
    gt3ConfigBean.setWebviewTimeout(10000);
    // Set callback listener
    gt3ConfigBean.setListener(new GT3Listener());
    gt3GeetestUtils.init(gt3ConfigBean);
    // Start CAPTCHA verification
    gt3GeetestUtils.startCustomFlow();

Integrate with GeeTest captcha button

  1. Insert captcha button

    <com.geetest.sdk.views.GT3GeetestButton
    android:id="@+id/btn_geetest"
    android:layout_width="290dp"
    android:layout_height="44dp"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="horizontal" />
  2. Create a captcha object

    Create the new object in onCreate() method

    // Please note that this step has to be done in onCreate() method
    GT3GeetestButton geetestButton = (GT3GeetestButton) findViewById(R.id.btn_geetest);
    GT3GeetestUtils gt3GeetestUtils = new GT3GeetestUtils(this);
  3. Configure parameter for captcha initialization

    Configure it in the life cycle of oncreate or configure it when it has been called

    // Configure bean file and 
    gt3ConfigBean = new GT3ConfigBean();
    // Set how captcha is presented,1:bind,2:unbind
    gt3ConfigBean.setPattern(2);
    // The default is false
    gt3ConfigBean.setCanceledOnTouchOutside(false);
    // // Set language. Use system default language if null(init before setContentView to take immediate effect)
    gt3ConfigBean.setLang(null);
    // Set the timeout for loading webview static files
    gt3ConfigBean.setTimeout(10000);
    // Set the timeout for webview request after user finishing the CAPTCHA verification. The default is 10,000
    gt3ConfigBean.setWebviewTimeout(10000);
    // Set callback listener
    gt3ConfigBean.setListener(new GT3Listener());
    gt3GeetestUtils.init(gt3ConfigBean);
    // Bind
    geetestButton.setGeetestUtils(gt3GeetestUtils);

Handle captcha callback

To deal with the callback data during the verification, you need to process API1 request via onButtonClick method, and process API2 request via onDialogResult method.

	
gt3ConfigBean.setListener(new GT3Listener() {

/**
* CAPTCHA loading is completed
* @param duration Loading duration and version info,in JSON format
*/
@Override
public void onDialogReady(String duration) {
Log.e(TAG, "GT3BaseListener-->onDialogReady-->" + duration);
}

/**
* Verification result callback
* @param code 1:success, 0:fail
*/
@Override
public void onReceiveCaptchaCode(int code) {
Log.e(TAG, "GT3BaseListener-->onReceiveCaptchaCode-->" + code);
}

/**
* api2 custom call
* @param result
*/
@Override
public void onDialogResult(String result) {
Log.e(TAG, "GT3BaseListener-->onDialogResult-->" + result);
// Start api2 workflow
new RequestAPI2().execute(result);
}

/**
* Statistic info.
* @param result
*/
@Override
public void onStatistics(String result) {
Log.e(TAG, "GT3BaseListener-->onStatistics-->" + result);
}

/**
* Close the CAPTCHA
* @param num 1 Click the close button to close the CAPTCHA, 2 Click anyplace on screen to close the CAPTCHA, 3 Click return button the close
*/
@Override
public void onClosed(int num) {
Log.e(TAG, "GT3BaseListener-->onClosed-->" + num);
}

/**
* Verfication succeeds
* @param result
*/
@Override
public void onSuccess(String result) {
Log.e(TAG, "GT3BaseListener-->onSuccess-->" + result);
}

/**
* Verification fails
* @param errorBean Version info, error code & description, etc.
*/
@Override
public void onFailed(GT3ErrorBean errorBean) {
Log.e(TAG, "GT3BaseListener-->onFailed-->" + errorBean.toString());
}

/**
* api1 custom call
*/
@Override
public void onButtonClick() {
new RequestAPI1().execute();
}

});

Please refer to the demo project for details.

Was this helpful?
Send