Android 10 平台 (api29) 的 Vimeo 网络库崩溃

Vimeo Networking Library Crash for Android 10 platform (api29)

我使用 vimeo 网络库实现了 vimeo 网络(https://github.com/vimeo/vimeo-networking-java), exoplayer and explained in this post

现在的问题是当我检查 API 30 时出现错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emergingit.emergingstudy/com.emergingit.emergingstudy.activities.course.ExoPlayerActivity}: java.lang.IllegalStateException: Unable to extract the trust manager on Android10Platform, sslSocketFactory is class com.android.org.conscrypt.OpenSSLSocketFactoryImpl

Caused by: java.lang.IllegalStateException: Unable to extract the trust manager on Android10Platform, sslSocketFactory is class com.android.org.conscrypt.OpenSSLSocketFactoryImpl

表示

上的错误
VimeoClient.initialize(configuration);

这里讨论了哪个问题:https://github.com/square/okhttp/issues/5878,问题讨论:

I assume you are calling the deprecated form of sslSocketFactory, which is broken on newer Android versions. https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/OkHttpClient.kt#L719

/**
 * Sets the socket factory used to secure HTTPS connections. If unset, the system default will
 * be used.
 *
 * @deprecated [SSLSocketFactory] does not expose its [X509TrustManager], which is a field that
 *     OkHttp needs to build a clean certificate chain. This method instead must use reflection
 *     to extract the trust manager. Applications should prefer to call
 *     `sslSocketFactory(SSLSocketFactory, X509TrustManager)`, which avoids such reflection.
 */
@Deprecated(
    message = "Use the sslSocketFactory overload that accepts a X509TrustManager.",
    level = DeprecationLevel.ERROR
)
fun sslSocketFactory(sslSocketFactory: SSLSocketFactory) = apply {
  if (sslSocketFactory != this.sslSocketFactoryOrNull) {
    this.routeDatabase = null
  }

  this.sslSocketFactoryOrNull = sslSocketFactory
  this.certificateChainCleaner = Platform.get().buildCertificateChainCleaner(sslSocketFactory)
}

使用当前版本的“1.1.3”网络库是否有任何解决方案,或者我必须等到库更新?

我主要使用 Vimeo-Networking 库 (v1.1.3) 解决了 Android 10 平台上 Vimeo 视频流的问题。 因为主要问题出在 OkHttpClient 构建方法中

builder.sslSocketFactory(sSLSocketFactory);
//RetrofitClientBuilder.java#186    

其中 1 个参数版本被描述,我需要像这样将 X509TTrustManager 作为第二个参数传递:

builder.sslSocketFactory(sSLSocketFactory, new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        });

所以它解决了我的问题,直到这个更新出现在这个库的稳定版本中。下面描述了我是如何实现的:

  1. Error was showed on VimeoClient.initialize(accessToken); 行,我搜索了这个错误,发现是因为这个OkHttpClient 方法弃用(带 1 个参数)。

  2. 所以我进入 VimeoClient.java 并在其构造函数中看到 private Retrofit createRetrofit() 被调用并在其中 private OkHttpClient createOkHttpClient() 被调用。并且创建了 public class RetrofitClientBuilder 对象,它的问题方法是 public OkHttpClient build()其中:if (sSLSocketFactory != null) { builder.sslSocketFactory(sSLSocketFactory); } 调用了这个已弃用的方法。

  3. 所以我只需要创建 2 个新的 java 文件,名称为 VimeoClientUpdated.javaRetrofitClientBuilderUpdated.java 这只是 VimeoClient.javaRetrofitClientBuilder.java 的重命名副本,现在我的 RetrofitClientBuilderUpdated.java 没有弃用的方法,而是正确的方法,VimeoClientUpdated.java 正在调用 RetrofitClientBuilderUpdated.java (由于最终的 class 我无法扩展,甚至无法将 class 包装成该解决方案的网关,我不得不面对无法更改的私有方法包装纸 class)

  4. 最后,我没有调用 VimeoClient.initialize(accessToken),而是调用了我新创建的 VimeoClientUpdated(accessToken),它在 Android 10 上播放 Vimeo 视频时不会崩溃。

建议我是否有更好的方法来实现我的目的(解决从 android activity 调用已弃用的库方法)。 干杯..!!