Android Integration

更新时间:
复制 MD 格式

This document explains how to integrate the ID Verification SDK for Android and provides code examples.

Limitations

The SDK supports mobile devices (phones or tablets) running Android 4.3 (API level 18) or later. The x86 architecture is not supported.

Permissions

The SDK requires the following permissions:

Permission

Required

Description

android.permission.INTERNET

Yes

Required for the SDK to connect to the internet.

android.permission.ACCESS_NETWORK_STATE

No (Recommended)

android.permission.CAMERA

Yes

Camera-related permissions. This is a runtime permission on Android 6.0 (API level 23) and later.

android.permission.READ_PHONE_STATE

No (Recommended)

android.permission.READ_EXTERNAL_STORAGE

No (Recommended)

Download and configure the SDK

  1. Download the Android-CardVerifySDK. The SDK is a standard Android Archive (AAR) file.

  2. After downloading, unzip the package. Copy all AAR files from the Android SDK folder to your project's libs directory. Then, add the following dependencies to your module-level build.gradle file.

// SDK modules 
implementation files('libs/aliyun-identitybase-meta-sdk-xxx.aar') 
implementation files('libs/aliyun-identityocrservice-meta-sdk-xxx.aar') 
implementation files('libs/aliyun-identitycrypto-meta-sdk-1.0.0.aar') 
implementation files('libs/aliyun-identityplatform-meta-sdk-1.0.0.aar') 
implementation files('libs/aliyun-identityocr-meta-sdk-xxx.aar') 
implementation files('libs/aliyun-identitymnn-meta-sdk-xxx.aar')
// SDK third-party dependency modules 
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okio:okio:2.2.2'
implementation 'com.aliyun.dpa:oss-android-sdk:2.9.4'
implementation 'com.alibaba:fastjson:1.1.72.android'
Note
  • In the dependencies, xxx represents the specific version number of the SDK.

  • Do not omit any third-party dependencies, as doing so will cause the SDK to malfunction.

API reference

The Android SDK provides methods for initialization (install), metainfo retrieval (getMetaInfo), and verification (verify).

Initialize the SDK (install)

  • Method signature:

    public void install(Context context);
  • Parameters:

    Parameter

    Type

    Description

    context

    Context

    The context of the current application.

Get metainfo (getMetaInfo)

  • Method signature:

    public static String getMetaInfo(Context context);
  • Parameters:

    Parameter

    Type

    Description

    context

    Context

    The context of the current application.

  • Return value: A string in JSON format that contains information about the mobile device's environment.

    {
      "apdidToken": "",
      "appName": "com.aliyun.identity.platform",
      "appVersion": "1.0.1",
      "bioMetaInfo": "5.1.0:11501568,4",
      "deviceBrand": "xxx",
      "deviceManufacturer": "xxx",
      "deviceModel": "xxx",
      "deviceType": "android",
      "identityVer": "1.0.0",
      "osVersion": "10",
      "sdkVersion": "1.0.9"
    }

Start verification (verify)

Important
  • Before calling this method, pass the metainfo to your server and obtain a certifyId by calling the server-side InitCardVerify API.

  • The certifyId obtained from the server corresponds to the transactionId parameter of the verify method.

  • Method signature:

    public void verify(String transactionId, Map<String, String> extParams, IdentityCallback callback);
  • Parameters:

    Parameter

    Type

    Description

    transactionId

    String

    The certifyId obtained from the server-side initialization API (InitCardVerify).

    Important

    Each transactionId is valid for a single call to the verify method. You must obtain a new transactionId for each verification attempt.

    extParams

    Map<String, String>

    Optional parameters. In most cases, you can pass null.

    For details on the supported custom fields, see the extParams configuration table below.

    callback

    IdentityCallback

    A callback interface for the verification result. For details about the return codes in the callback, see the IdentityResponse.code table.

    public class IdentityResponse {
        // See the "IdentityResponse.code table" for details.
        public int code;
    
        // A description of the return code.
        public String message;
    }
    
    public interface IdentityCallback {
        boolean response(IdentityResponse response);
    }
  • extParams configuration:

    Key

    Description

    Example (String)

    IdentityParams.OcrResultButtonColor

    The color of the button on the OCR result page.

    #FF0000

    IdentityParams.RoundProgressColor

    The color of the circle during the face scan.

    #FF0000

    IdentityParams.ShowBlbumIcon

    Specifies whether to display the album upload entry during the document OCR step:

    • 1: Display (default)

    • 0: Do not display

    1

    IdentityParams.CardOcrTimeOutPeriod

    The timeout period for the OCR step.

    The value must be between 20 and 60 seconds. The default is 20 seconds.

    20

    IdentityParams.SdkLanguage

    The SDK supports the following languages by default:

    • zh-Hans: Simplified Chinese

    • zh-Hant: Traditional Chinese

    • en: English

    • ja: Japanese

    • ko: Korean

    • id: Indonesian

    • ms: Malay

    • th: Thai

    • vi: Vietnamese

    Note

    In addition to the default languages, the SDK also supports custom languages. For more information, see Add a language.

    zh-Hans

    CloseButtonLayout

    The position of the close button:

    • left: On the left (default)

    • right: On the right

    left

  • IdentityResponse.code table:

    Return code

    Billed

    Description

    1000

    Yes

    The user completed the verification process, and the recommended result is Pass. This result is for reference only. Call the server-side CheckResult API to get the definitive verification result.

    1001

    Yes

    The user completed the verification process, and the recommended result is Fail. This result is for reference only. Call the server-side CheckResult API to get the definitive verification result and the detailed reason for failure.

    1002

    No

    System error.

    1003

    No

    SDK initialization failed. Check if the client's system time is correct.

    1004

    No

    Camera permission error. Try the following steps to resolve this issue:

    1. Ensure your app has been granted camera permission before starting the verification.

    2. If the permission error persists after authorization, try clearing the app's cache and retrying.

    1005

    No

    Network error.

    1006

    No

    User canceled the process.

    1007

    No

    Invalid transactionId.

    1009

    No

    Client timestamp error.

    1011

    No

    Incorrect document type submitted.

    1012

    No

    Key information is missing from the recognized document, or format validation failed.

    1013

    No

    Poor image quality.

    1014

    No

    The number of attempts exceeded the limit.

    1015

    No

    The Android system version is too low.

    1016

    No

    Camera permission not granted.

Add a language

To add a custom language:

  1. Open your app project (the module that integrates the ID Verification SDK).

  2. In your app project, create a resource directory for the target language and a strings.xml file within it. For example, to add Russian, create a values-ru-rRU directory under the main module and add the strings.xml file to it.

values
  values-de-rDE
  values-fr-rFR
  values-ru-rRU
    strings.xml
AndroidManifest.xml
  1. Translate the string values in the strings.xml file into the target language. Refer to the template in Appendix 1 and replace the Chinese text with the target language.

  2. In your code, specify the custom language in the "language-country" format. For example, to set the language to Russian:

Map&lt;String,String&gt; extParams = new HashMap<>();
extParams.put(IdentityParams.SdkLanguage,"ru-RU");
IdentityPlatform.getInstance().verify(transactionId, extParams, new IdentityCallback() {
        @Override
        public boolean response(final IdentityResponse response) {
            if (IdentityResponseCode.IDENTITY_SUCCESS == response.code) {
                Toast.makeText( context: MainActivity.this,
                        text: "Verify Success.",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText( context: MainActivity.this,
                        text: "Verify Failed. [" + response.code + "]" + response.message + ")",
                        Toast.LENGTH_LONG).show();
            }
            return true;
        }
});

Sample code

public class MainActivity extends AppCompatActivity {
  private String transactionId = "";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // Initialize the SDK
      IdentityPlatform.getInstance().install(MainActivity.this);
      // Get metainfo
      String metaInfo = IdentityPlatform.getMetaInfo(MainActivity.this);
      /**
       * Send metainfo to your app server. Then, call the server-side InitCardVerify API to obtain the transactionId.
       */
      // transactionId = getTransactionIdFromServer(metaInfo);
      // Set the SDK language
      Map<String, String> extParams = new HashMap<>();
      extParams.put(IdentityParams.SdkLanguage, "en");
      // Start verification
      IdentityPlatform.getInstance().verify(transactionId, extParams,
            new IdentityCallback() {
                @Override
                public boolean response(final IdentityResponse response) {
                    if (IdentityResponseCode.IDENTITY_SUCCESS == response.code) {
                        Toast.makeText(MainActivity.this,
                                "Verification passed", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this,
                                "Verification failed([" + response.code + "]" +
                                        response.message + ")",
                                Toast.LENGTH_LONG).show();
                    }
                    return true;
                }
            });
  }
}
        

Obfuscation configuration

-verbose
-keep class com.aliyun.identity.platform.api.** {*;}
-keep class com.aliyun.identity.IdentityUtils {*;}
-keep class com.aliyun.identity.ocr.IdentityOcrApi {*;}
-keep class com.aliyun.identity.platform.model.** {*;}
-keep class com.aliyun.identity.platform.config.** {*;}
-keep class com.aliyun.meta.verify.service.algorithm.** {*;}
-keep class com.aliyun.meta.verify.base.algorithm.** {*;}
-keep class com.aliyun.meta.verify.service.ocr.IdentityDocService {*;}
-keep class com.alibaba.fastjson.** {*;}
-keep class com.aliyun.identity.platform.log.** {*;}// Obfuscation for the log module, optional

Demo package

Download AliyunIdcardVerifyDemo.zip to try out the demo.