Virtual Avatar
  • iOS
  • Android : Java
  • Overview
  • Client SDKs
  • Demo app
  • Getting started
    • Integrate the SDK
    • Create a virtual avatar
    • ZegoCharacterHelper instructions
  • Guides
  • Best practice
  • Error codes
  • Server APIs
  • Documentation
  • Virtual Avatar
  • Getting started
  • Integrate the SDK

Integrate the SDK

Last updated:2022-12-23 13:03

Prerequisites

Before you integrate the ZegoAvatar SDK, ensure that the development environment meets the following requirements:

  • Android Studio 2.1 or later.
  • Android SDK 25, Android SDK Build-Tools 25.0.2, or Android SDK Platform-Tools 25.x.x or later is used.
  • An real Android device that is running on Android 5.1 or later and supports audio and video.
  • The front camera and microphone of the device function normally.

Integrate the SDK

1. Optional: Create a project

Skip this step if a project already exists.
  1. Open Android Studio and select File > New > New Project.

  2. Enter the project name and select the project storage path.

  3. Retain the default settings for other fields, and click Next and then Finish to complete the project creation.

2. Import the SDK

Supported platform architectures include armeabi-v7a and arm64-v8a.

  1. Go to SDK Downloads to download the latest version of SDK.

  2. Decompress the SDK package, copy the ZegoAvatar.aar file in the package to a project directory, such as app/libs.

    /Pics/ZegoAvatar/Android/SDK_cap.png

  3. Add SDK reference. Go to the app folder, open the build.gradle file, and introduce all JAR files under libs to dependencies.

    /Pics/Avatar_Android/avatar_aar_in_project.png

    implementation fileTree(dir: 'libs', include: ['*.jar', "*.aar"]) //Wildcard introduction

3. Set permissions

Based on actual application needs, set permissions required by the application.

Go to the app/src/main directory, open the AndroidManifest.xml file, and add permissions.

<!-- Permissions required by the SDK -->

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<!-- Some permissions required by the app -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Some key permissions of Android 6.0 must be dynamically applied instead of statically applied from the AndroidMainfest.xml file. Therefore, you need to execute the following code. requestPermissions indicates the activity method.

String[] permissionNeeded = {
    "android.permission.CAMERA"};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (ContextCompat.checkSelfPermission(this, "android.permission.CAMERA") != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(permissionNeeded, 101);
    }
}
Required Permission Description Reason for application
Required
CAMERA
Camera access permission
This permission is required when facial expressions are analyzed based on the image captured by the camera.
RECORD_AUDIO
Permission for recording audio data
This permission is required when facial expressions are analyzed based on the sound waves.
WRITE_EXTERNAL_STORAGE
Permission for writing data inside the SDK
The SDK will save logs and related configuration files inside the SDK. This permission is also required when screenshots or recorded videos need to be stored.
Optional
INTERNET
Network access permission
This permission is required when the SDK performs the authentication.
READ_EXTERNAL_STORAGE
File reading permission
This permission is required when the SDK needs to read the resource packages.

4. Prevent code obfuscation

In the proguard-rules.pro file, add the -keep class configuration for the SDK to prevent obfuscation of the SDK public class names.

-keep class **.zego.**{*;}

Import resource packages

Before using AI capabilities provided by the ZegoAvatar SDK, you need to import the corresponding resource packages. Go to Download to obtain the following resource packages.

You can import resource packages using the dynamic downloading or added from local method.

Resource name Description Package size
Support dynamic downloading
Recommended download time
AIModel.bundle
AI model resources of the ZegoAvatar SDK. To use the facial expression mirroring, speech simulation, automatic avatar generation, or other capabilities, you need to transfer the absolute path of the resource to the ZegoAvatar SDK.
  • Facial expression mirroring: 8.2 MB
  • Speech simulation: 2.4 MB
  • Automatic avatar generation: 12.4 MB
Yes
Download the resource before the ZegoAvatarService SDK is initialized.
base.bundle
Aesthetics resources, including the basic 3D human model resources, resource mapping table, and default human model appearance.
  • Android: 13 MB
  • iOS: 14.4 MB
Yes
Download the resource before the ZegoCharacterHelper class is created.
Packages
Makeup, widget, accessory, and other resources.
Each resource is 200 KB to 1 MB in size, depending on the resource complexity.
Yes
We recommend that you download the related resources only when you need them to reduce the occupation of the local storage space.

Packages contain some aesthetic resources. Contact ZEGOCLOUD team to obtain all aesthetic resources if required.

Added from local

  1. Go to SDK Downloads to obtain related resource packages.

  2. Decompress obtained resource packages, find the assets folder, and copy it to the assets folder of your project.

    /Pics/Avatar_Android/SDK_resouce_cap_3.png

    /Pics/Avatar_Android/resource_in_pro_3.png

  1. During project running, copy the AIModel.bundle, base.bundle, and Packages files to the private directory (/data/data/package name/files) of the device by using the following code. (Note: The assets folder of the Android system is read-only.)
    AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
                "AIModel.bundle"/*The assets root directory in APK.*/, "assets"/* Directory in the SD card. The value is: getFilesDir().getAbsolutePath() + File.separator + destPath. */);
    AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
                "base.bundle", "assets");
    AssetsFileTransfer.copyAssetsDir2Phone(this.getApplication(),
                "Packages", "assets");
    /**
     * Copy all content in the assets/${filePath} directory to the ${destPath}/ directory under the storage directory of the mobile phone.
     *
     * @param activity activity Activity of the CopyFiles class is used.
     * @param filePath String File path corresponding to the assets directory in the Android APK, such as AIModel.bundle.
     * @param destPath String Target to be copied, such as /data/data/package name/files/assets/.
     */
    public static void copyAssetsDir2Phone(Context activity, String filePath, String destPath) {
        try {
            String[] fileList = activity.getAssets().list(filePath);
            if (fileList.length > 0) {//If it is a directory
                File file = new File(activity.getFilesDir().getAbsolutePath() + File.separator + destPath + File.separator + filePath);
                if (file.exists()) {
                    deleteAllFiles(file);
                }
                file.mkdirs();//If the folder does not exist, recursion is performed.
                for (String fileName : fileList) {
                    filePath = filePath + File.separator + fileName;

                    copyAssetsDir2Phone(activity, filePath, destPath);

                    filePath = filePath.substring(0, filePath.lastIndexOf(File.separator));
                    Log.i(TAG, filePath);
                }
            } else {//If it is a file
                InputStream inputStream = activity.getAssets().open(filePath);
                File file = new File(activity.getFilesDir().getAbsolutePath() + File.separator + destPath + File.separator + filePath);
                if (file.exists()) {
                    boolean delete = file.delete();
                }
                if (!file.exists() || file.length() == 0) {
                    FileOutputStream fos = new FileOutputStream(file);
                    int len = -1;
                    byte[] buffer = new byte[1024];
                    while ((len = inputStream.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);
                    }
                    fos.flush();
                    inputStream.close();
                    fos.close();
                }
            }
        } catch (IOException e) {
            Log.e(TAG, "copy file faild, src:" + filePath + " dest:" + destPath);
            e.printStackTrace();
        }
    }
  1. After the resources are copied and run on a mobile device, the following structure exists. Note: The returned directory of getFilesDir().getAbsolutePath() may vary depending on the mobile device. For example, for a Huawei mobile phone, the returned directory is /data/data/im.zego.zegoavatarexample.

    /Pics/ZegoAvatar/Android/android_assets.png

  2. To use a feature, introduce the absolute path of the resource required by the corresponding API.

Page Directory