无法在 Android Studio 上设置 Twilio SDK

问题描述

我正在尝试使用

将 TWILIO 集成到我的应用程序中
implementation group: "com.twilio.sdk",name: "twilio",version: "8.7.0"

但出现以下错误

More than one file was found with OS independent path 'meta-inf/DEPENDENCIES'.

然后在搜索这个错误后我发现它可以通过添加解决

packagingOptions {
    exclude 'meta-inf/DEPENDENCIES'
}

添加应用程序后,现在构建成功。运行应用程序后,我收到以下错误

Caused by: j.a.a.b.b: java.lang.classCastException: The application has specified that a custom LogFactory implementation should be used but Class 'org.apache.commons.logging.impl.LogFactoryImpl' cannot be converted to 'org.apache.commons.logging.LogFactory'. Please check the custom implementation

然后为了解决这个错误添加

-keep class org.apache.** { *; }

到proguard-rules文件

再次运行代码后出现以下错误

java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; 

in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; 

or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar!classes3.dex)

我无法找到解决方案。有人可以帮我解决这些错误以将 TWILIO 添加到我的项目中吗?


编辑 - 1

代码添加打印后,我发现导致上述错误代码行是

Token token = Token.creator().create();

我正在尝试实现以下代码

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Token;

public class Example {
 // Find your Account Sid and Token at twilio.com/console
 // and set the environment variables. See http://twil.io/secure
 public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
 public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");

 public static void main(String[] args) {
    Twilio.init(ACCOUNT_SID,AUTH_TOKEN);
    Token token = Token.creator().create();

    System.out.println(token.getUsername());
 }
}

来自

https://www.twilio.com/docs/stun-turn

解决方法

语音 Android 3.2.0+

# Twilio Programmable Voice
-keep class com.twilio.** { *; }
-keep class tvo.webrtc.** { *; }
-dontwarn tvo.webrtc.**
-keep class com.twilio.voice.** { *; }
-keepattributes InnerClasses

这些规则确保 ProGuard 不会删除可编程语音库。

https://www.twilio.com/docs/voice/voip-sdk/android