Dynamically load SO files with the Android SDK
The Android software development kit (SDK) lets you dynamically load SO files to reduce the size of your installation package. This topic describes how to dynamically load SO files when you integrate the SDK.
Background information
Traditional installation packages that include SO files are often large and inconvenient for users to download. Dynamic loading solves this issue by not packaging SO files into the APK. Instead, the SO files are downloaded in the background and loaded into the application after it is installed and launched. The Alibaba Cloud Real-Time Communication (RTC) Android SDK offers a separate package for this dynamic loading solution. In this package, the SO files are separated from the AAR file, allowing developers to package the AAR file into the APK and host the larger SO files on a cloud server for download. This approach significantly reduces the installation package size and improves the user's download experience.
Notes
- Dynamic loading is not always successful. If the SO files fail to load, the application will crash. In the event of a failure, you should implement a fallback mechanism or attempt to reload the files. Do not allow the application to start until the files have been successfully loaded.
- To improve the user experience, download the SO files when the application is idle.
Integrate the SDK
Method 1: Maven integration
- Add the Maven repository address to the build.gradle file in your project's root directory.
allprojects { repositories { google() jcenter() // Add the Maven repository for RTC maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } } } - In the app/build.gradle file of your project, add the following lines.
dependencies { ... // The RTC SDK dependency implementation 'com.aliyun.rtc:AliRTC:1.17.41.2102238' }Note The Maven dependency version shown here is for reference only. For the latest Maven dependency, see Lite SDK. - Download and decompress the SO files. For the download link, see Lite SDK.
- Add the following code to load the SO files.
System.load("absolute_path_to_so_file");Note- The decompressed package contains both AAR and SO files. Select the required SO file based on your needs.
- You cannot use the
System.loadLibrary()System.loadLibrary()method to load the .so file. This method takes the library name as its parameter and searches only in paths specified by thejava.library.pathJVM property. However, permission restrictions prevent you from saving the downloaded .so file to a path specified injava.library.path. Instead, you must use the System.load() method, which takes the absolute path of the .so file as its parameter.
- Add the following code to the app/src/main/AndroidManifest.xml file to declare the required device permissions.
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
Method 2: Manual integration
- Download and decompress the Android SDK. For the download link, see Lite SDK.
- Copy the `AliRTCSdk.aar` file to the `libs` folder in your app module.
- Add the following code to load the SO files.
System.load("absolute_path_to_so_file");Note- The decompressed package contains both AAR and SO files. Select the required SO file based on your needs.
- You cannot use the
System.loadLibrary()System.loadLibrary()method to load the .so file. This method takes the library name as its parameter and searches only in paths specified by thejava.library.pathJVM property. However, permission restrictions prevent you from saving the downloaded .so file to a path specified injava.library.path. Instead, you must use the System.load() method, which takes the absolute path of the .so file as its parameter.
- Add the following code to the app/src/main/AndroidManifest.xml file to declare the required device permissions.
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
Related demo
For sample code that demonstrates how to dynamically load SO files with the Android SDK, see Lite SDK.