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博客,谢谢!

相关推荐
Mr Lee_几秒前
android 配置鼠标右键快捷对apk进行反编译
android
顾北川_野36 分钟前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
&岁月不待人&1 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
Winston Wood3 小时前
Android Parcelable和Serializable的区别与联系
android·序列化
清风徐来辽3 小时前
Android 项目模型配置管理
android
帅得不敢出门3 小时前
Gradle命令编译Android Studio工程项目并签名
android·ide·android studio·gradlew
problc4 小时前
Flutter中文字体设置指南:打造个性化的应用体验
android·javascript·flutter
帅得不敢出门14 小时前
安卓设备adb执行AT指令控制电话卡
android·adb·sim卡·at指令·电话卡
我又来搬代码了16 小时前
【Android】使用productFlavors构建多个变体
android
德育处主任17 小时前
Mac和安卓手机互传文件(ADB)
android·macos