一、引入库文件
implementation 'com.github.bumptech.glide:glide:4.5.0'annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'//https图片处理implementation "com.github.bumptech.glide:okhttp3-integration:4.5.0"
二、重写AppGlideModule
/** * Created by wangfengkai on 2019/2/18. */@GlideModulepublic class OkHttpAppGlideModule extends AppGlideModule {@Overridepublic void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { OkHttpClient client = UnsafeOkHttpClient.getUnsafeOkHttpClient(); registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client)); } }
必须加上 @GlideModule注解,其中UnsafeOkHttpClient类的代码如下:
public class UnsafeOkHttpClient {public static OkHttpClient getUnsafeOkHttpClient() { try { // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { @Override public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.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(); OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]); builder.hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); builder.connectTimeout(20, TimeUnit.SECONDS); builder.readTimeout(20,TimeUnit.SECONDS); OkHttpClient okHttpClient = builder.build(); return okHttpClient; } catch (Exception e) { throw new RuntimeException(e); } } }
三、配置glide加载https所需要的GlideModule
<meta-data android:name="xxx.xxx.xxx.OkHttpAppGlideModule" android:value="AppGlideModule"/>
四、混淆
-keep public class * implements com.bumptech.glide.module.GlideModule-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** { **[] $VALUES; public *; } -keep public class * extends com.bumptech.glide.module.AppGlideModule-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl { *; }
作者:flywfk
链接:https://www.jianshu.com/p/94414df7b407
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦