Android 12 Unable to extract the trust manager on Android10Platform

java.lang.IllegalStateException: Unable to extract the trust manager on Android10Platform, sslSocketFactory is class com.android.org.conscrypt.OpenSSLSocketFactoryImpl
2024-04-01 17:56:38.044 23697-25362 System.err              com.xxx.           W  	at okhttp3.OkHttpClient$Builder.sslSocketFactory(OkHttpClient.kt:810)
2024-04-01 17:56:38.044 23697-25362 System.err               com.xxx.          W  	at  com.xxx..http.HttpHelper.getClient(HttpHelper.java:73)
2024-04-01 17:56:38.044 23697-25362 System.err               com.xxx.          W  	at  com.xxx..http.HttpHelper.doRequest(HttpHelper.java:110)

1.最近由于升级Android Studio以及 sdk版本 导致上传报告使用okhttp报错如上面代码所示,于是我查看源码分析一波发现问题出现在 在 OkHttpClient 构建方法中 于是尝试修改代码

2.修改之前代码

    /**
     * get okhttpclient
     * @param timeOutMs         time out ms
     * @return                  OkHttpClient
     */
    public static OkHttpClient getClient(long timeOutMs) {
        OkHttpClient.Builder mBuilder = new OkHttpClient.Builder();
        mBuilder.sslSocketFactory(createSSLSocketFactory());
        mBuilder.hostnameVerifier(new TrustAllHostnameVerifier());
        mBuilder.readTimeout(timeOutMs, TimeUnit.MILLISECONDS);
        mBuilder.connectTimeout(timeOutMs, TimeUnit.MILLISECONDS);
        mBuilder.writeTimeout(timeOutMs, TimeUnit.MILLISECONDS);

        OkHttpClient client = mBuilder.build();
        return client;
    }

3.修改后代码

 /**
     * get okhttpclient
     *
     * @param timeOutMs time out ms
     * @return OkHttpClient
     */
    public static OkHttpClient getClient(long timeOutMs) {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        }

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

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

//            okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder();
//            builder.sslSocketFactory(sslSocketFactory);
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.sslSocketFactory(sslSocketFactory, new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            });
            builder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            OkHttpClient okHttpClient = builder.build();
            builder.readTimeout(timeOutMs, TimeUnit.MILLISECONDS);
            builder.connectTimeout(timeOutMs, TimeUnit.MILLISECONDS);
            builder.writeTimeout(timeOutMs, TimeUnit.MILLISECONDS);
            return okHttpClient;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        }
    }

4、到这里基本结束,只是简单记录下,后续有其他同学遇到这个坑这样解决就行。

感谢

runtime error - Vimeo Networking Library Crash for Android 10 platform (api29) - Stack Overflow

转载请注明出处Android 12 Unable to extract the trust manager on Android10Platform-CSDN博客,谢谢!

相关推荐
拭心10 小时前
Google 提供的 Android 端上大模型组件:MediaPipe LLM 介绍
android
带电的小王13 小时前
WhisperKit: Android 端测试 Whisper -- Android手机(Qualcomm GPU)部署音频大模型
android·智能手机·whisper·qualcomm
梦想平凡13 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
元争栈道13 小时前
webview和H5来实现的android短视频(短剧)音视频播放依赖控件
android·音视频
阿甘知识库14 小时前
宝塔面板跨服务器数据同步教程:双机备份零停机
android·运维·服务器·备份·同步·宝塔面板·建站
元争栈道15 小时前
webview+H5来实现的android短视频(短剧)音视频播放依赖控件资源
android·音视频
MuYe15 小时前
Android Hook - 动态加载so库
android
居居飒16 小时前
Android学习(四)-Kotlin编程语言-for循环
android·学习·kotlin
Henry_He18 小时前
桌面列表小部件不能点击的问题分析
android
工程师老罗19 小时前
Android笔试面试题AI答之Android基础(1)
android