The face verification service provides an Android client SDK for implementing a face verification feature in your app. This topic shows you how to integrate the service into your Android application with code examples.
For questions about API integration, API usage, or other issues related to the AI capabilities of Alibaba Cloud Vision AI Open Platform, join our DingTalk group (ID: 23109592) for assistance.
Prerequisites
Your application must run on a physical device with Android 4.4 or later.
Permissions
To support a foldable device, you must declare the following permission in your app's AndroidManifest.xml file. This allows the SDK to listen for fold and unfold state changes.
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>Get the SDK and demo code
Download the Android SDK
Download the Android SDK Demo
Running the SDK on an emulator poses security risks and is not supported.
Configure dependencies
Unzip the SDK package and place all .aar files into the libs directory.
In your project's build.gradle file, add the libs directory as a dependency repository.
repositories { flatDir { dirs 'libs' // The directory that contains the AAR files. } }In the app-level build.gradle file, add the following configuration.
android { // Add the following content. useLibrary 'org.apache.http.legacy' }Add the following dependencies to your app's build.gradle file.
dependencies { .... // Fastjson dependency implementation 'com.alibaba:fastjson:1.2.83_noneautotype' // Local dependencies compile(name: 'baseverify-2.3.42.251020152522', ext: 'aar') compile(name: 'facade-2.3.42.251020152522', ext: 'aar') compile(name: 'face-2.3.42.251020152522', ext: 'aar') compile(name: 'ocr-2.3.42.251020152522', ext: 'aar') compile(name: 'faceaudio-2.3.42.251020152522', ext: 'aar') compile(name: 'facelanguage-2.3.42.251020152522', ext: 'aar') compile(name: 'APSecuritySDK-deepSec-7.0.1.20250917.jiagu', ext: 'aar') compile(name: 'Android-AliyunFaceGuard-10057', ext: 'aar') }
Call the API
Initialize the SDK.
Initialize the SDK on your Android client to improve the user experience and prepare data for face verification. The following code provides an example:
DTFFacade.install(this);Get the metainfo.
The following code shows an example:
String metaInfos = DTFFacade.getMetaInfos(context);You must pass this metainfo value when making a mobile identity verification request.
Start the verification.
The following code shows an example:
DTFFacade dtfFacade = DTFFacadeBuilder.create(MainActivity.this); HashMap<String, String> extParams = new HashMap<>(); // To specify the UI orientation for liveness detection (for example, landscape), set this parameter. //extParams.put(DTFFacade.ZIM_EXT_PARAMS_KEY_SCREEN_ORIENTATION, DTFFacade.ZIM_EXT_PARAMS_VAL_SCREEN_LAND); // To enable video return for liveness detection, set this parameter. You can then get the local path of the video from response.videoFilePath. extParams.put(DTFFacade.ZIM_EXT_PARAMS_KEY_USE_VIDEO, DTFFacade.ZIM_EXT_PARAMS_VAL_USE_VIDEO_TRUE); // To customize the progress bar color on the liveness detection page (optional), set this parameter. For example, use #FF0000 for red. extParams.put(DTFFacade.ZIM_EXT_PARAMS_KEY_FACE_PROGRESS_COLOR, "#FF0000"); dtfFacade.verify(verificationToken, true, extParams, new DTFCallback() { @Override public boolean response(final DTFResponse response) { if (1000 == response.code) { showMessageBox("Verification successful"); } else { showMessageBox("Verification failed ([" + response.code + "]" + response.reason + ")"); } return true; } });NoteThe
verificationTokenreturned by the Identity Verification Mobile Client Request API is valid for 30 minutes from the time it is generated. If you perform face verification after the token expires, an error occurs. Please complete the verification within the validity period.The DTFCallback response handler must return a value, which is true by default.
Results
The face verification SDK callback returns a DTFResponse object with the result codes and reasons defined below:
/** * Callback result for face verification. */ public class DTFResponse { /** * Result code: * 1000: Client-side verification successful. (For the final result, query the server.) * 1001: System error * 1003: Verification interrupted * 2002: Network error * 2006: Client-side verification failed */ public int code; /** * Reason for the result. */ public String reason; /** * A description of the result. This field can be empty. */ public String msg; /** * The device token. */ public String deviceToken; /** * If video recording is enabled, this field contains the path to the video file. */ public String videoFilePath; /** * The verification photo. */ public byte[] bitmap; }Result descriptions
Http code
Description
1000
The face verification was successful. This result is for reference only. To get the final verification result, call the query mobile identity verification results API.
1001
System error.
1003
The verification was interrupted.
2002
Network error.
2006
The face verification failed. For more detailed failure reasons, call the query mobile identity verification results API to get the final verification result.
For more information, see Android client error codes.
Obfuscation configuration
To prevent issues from code obfuscation, add the following rules to your ProGuard configuration file to preserve the required classes:
-verbose
-keep class com.antdigital.livenessverify.network.mpass.biz.model.** { *; }
-keep class com.antdigital.livenessverify.api.** {*;}
-keep class com.alipay.zoloz.toyger.**{*;}
-keep class com.dtf.face.api.** {*;}
-keep class com.dtf.face.ocr.verify.DTFOcrFacade { *; }
-keep class com.dtf.face.verify.** {*;}
-keep class com.dtf.face.network.model.** {*;}
-keep class com.dtf.face.network.APICallback {*;}
-keep class com.dtf.face.config.**{*;}
-keep class com.dtf.face.log.** {*;}
-keep class com.dtf.face.ui.widget.ToygerWebView {*;}
-keep class com.dtf.toyger.base.** {*;}
-keep class com.dtf.face.network.mpass.biz.model.** { *; }
-keep class faceverify.** { *; }
-keep class ocrverify.** { *; }
-keep class com.alipay.alipaysecuritysdk.** { *; }
-keep class com.alipay.deviceid.** { *; }
-keep class face.security.device.api.** {*;}
# FastJson Keep
-keep class com.alibaba.fastjson.** {*;}
-keep class org.json.** {*;}