tencent cloud

Feedback

SDK Integration (Android Studio)

Last updated: 2022-05-19 16:47:43

    Android Project Configuration

    System requirement

    The UGSV SDK is supported on Android 4.0.3 (API level 15) and above, but hardware encoding can be enabled only on Android 4.3 (API level 18) or above.

    Development environment

    Below are the environment requirements for SDK development. You don’t need to meet the same requirements for application development, but make sure that your application is compatible with the SDK.

    • Android NDK: android-ndk-r12b
    • Android SDK Tools: android-sdk_25.0.2
    • minSdkVersion: 15
    • targetSdkVersion: 26
    • Android Studio (Android Studio is recommended. You can also use Eclipse + ADT.)

    Step 1. Integrate the SDK

    1. Create a project
    2. Configure the project
    3. In build.gradle of your project directory, add code that references the AAR file:
      dependencies {
         compile fileTree(dir: 'libs', include: ['*.jar'])
         // Import the SDK AAR file. Replace `x.y.zzzz` in `LiteAVSDK_UGC_x.y.zzzz` with the latest version number.
         compile(name: 'LiteAVSDK_UGC_7.4.9211', ext: 'aar')
         ...
      }
      
    4. Add flatDir to build.gradle of your project directory, and specify the local repository:
      allprojects {
       repositories {
           jcenter()
           flatDir {
               dirs 'libs'
           }
       }
      }
      
    5. Under defaultConfig in build.gradle of your project directory, specify architectures compatible with the NDK:
      defaultConfig {
         ...
         ndk {
             abiFilters "armeabi", "armeabi-v7a"
         }
      }
      
    6. Click Sync Now to build the project.

    Step 2. Configure app permissions

    Configure application permissions in AndroidManifest.xml. Audio/Video applications generally need the following permissions:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.Camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus" />
    

    Step 3. Configure the license

    1. Follow the steps in License Application to apply for a license, and copy the key and license URL in the VOD console, as shown below:
    2. Before you use UGSV features in your application, we recommend that you complete the following configuration in - Application onCreate():
      public class DemoApplication extends Application {
      String ugcLicenceUrl = ""; // Enter the license URL obtained from the console.
      String ugcKey = "";        // Enter the license key obtained from the console.
       @Override
      public void onCreate() {
          super.onCreate();
          TXUGCBase.getInstance().setLicence(instance, ugcLicenceUrl, ugcKey);
      }
      }
      
    Note:

    If you use a v4.7 license and have updated the SDK to v4.9, you can click Switch to New License in the console to generate a new license key and URL. A new license can be used only on v4.9 or above and should be configured as described above.

    Step 4. Print logs

    You can enable/disable console log printing and set the log level in TXLiveBase. See the sample code below.

    • setConsoleEnabled
      Sets whether to print the SDK logs in the Android Studio console.
    • setLogLevel
      Sets whether to allow the SDK to print local logs. By default, the SDK writes logs to the SD card in Android/data/com.tencent.liteav.demo/files/log/tencent/liteav. We recommend that you enable local log printing and provide the log file after a problem occurs so that we can offer technical support.
    • Viewing log files
      To reduce the storage space taken up by log files, the UGSV SDK encrypts local logs and limits their number. You need a log decompression tool to view the content of log files.
      TXLiveBase.setConsoleEnabled(true);
      TXLiveBase.setLogLevel(TXLiveConstants.LOG_LEVEL_DEBUG);
      

    Step 5. Build and run the project

    Call an SDK API in your project to get the SDK version number and verify whether your project is correctly configured.

    1. Import the SDK:
      Import the SDK class in MainActivity.java:

      import com.tencent.rtmp.TXLiveBase;
      
    2. Call the API:
      Call getSDKVersioin in onCreate to get the version number:

      String sdkver = TXLiveBase.getSDKVersionStr();
      Log.d("liteavsdk", "liteav sdk version is : " + sdkver);
      
    3. Build and run the project:
      If the above steps are performed correctly, you will build the project successfully and, after running it, you will see the following log information in logcat.
      09-26 19:30:36.547 19577-19577/ D/liteavsdk: liteav sdk version is : 7.4.9211

    Troubleshooting

    After importing the UGSV SDK, when you build and run your project, if the following error occurs:

    Caused by: android.view.InflateException:
    Binary XML file #14:Error inflating class com.tencent.rtmp.ui.TXCloudVideoView
    

    Follow the steps below to troubleshoot the problem:

    1. Check whether you have copied the JAR and SO files to the jniLibs directory.

    2. If you use the AAR method to integrate all features of the SDK, check in defaultConfig of build.gradle if the x64 SO libraries have been filtered out. The audio processing component, which enables the co-anchoring feature, does not support x64 phones currently.

      defaultConfig {
      ...   
      ndk {
          abiFilters "armeabi", "armeabi-v7a"
      }
      }
      
    3. Check if the package name of the SDK has been added to the “do not obfuscate” list.

      -keep class com.tencent.** { *;}
      
      
    4. Configure packaging options for your application.

    Integrating UGSV Modules

    This section describes how to quickly integrate the UGSV SDK into your existing project to enable a complete range of UGSV features including shooting, editing, and composition. The code and resources mentioned in this section can be found in the SDK ZIP file and the UGSV demo.

    Integrating UGCKit

    Step 1. Create a project (Empty Activity)

    1. Create an empty Android Studio project. You can name it ugc and give it a custom package name. Make sure the project can be built and run successfully.

    2. Configure build.gradle of the project.

      // Top-level build file where you can add configuration options common to all sub-projects/modules.
      buildscript {
      repositories {
          google()
          jcenter()
       }
      dependencies {
          # Copying starts.
          classpath 'com.android.tools.build:gradle:3.6.1'
          # Copying ends.
          // NOTE: Do not place your application dependencies here; they belong
          // in the individual module build.gradle files
      }
      }
      allprojects {
      repositories {
          google()
          jcenter()
          # Copying starts.
          flatDir {
              dirs 'src/main/jniLibs'
              dirs project(':ugckit').file('libs')
          }
          # Copying ends.
          jcenter() // Warning: this repository is going to shut down soon
      }
      }
      task clean(type: Delete) {
      delete rootProject.buildDir
      }
      # Copying starts.
      ext {
      compileSdkVersion = 25
      buildToolsVersion = "25.0.2"
      supportSdkVersion = "25.4.0"
      minSdkVersion = 16
      targetSdkVersion = 23
      versionCode = 1
      versionName = "v1.0"
      proguard = true
      rootPrj = "$projectDir/.."
      ndkAbi = 'armeabi-v7a'
      noffmpeg = false
      noijkplay = false
      aekit_version = '1.0.16-cloud'
      liteavSdk="com.tencent.liteav:LiteAVSDK_Professional:latest.release"
      }
      # Copying ends.
      
    3. Configure build.gradle of your application.

      plugins {
      id 'com.android.application'
      }
      android {
      # Copying starts.
      compileSdkVersion = rootProject.ext.compileSdkVersion
      buildToolsVersion = rootProject.ext.buildToolsVersion
      # Copying ends.
      defaultConfig {
          applicationId "com.yunxiao.dev.liteavdemo"
          # Copying starts.
          minSdkVersion rootProject.ext.minSdkVersion
          targetSdkVersion rootProject.ext.targetSdkVersion
          versionCode rootProject.ext.versionCode
          versionName rootProject.ext.versionName
          renderscriptTargetApi = 19
          renderscriptSupportModeEnabled = true
          multiDexEnabled = true
          ndk {
              abiFilters rootProject.ext.ndkAbi
          }
           # Copying ends.
          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
      }
       buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
      compileOptions {
          sourceCompatibility JavaVersion.VERSION_1_8
          targetCompatibility JavaVersion.VERSION_1_8
      }
      }
      dependencies {
      # Copying starts.
      compile fileTree(include: ['*.jar'], dir: 'libs')
      compile fileTree(include: ['*.jar'], dir: 'src/main/jniLibs')
      compile 'com.mcxiaoke.volley:library:1.0.19'
      compile 'com.android.support:design:25.3.1'
      compile 'com.android.support:recyclerview-v7:25.3.1'
      compile 'com.google.code.gson:gson:2.3.1'
      compile 'com.github.bumptech.glide:glide:3.7.0'
      compile 'com.github.ctiao:dfm:0.4.4'
      compile project(':ugckit')
      compile 'com.android.support.constraint:constraint-layout:1.1.3'
      # Copying ends.
      }
      
    4. Specify the Gradle version.

      distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
      

    Step 2. Import modules

    1. Copy ugckit module to the ugc directory of your newly created project.

    2. Copy beautysettingkit module to the ugc directory of your newly created project.

    3. Import ugckit to settings.gradle of the project.

    4. In UGC/settings.gradle of the project, import the modules below:

      include ':ugckit'
      include ':beautysettingkit'
      
    5. Add ugckit as a dependency for the app modules of your project.

      compile project(':ugckit')
      

    Step 3. Apply for a license

    Before using UGCKit, you must configure the license.

    Enabling shooting, importing, clipping, and special effects

    1. Configure the license and initialize UGCKit

    Configure the license in Application.java and initialize UGCKit.

    // Configure the license
    TXUGCBase.getInstance().setLicence(this, ugcLicenceUrl, ugcKey);
    // Initialize `UGCKit`
    UGCKit.init(this);
    

    2. Implement video shooting

    1. Create an XML file for shooting and add the code below:

      <com.tencent.qcloud.ugckit.UGCKitVideoRecord
      android:id="@+id/video_record_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
      
    2. Create an empty theme for shooting in res/values/styles.xml and inherit the default shooting theme of UGCKit.

      <style name="RecordActivityTheme" parent="UGCKitRecordStyle"/>
      
    3. Create an activity for shooting, inherit FragmentActivity, implement the ActivityCompat.OnRequestPermissionsResultCallback API, get a UGCKitVideoRecord object, and set the callback.

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // You must configure the theme in the code (`setTheme`) or in `AndroidManifest` (android:theme).
      setTheme(R.style.RecordActivityTheme);
      setContentView(R.layout.activity_video_record);
      // Get `UGCKitVideoRecord` 
      mUGCKitVideoRecord = (UGCKitVideoRecord) findViewById(R.id.video_record_layout);
      // Listen for shooting events
      mUGCKitVideoRecord.setOnRecordListener(new IVideoRecordKit.OnRecordListener() {
          @Override
          public void onRecordCanceled() {
              // Shooting was canceled.
          }
           @Override
          public void onRecordCompleted(UGCKitResult result) {
              // Shooting was completed.
          }
      });
      }
      @Override
      protected void onStart() {
      super.onStart();
      // Get whether the camera and audio recording permissions are granted. For details, see `Github/Demo`.
      if (hasPermission()) {
          // `UGCKit` takes over the shooting lifecycle. For details, see `Github/Demo`.
          mUGCKitVideoRecord.start();
      }
      }
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
      if (grantResults != null && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          mUGCKitVideoRecord.start();
      }
      }
      

    The UI view looks like this:

    3. Implement video importing

    1. Create an XML file and add the code below:

      <com.tencent.qcloud.ugckit.UGCKitVideoPicker
          android:id="@+id/video_picker"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
      
    2. Create an empty theme in res/values/styles.xml and inherit the default video importing theme of UGCKit.

      <style name="PickerActivityTheme" parent="UGCKitPickerStyle"/>
      
    3. Create an activity, inherit Activity, get a UGCKitVideoPicker object, and set the callback.

      @Override
      public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      // You must configure the theme in the code (`setTheme`) or in `AndroidManifest` (android:theme).
      setTheme(R.style.PickerActivityTheme);
      setContentView(R.layout.activity_video_picker);
      // Get `UGCKitVideoPicker`
      mUGCKitVideoPicker = (UGCKitVideoPicker) findViewById(R.id.video_picker);
      // Listen for video importing events
      mUGCKitVideoPicker.setOnPickerListener(new IPickerLayout.OnPickerListener() {
          @Override
          public void onPickedList(ArrayList<TCVideoFileInfo> list) {
              // `UGCKit` returns the paths of selected videos.
          }
      });
      }
      

    The UI view looks like this:

    4. Implement video clipping

    1. Create an XML file and add the code below:

      <com.tencent.qcloud.ugckit.UGCKitVideoCut
          android:id="@+id/video_cutter"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
      
    2. Create an empty theme in res/values/styles.xml and inherit the default video editing theme of UGCKit.

      <style name="EditerActivityTheme" parent="UGCKitEditerStyle"/>
      
    3. Create an activity, implement the FragmentActivity API, get a UGCKitVideoCut object, and set the callback.

      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // You must configure the theme in the code (`setTheme`) or in `AndroidManifest` (android:theme).
      setTheme(R.style.EditerActivityTheme);
      setContentView(R.layout.activity_video_cut);
      mUGCKitVideoCut = (UGCKitVideoCut) findViewById(R.id.video_cutter);
      // Get the paths of videos imported via the previous view
      mVideoPath = getIntent().getStringExtra(UGCKitConstants.VIDEO_PATH);
      // `UGCKit` sets the video path.
      mUGCKitVideoCut.setVideoPath(mVideoPath);
      // Listen for the generation of videos
      mUGCKitVideoCut.setOnCutListener(new IVideoCutKit.OnCutListener() {
          
          @Override
          public void onCutterCompleted(UGCKitResult ugcKitResult) {
              // Callback for the completion of video clipping
          }
           @Override
          public void onCutterCanceled() {
              // Callback for clipping being canceled
          }
      });
      }
      @Override
      protected void onResume() {
      super.onResume();
      // `UGCKit` takes over the lifecycle of the video clipping view. For details, see `Github/Demo`.
      mUGCKitVideoCut.startPlay();
      }
      

    The UI view looks like this:

    5. Implement special effects

    1. In the XML file of the editing activity, add the code below:

      <com.tencent.qcloud.ugckit.UGCKitVideoEdit
          android:id="@+id/video_edit"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
      
    2. Create an editing activity, inherit FragmentActivity, get a UGCKitVideoEdit object, and set the callback.

      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // You must configure the theme in the code (`setTheme`) or in `AndroidManifest` (android:theme).
      setTheme(R.style.EditerActivityTheme);
      setContentView(R.layout.activity_video_editer);
      // Set the video path (optional). You can skip this if the previous view is video clipping and `setVideoEditFlag(true)` is called.
      mVideoPath = getIntent().getStringExtra(UGCKitConstants.VIDEO_PATH);
      mUGCKitVideoEdit = (UGCKitVideoEdit) findViewById(R.id.video_edit);
      if (!TextUtils.isEmpty(mVideoPath)) {
          mUGCKitVideoEdit.setVideoPath(mVideoPath);
      }
      // Initialize the player
      mUGCKitVideoEdit.initPlayer();
      mUGCKitVideoEdit.setOnVideoEditListener(new IVideoEditKit.OnEditListener() {
          @Override
          public void onEditCompleted(UGCKitResult ugcKitResult) {
              // Video editing completed.
          }
           @Override
          public void onEditCanceled() {
              
          }
      });
      }
      @Override
      protected void onResume() {
      super.onResume();
      // `UGCKit` takes over the lifecycle of the editing view. For details, see `Github/Demo`.
      mUGCKitVideoEdit.start();
      }
      

    The UI view looks like this:

    Module Description

    See the documents below for a detailed description of different UGSV modules.

    FAQs

    Can I use AndroidX?

    Since most of our users use Android Support Library, UGCKit is based on Android Support Library currently. However, to meet the demand of users using AndroidX, we provide a scheme for you to migrate UGCKit to AndroidX.

    The directions below use the UGSV SDK as an example, and the UGCKit module is used in the demo.

    1. Prerequisites:
      • Update Android Studio to v3.2 or above.
      • Update the Android Gradle plugin to v4.6 or above.
      • Update compileSdkVersion to 28 or above.
      • Update buildToolsVersion to 28.0.2 or above.
    2. Migrate to AndroidX:
      1. Import the project to Android Studio and select Refactor > Migrate to AndroidX.
      2. Click Migrate to migrate the project to AndroidX.

    What should I do if a UGCKit build version error occurs?

    • Error message:
      ERROR: Unable to find method 'org.gradle.api.tasks.compile.CompileOptions.setBootClasspath(Ljava/lang/String;)V'.
      Possible causes for this unexpected error include:
      

    -Cause: The problem occurs because the version of the Gradle plugin used for UGCKit is v2.2.3, but that of Gradle is v3.3.

    What should I do if the following error occurs when I build UGCKit?

    • Error message:
    • Cause: The problem occurs because renderscript-v8.jar is missing from ugckit module. renderscript-v8.jar is responsible for image processing, blurring, and rendering.
    • Solution: Create a libs folder under ugckit module and add renderscript-v8.jar, which you can find in \sdk\build-tools\, to the folder.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support