AppAllCLassLoaderList = new ArrayList<>();
110 | // private Object mProxyInstance;
111 | //
112 | //
113 | // @Override
114 | // public Object invoke(Object proxy, Method method, Object[] args) {
115 | //// CLogUtils.e("调用方法名字 "+method.getName());
116 | //// HttpLoggingInterceptor httpLoggingInterceptor =new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
117 | //// @Override
118 | //// public void log(String message) {
119 | ////
120 | //// }
121 | //// });
122 | // CLogUtils.NetLogger((String) args[0]);
123 | //
124 | // return null;
125 | // }
126 | //
127 | // @Override
128 | // public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) {
129 | // try {
130 | // shared = new XSharedPreferences("Xposed.Okhttp.Cat", "config");
131 | // shared.reload();
132 | //
133 | // InvokPackage = shared.getString("APP_INFO", "");
134 | //
135 | // //MODEL = shared.getString("MODEL", "1");
136 | //
137 | //// if (MODEL.equals("4")) {
138 | //// isShowStacktTrash = true;
139 | //// }
140 | //
141 | // //先重启 选择 好 要进行Hook的 app
142 | // if (!"".equals(InvokPackage) && lpparam.packageName.equals(InvokPackage)) {
143 | // HookLoadClass();
144 | // HookAttach();
145 | // }
146 | // } catch (Throwable e) {
147 | // e.printStackTrace();
148 | // CLogUtils.e("Test Exception " + e.toString());
149 | // }
150 | //
151 | // }
152 | //
153 | // private void HookLoadClass() {
154 | // XposedBridge.hookAllMethods(ClassLoader.class,
155 | // "loadClass",
156 | // new XC_MethodHook() {
157 | // @Override
158 | // protected void afterHookedMethod(MethodHookParam param) throws Throwable {
159 | // super.afterHookedMethod(param);
160 | // if (param == null) {
161 | // return;
162 | // }
163 | // Class cls = (Class) param.getResult();
164 | // //在这块 做个判断 是否 存在 如果 不存在 在保存 否则 影响效率
165 | // //如果 不存在
166 | // if (cls == null) {
167 | // return;
168 | // }
169 | //
170 | // isNeedAddClassloader(cls.getClassLoader());
171 | // }
172 | // });
173 | // }
174 | //
175 | // /**
176 | // * 对 每个 classloader进行判断
177 | // *
178 | // * @param classLoader 需要 的classloader
179 | // * @return
180 | // */
181 | // private void isNeedAddClassloader(ClassLoader classLoader) {
182 | //
183 | // //先过滤掉系统类 ,如果是系统预装的 类也 不要
184 | //// if (classLoader.getClass().getName().equals("java.lang.BootClassLoader")) {
185 | //// return;
186 | //// }
187 | // for (ClassLoader loader : AppAllCLassLoaderList) {
188 | // if (loader.hashCode() == classLoader.hashCode()) {
189 | // return;
190 | // }
191 | // }
192 | // //CLogUtils.e("加入的classloader名字 " + classLoader.getClass().getName());
193 | // AppAllCLassLoaderList.add(classLoader);
194 | // }
195 | //
196 | // /**
197 | // * 手动申请SD卡权限
198 | // */
199 | // private void initPermission() {
200 | // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
201 | // if (mOtherContext.checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE")
202 | // != PackageManager.PERMISSION_GRANTED) {
203 | // XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
204 | // @Override
205 | // protected void afterHookedMethod(MethodHookParam param) {
206 | // final Activity firstActivity = (Activity) param.thisObject;
207 | //
208 | // ActivityCompat.requestPermissions(firstActivity, new String[]{
209 | // "android.permission.WRITE_EXTERNAL_STORAGE",
210 | // "android.permission.ACCESS_COARSE_LOCATION",
211 | // "android.permission.ACCESS_FINE_LOCATION",
212 | // "android.permission.READ_PHONE_STATE"}, 0);
213 | // }
214 | // });
215 | // }
216 | // }
217 | // }
218 | //
219 | // /**
220 | // * 遍历当前进程的Classloader 尝试进行获取指定类
221 | // *
222 | // * @param className
223 | // * @return
224 | // */
225 | // private Class getClass(String className) {
226 | // Class> aClass = null;
227 | // try {
228 | // try {
229 | // aClass = Class.forName(className);
230 | // } catch (ClassNotFoundException classNotFoundE) {
231 | //
232 | // try {
233 | // aClass = Class.forName(className, false, mLoader);
234 | // } catch (ClassNotFoundException e) {
235 | // e.printStackTrace();
236 | // }
237 | // if (aClass != null) {
238 | // return aClass;
239 | // }
240 | // try {
241 | // for (ClassLoader classLoader : AppAllCLassLoaderList) {
242 | // try {
243 | // aClass = Class.forName(className, false, classLoader);
244 | // } catch (Throwable e) {
245 | // continue;
246 | // }
247 | // if (aClass != null) {
248 | // return aClass;
249 | // }
250 | // }
251 | // } catch (Throwable e) {
252 | // e.printStackTrace();
253 | // }
254 | // }
255 | //
256 | // return aClass;
257 | // } catch (Throwable e) {
258 | //
259 | // }
260 | // return null;
261 | // }
262 | //
263 | // /**
264 | // * hook Attach方法
265 | // */
266 | // private void HookAttach() {
267 | //
268 | ////
269 | //// Class OkHttpClientClass = getClass("com.squareup.okhttp.OkHttpClient");
270 | ////
271 | //// if(OkHttpClientClass!=null){
272 | //// CLogUtils.e("成功拿到 系统okHttpClient");
273 | //// }
274 | //
275 | //
276 | // XposedHelpers.findAndHookMethod(Application.class,
277 | // "attach",
278 | // Context.class,
279 | // new XC_MethodHook() {
280 | // @Override
281 | // protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
282 | // super.beforeHookedMethod(param);
283 | // mOtherContext = (Context) param.args[0];
284 | // mLoader = mOtherContext.getClassLoader();
285 | // CLogUtils.e("拿到Classloader ");
286 | // }
287 | //
288 | // @Override
289 | // protected void afterHookedMethod(MethodHookParam param) throws Throwable {
290 | // super.afterHookedMethod(param);
291 | // String processName = getCurProcessName(mOtherContext);
292 | // if (processName != null && processName.equals(mOtherContext.getPackageName())) {
293 | // HookOKClient();
294 | // }
295 | // }
296 | // });
297 | //
298 | //
299 | //// XposedHelpers.findAndHookMethod(Application.class,
300 | //// "attachBaseContext",
301 | //// Context.class,
302 | //// new XC_MethodHook() {
303 | //// @Override
304 | //// protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
305 | //// super.beforeHookedMethod(param);
306 | ////
307 | //// }
308 | ////
309 | //// @Override
310 | //// protected void afterHookedMethod(MethodHookParam param) throws Throwable {
311 | //// super.afterHookedMethod(param);
312 | ////
313 | //// }
314 | //// });
315 | //
316 | //// XposedHelpers.findAndHookMethod(Application.class, "onCreate",
317 | //// new XC_MethodHook() {
318 | //// protected void afterHookedMethod(MethodHookParam param) throws Throwable {
319 | //// super.afterHookedMethod(param);
320 | ////
321 | //// }
322 | //// });
323 | // }
324 | //
325 | // /**
326 | // * 获得当前进程的名字
327 | // *
328 | // * @return 进程号
329 | // */
330 | // private String getCurProcessName(Context context) {
331 | //
332 | // int pid = android.os.Process.myPid();
333 | //
334 | // ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
335 | //
336 | // for (ActivityManager.RunningAppProcessInfo appProcess : Objects.requireNonNull(activityManager)
337 | // .getRunningAppProcesses()) {
338 | //
339 | // if (appProcess.pid == pid) {
340 | // return appProcess.processName;
341 | // }
342 | // }
343 | // return null;
344 | // }
345 | //
346 | // /**
347 | // * 判断 OkHttp是否存在 混淆 这步骤
348 | // */
349 | // private synchronized void HookOKClient() {
350 | // try {
351 | // if (OkHttpClient == null) {
352 | // OkHttpClient = Class.forName("okhttp3.OkHttpClient", false, mLoader);
353 | // }
354 | // } catch (ClassNotFoundException e) {
355 | // CLogUtils.e("出现异常 发现对方没有使用OkHttp或者被混淆,开始尝试自动获取路径 ");
356 | // HookAndInitProguardClass();
357 | // return;
358 | // }
359 | // try {
360 | // if (OkHttpBuilder == null) {
361 | // OkHttpBuilder = Class.forName("okhttp3.OkHttpClient$Builder", false, mLoader);
362 | // }
363 | // } catch (ClassNotFoundException e) {
364 | // CLogUtils.e("出现异常 发现对方没有使用OkHttp或者被混淆,开始尝试自动获取路径 ");
365 | // HookAndInitProguardClass();
366 | // return;
367 | // }
368 | // if (isExactness()) {
369 | // CLogUtils.e("okHttp本身未混淆");
370 | // HookClientAndBuilderConstructor();
371 | // } else {
372 | // HookAndInitProguardClass();
373 | // }
374 | // }
375 | //
376 | // private synchronized void HookAndInitProguardClass() {
377 | // //放在子线程去执行,防止卡死
378 | // //先拿到 app里面全部的
379 | //
380 | // getAllClassName();
381 | //
382 | // initAllClass();
383 | //
384 | //
385 | // //第一步 先开始 拿到 OkHttp 里面的 类 如Client 和 Builder
386 | // getClientClass();
387 | // getBuilder();
388 | //
389 | // if (OkHttpBuilder != null && OkHttpClient != null) {
390 | // CLogUtils.e("使用了okHttp 开始添加拦截器");
391 | // HookClientAndBuilderConstructor();
392 | // } else {
393 | // CLogUtils.e("对方App可能没有使用okHttp 开始Hook底层方法");
394 | // //可能对方没有使用OkHttp
395 | // HookGetOutPushStream();
396 | // }
397 | //
398 | //
399 | // }
400 | //
401 | // /**
402 | // * 添加IO目录
403 | // *
404 | // * 这个方法 主要是 有的 io目录 被混淆了 导致后面的 拦截器方法 被 干掉了
405 | // * 需要先 找到 okio的 路径
406 | // * 通过 FormBodyz这个方法 进行 参数1 拿到 路径
407 | // * public void writeTo(BufferedSink sink) throws IOException
408 | // *
409 | // * 比如 okio 目录被混淆成 abc这样的路径 我没做识别
410 | // * 没有加到 mClassList 这个里面也就导致后面的程序 会 找不到 抛异常
411 | // * 为了保险 在此添加一次 跟 Okio有关的类
412 | // */
413 | // private synchronized void getIOPath() {
414 | //
415 | // for (Class mClass : mClassList) {
416 | // int ListCount = 0;
417 | // int staticCount = 0;
418 | // if (Modifier.isFinal(mClass.getModifiers())) {
419 | // Field[] declaredFields = mClass.getDeclaredFields();
420 | // if (declaredFields.length == 3) {
421 | // for (Field field : declaredFields) {
422 | // if (field.getType().getName().equals(List.class.getName()) &&
423 | // Modifier.isFinal(field.getModifiers()) &&
424 | // Modifier.isPrivate(field.getModifiers())
425 | // ) {
426 | // ListCount++;
427 | // }
428 | // if (Modifier.isFinal(field.getModifiers())
429 | // && Modifier.isPrivate(field.getModifiers()) &&
430 | // Modifier.isStatic(field.getModifiers())
431 | // ) {
432 | // staticCount++;
433 | // }
434 | // }
435 | // if (ListCount == 2 && staticCount == 1) {
436 | // String ioPath = getWriterToParameterTypeString(mClass);
437 | // if (ioPath.equals("okio")) {
438 | // CLogUtils.e("IO目录名字 是okio 直接return ");
439 | // return;
440 | // } else {
441 | // CLogUtils.e("IO目录名字 " + ioPath);
442 | // //将 OKIO 目录 加进去
443 | // for (String string : AllClassNameList) {
444 | // if (string.startsWith(ioPath)) {
445 | // Class> aClass = null;
446 | // try {
447 | // aClass = getClass(string);
448 | // } catch (Throwable e) {
449 | // e.printStackTrace();
450 | // }
451 | // if (aClass != null) {
452 | // mClassList.add(aClass);
453 | // }
454 | // }
455 | // }
456 | // }
457 | // }
458 | // }
459 | // }
460 | // }
461 | // }
462 | //
463 | // private String getWriterToParameterTypeString(Class mClass) {
464 | // Method[] declaredMethods = mClass.getDeclaredMethods();
465 | // for (Method method : declaredMethods) {
466 | // if (method.getReturnType().getName().equals(void.class.getName())
467 | // && Modifier.isPublic(method.getModifiers())
468 | // && method.getParameterTypes().length == 1
469 | // ) {
470 | // //String str="jjj.kkk.yyy.uuu";
471 | // String parameterType = method.getParameterTypes()[0].getName();
472 | // String[] split = parameterType.split("\\.");
473 | // StringBuilder stringBuffer = new StringBuilder();
474 | // for (int i = 0; i < split.length - 1; i++) {
475 | // stringBuffer.append(split[i]).append(".");
476 | // }
477 | // return stringBuffer.toString();
478 | // }
479 | // }
480 | // return "";
481 | // }
482 | //
483 | // /**
484 | // * 初始化 需要的 class的 方法
485 | // */
486 | // private void initAllClass() {
487 | // mClassList.clear();
488 | //
489 | // try {
490 | // CLogUtils.e("需要初始化Class的个数是 " + AllClassNameList.size());
491 | //
492 | // Class> MClass = null;
493 | //
494 | // for (int i = 0; i < AllClassNameList.size(); i++) {
495 | //
496 | // MClass = getClass(AllClassNameList.get(i));
497 | // if (MClass != null) {
498 | // //CLogUtils.e("添加成功 "+MClass.getName());
499 | // mClassList.add(MClass);
500 | // }
501 | //
502 | // }
503 | //
504 | // CLogUtils.e("初始化全部类的个数 " + mClassList.size());
505 | // } catch (Throwable e) {
506 | // CLogUtils.e("initAllClass error " + e.toString());
507 | // }
508 | // }
509 | //
510 | // /**
511 | // * 获取 ClientCLass的方法
512 | // */
513 | // private void getClientClass() {
514 | // if (mClassList.size() == 0) {
515 | // CLogUtils.e("全部的 集合 mClassList 的个数 为 0 ");
516 | // return;
517 | // }
518 | // CLogUtils.e("开始 查找 ClientClass");
519 | // try {
520 | // for (Class mClient : mClassList) {
521 | // //判断 集合 个数 先拿到 四个集合 可以 拿到 Client
522 | // if (isClient(mClient)) {
523 | // OkHttpClient = mClient;
524 | // return;
525 | // }
526 | // }
527 | // } catch (Throwable e) {
528 | // e.printStackTrace();
529 | // }
530 | // CLogUtils.e("没找到 client ");
531 | // }
532 | //
533 | // /**
534 | // * 混淆 以后 获取 集合并添加 拦截器的方法
535 | // */
536 | // private void getBuilder() {
537 | // if (OkHttpClient != null) {
538 | // //开始查找 build
539 | // for (Class builder : mClassList) {
540 | // if (isBuilder(builder)) {
541 | // OkHttpBuilder = builder;
542 | // }
543 | // }
544 | // }
545 | // }
546 | //
547 | // /**
548 | // * 当 OkHttpBuilderClass 和OkHttpClient不为Null
549 | // * Hook Client和 Builder 的构造
550 | // *
551 | // * 如果两个都没找到可能没有使用okHttp
552 | // * 尝试Hook 底层函数
553 | // */
554 | // private void HookClientAndBuilderConstructor() {
555 | //
556 | // try {
557 | // interceptorClass = getInterceptorClass();
558 | // if (interceptorClass == null) {
559 | // CLogUtils.e("HookClientAndBuilderConstructor 出现问题没有拿到解释器类 ");
560 | // return;
561 | // }
562 | // } catch (Throwable e) {
563 | // CLogUtils.e("getInterceptorClass error " + e.toString());
564 | // }
565 | //
566 | //
567 | // if (OkHttpClient != null) {
568 | // try {
569 | // XposedHelpers.findAndHookConstructor(
570 | // OkHttpClient,
571 | // OkHttpBuilder,
572 | // new XC_MethodHook() {
573 | //// @Override
574 | //// protected void afterHookedMethod(MethodHookParam param) throws Throwable {
575 | //// super.afterHookedMethod(param);
576 | //
577 | //// }
578 | //
579 | // @Override
580 | // protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
581 | // super.beforeHookedMethod(param);
582 | // CLogUtils.e("Hook 到 构造函数 OkHttpClient");
583 | // AddInterceptors2(param);
584 | // }
585 | // });
586 | // } catch (Throwable e) {
587 | // CLogUtils.e("findAndHookConstructor OkHttpClient error " + e.toString());
588 | // e.printStackTrace();
589 | // }
590 | // }
591 | //
592 | // }
593 | //
594 | // private Method getAddInterceptorMethod(Class> okHttpBuilder) {
595 | // Method[] declaredMethods = okHttpBuilder.getDeclaredMethods();
596 | //
597 | // Class interceptorClass = getInterceptorClass();
598 | // if (interceptorClass != null) {
599 | // for (Method method : declaredMethods) {
600 | // if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].getName().equals(interceptorClass.getName())) {
601 | // method.setAccessible(true);
602 | // return method;
603 | // }
604 | // }
605 | // }
606 | // CLogUtils.e("没找到AddInterceptorMethod");
607 | // return null;
608 | // }
609 | //
610 | // private boolean isBuilder(@NonNull Class ccc) {
611 | //
612 | // try {
613 | // int ListTypeCount = 0;
614 | // int FinalTypeCount = 0;
615 | // Field[] fields = ccc.getDeclaredFields();
616 | // for (Field field : fields) {
617 | // String type = field.getType().getName();
618 | // //四个 集合
619 | // if (type.contains(List.class.getName())) {
620 | // ListTypeCount++;
621 | // }
622 | // //2 个 为 final类型
623 | // if (type.contains(List.class.getName()) && Modifier.isFinal(field.getModifiers())) {
624 | // FinalTypeCount++;
625 | // }
626 | // }
627 | // //四个 List 两个 2 final 并且 包含父类名字
628 | // if (ListTypeCount == 4 && FinalTypeCount == 2 && ccc.getName().contains(OkHttpClient.getName())) {
629 | // CLogUtils.e(" 找到 Builer " + ccc.getName());
630 | // return true;
631 | // }
632 | // } catch (Throwable e) {
633 | // e.printStackTrace();
634 | // }
635 | // return false;
636 | // }
637 | //
638 | // private boolean isClient(@NonNull Class> mClass) {
639 | // try {
640 | // int typeCount = 0;
641 | // int StaticCount = 0;
642 | //
643 | // //getDeclaredFields 是个 获取 全部的
644 | // Field[] fields = mClass.getDeclaredFields();
645 | //
646 | // for (Field field : fields) {
647 | // field.setAccessible(true);
648 | // String type = field.getType().getName();
649 | //
650 | // //CLogUtils.e(" 复合 规则 该 Field是 " + type +"是否包含 "+ type.contains(Key.ListType) +" 是否是 final类型 "+Modifier.isFinal(field.getModifiers()));
651 | //
652 | // //四个 集合 四个final 特征
653 | // if (type.contains(List.class.getName()) && Modifier.isFinal(field.getModifiers())) {
654 | // //CLogUtils.e(" 复合 规则 该 Field是 " + field.getName() + " ");
655 | // typeCount++;
656 | // }
657 | // if (type.contains(List.class.getName()) && Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
658 | // //CLogUtils.e(" 复合 规则 该 Field是 " + field.getName() + " ");
659 | // StaticCount++;
660 | // }
661 | // }
662 | // //CLogUtils.e("field 符合标准 个数 是 " + typeCount);
663 | //
664 | // if (StaticCount >= 2 && typeCount == 6 && mClass.getInterfaces().length >= 1) {
665 | // CLogUtils.e("找到OkHttpClient 该类的名字是 " + mClass.getName());
666 | // return true;
667 | // }
668 | // } catch (Throwable e) {
669 | // CLogUtils.e("isClient error " + e.toString());
670 | // e.printStackTrace();
671 | // }
672 | // // mFieldArrayList.clear();
673 | // return false;
674 | // }
675 | //
676 | // private void getDexFileClassName(DexFile dexFile) {
677 | // if (dexFile == null) {
678 | // return;
679 | // }
680 | //
681 | // //获取df中的元素 这里包含了所有可执行的类名 该类名包含了包名+类名的方式
682 | // Enumeration enumeration = dexFile.entries();
683 | // while (enumeration.hasMoreElements()) {//遍历
684 | // String className = enumeration.nextElement();
685 | // //添加过滤信息
686 | // if (className.contains("okhttp") || className.contains("okio")
687 | // ) {
688 | // AllClassNameList.add(className);
689 | // }
690 | // }
691 | // }
692 | //
693 | // /**
694 | // * 核心方法
695 | // *
696 | // * 尝试添加拦截器
697 | // *
698 | // * @param param
699 | // */
700 | // private synchronized void AddInterceptors2(XC_MethodHook.MethodHookParam param) {
701 | // if (interceptorClass == null) {
702 | // CLogUtils.e("interceptorClass == null");
703 | // return;
704 | // }
705 | // try {
706 | //// if (flag) {
707 | // //找到添加拦截器的方法
708 | // Object httpLoggingInterceptor = getHttpLoggingInterceptorClass();
709 | //
710 | // if (httpLoggingInterceptor != null) {
711 | // CLogUtils.e("拿到的拦截器,动态代理实现,开始添加名字是 " + httpLoggingInterceptor.getClass().getName());
712 | // Hook.httpLoggingInterceptor = httpLoggingInterceptor;
713 | // //添加拦截器到集合里面
714 | // if (AddInterceptorForList(param, httpLoggingInterceptor)) {
715 | // //将状态修改为False
716 | // flag = false;
717 | // //CLogUtils.e("添加拦截器完毕");
718 | // }
719 | // } else {
720 | // CLogUtils.e("添加拦截器失败 getHttpLoggingInterceptorClass==null");
721 | // }
722 | //// }
723 | // } catch (Throwable e) {
724 | // CLogUtils.e("AddInterceptors2 异常 " + e.toString());
725 | // }
726 | // }
727 | //
728 | //
729 | // private boolean AddInterceptorForList(XC_MethodHook.MethodHookParam param, Object httpLoggingInterceptor)
730 | // throws IllegalAccessException {
731 | // //参数1 是 interceptors 可以修改
732 | // try {
733 | // Object object;
734 | // if (param.args == null || param.args.length == 0) {
735 | // object = param.thisObject;
736 | // CLogUtils.e("object=param.thisObject");
737 | // } else {
738 | // object = param.args[0];
739 | // CLogUtils.e("object=param.args[0] " + param.args.length);
740 | // }
741 | // for (Field field : object.getClass().getDeclaredFields()) {
742 | // if (field.getType().getName().equals(List.class.getName())) {
743 | // Type genericType = field.getGenericType();
744 | // if (null != genericType) {
745 | // ParameterizedType pt = (ParameterizedType) genericType;
746 | // // 得到泛型里的class类型对象
747 | // Class> actualTypeArgument = (Class>) pt.getActualTypeArguments()[0];
748 | // if (actualTypeArgument.getName().equals(interceptorClass.getName())) {
749 | // field.setAccessible(true);
750 | // List list;
751 | // if (param.args.length == 0) {
752 | // list = (List) field.get(param.thisObject);
753 | // } else {
754 | // list = (List) field.get(param.args[0]);
755 | // }
756 | //// if (isNeedAddLogging(list, httpLoggingInterceptor)) {
757 | // list.add(httpLoggingInterceptor);
758 | // CLogUtils.e("添加拦截器成功");
759 | //// }
760 | // return true;
761 | // }
762 | // }
763 | // }
764 | // }
765 | // } catch (Throwable e) {
766 | // CLogUtils.e("AddInterceptorForList error " + e.getMessage());
767 | // CLogUtils.e("AddInterceptorForList error " + Log.getStackTraceString(e));
768 | // }
769 | // return false;
770 | // }
771 | //
772 | // private boolean isNeedAddLogging(List list, Object httpLoggingInterceptor) {
773 | // CLogUtils.e("当前拦截器集合的个数 " + list.size());
774 | // CLogUtils.e("当前要添加拦截器的HashCode " + httpLoggingInterceptor.hashCode());
775 | //
776 | // for (int i = 0; i < list.size(); i++) {
777 | // CLogUtils.e("集合内部HashCode " + list.get(i).hashCode());
778 | //
779 | // if (list.get(i).hashCode() == httpLoggingInterceptor.hashCode()) {
780 | // return false;
781 | // }
782 | // }
783 | // return true;
784 | // }
785 | //
786 | // /**
787 | // * 自实现 loggerInterceptor 类
788 | // */
789 | // private Object getHttpLoggingInterceptorImp() {
790 | //
791 | // if (mProxyInstance != null) {
792 | // return mProxyInstance;
793 | // }
794 | // try {
795 | // if (interceptorClass != null) {
796 | // CLogUtils.e("在init函数执行之前,拦截器Class名字 " + interceptorClass.getName());
797 | // if (LogInterceptorImp.init(mClassList, interceptorClass, mLoader)) {
798 | // LogInterceptorImp logInterceptorImp = new LogInterceptorImp();
799 | // Object proxyInstance = Proxy.newProxyInstance(mLoader, new Class[]{interceptorClass}, logInterceptorImp);
800 | // mProxyInstance = proxyInstance;
801 | // return proxyInstance;
802 | // } else {
803 | // //动态初始化拦截器失败
804 | // CLogUtils.e("动态初始化My拦截器失败");
805 | // }
806 | // return null;
807 | // } else {
808 | // CLogUtils.e("getInterceptorClass 返回 Null");
809 | // return null;
810 | // }
811 | // } catch (Throwable e) {
812 | // CLogUtils.e("getHttpLoggingInterceptorImp error " + e.toString());
813 | // e.printStackTrace();
814 | // }
815 | // return null;
816 | // }
817 | //
818 | // private Class getInterceptorClass() {
819 | // try {
820 | // Class aClass = getClass("okhttp3.Interceptor");
821 | // if (aClass != null) {
822 | // return aClass;
823 | // }
824 | //
825 | // for (Class mClass : mClassList) {
826 | // if (isInterceptorClass(mClass)) {
827 | // CLogUtils.e("找到了 Interceptor 类名是 " + mClass.getName());
828 | // return mClass;
829 | // }
830 | // }
831 | // } catch (Throwable e) {
832 | // CLogUtils.e("getInterceptorClass error " + e.toString());
833 | // }
834 | // return null;
835 | // }
836 | //
837 | // /**
838 | // * 判断是否是 Interceptor
839 | // */
840 | // private boolean isInterceptorClass(Class mClass) {
841 | // if (mClass == null) {
842 | // return false;
843 | // }
844 | // try {
845 | // Method[] declaredMethods = mClass.getDeclaredMethods();
846 | // //一个方法 并且 方法参数 是 内部的接口
847 | // if (declaredMethods.length == 1
848 | // && mClass.isInterface()
849 | // ) {
850 | // Method declaredMethod = declaredMethods[0];
851 | // Class>[] parameterTypes = declaredMethod.getParameterTypes();
852 | //
853 | // return parameterTypes.length == 1 &&
854 | // parameterTypes[0].getName().contains(mClass.getName()) &&
855 | // declaredMethod.getExceptionTypes().length == 1 &&
856 | // declaredMethod.getExceptionTypes()[0].getName().equals(IOException.class.getName());
857 | //
858 | // }
859 | // } catch (Throwable e) {
860 | // CLogUtils.e("isInterceptorClass error " + e.toString());
861 | // }
862 | // return false;
863 | // }
864 | //
865 | //
866 | // private void getAllMethod(Class> bin) {
867 | // Method[] methods = bin.getMethods();
868 | // for (Method m : methods) {
869 | // CLogUtils.e(m.getName());
870 | // }
871 | // }
872 | //
873 | // /**
874 | // * 尝试获取拦截器
875 | // *
876 | // * @return
877 | // */
878 | // @NonNull
879 | // private synchronized Object getHttpLoggingInterceptorClass() {
880 | // //防止多次初始化影响性能
881 | // if (httpLoggingInterceptor != null) {
882 | // return httpLoggingInterceptor;
883 | // }
884 | //
885 | // try {
886 | // //第一步 首先 判断 本身项目里 是否存在 拦截器
887 | // //okhttp3.logging.HttpLoggingInterceptor
888 | // try {
889 | // mHttpLoggingInterceptorClass = getClass("okhttp3.logging.HttpLoggingInterceptor");
890 | // mHttpLoggingInterceptorLoggerClass = getClass("okhttp3.logging.HttpLoggingInterceptor$Logger");
891 | //
892 | // } catch (Throwable e) {
893 | //
894 | // }
895 | // if (mHttpLoggingInterceptorClass != null && mHttpLoggingInterceptorLoggerClass != null) {
896 | // CLogUtils.e("拿到了App本身的 拦截器 log");
897 | // return InitInterceptor();
898 | // }
899 | //
900 | // if (mHttpLoggingInterceptorLoggerClass == null || mHttpLoggingInterceptorClass == null) {
901 | // //当前 App 使用了OkHttp 三种种情况,需分别进行处理
902 | // //1,App没有被混淆,没有拦截器
903 | // if (isExactness()) {
904 | // CLogUtils.e("当前App的OkHttp没有被混淆,可直接动态添加拦截器");
905 | // //直接尝试动态加载即可
906 | // return initLoggingInterceptor();
907 | // } else {
908 | // CLogUtils.e("当前App的OkHttp被混淆");
909 | // //2,App被混淆,有拦截器,根据拦截器特征获取
910 | // Object httpLoggingInterceptorForClass = getHttpLoggingInterceptorForClass();
911 | // if (httpLoggingInterceptorForClass == null) {
912 | // CLogUtils.e("App Okhttp被混淆 并且没有拦截器");
913 | // //3,App被混淆,没有拦截器
914 | // return getHttpLoggingInterceptorImp();
915 | // } else {
916 | // CLogUtils.e("App Okhttp被混淆 存在拦截器");
917 | // return httpLoggingInterceptorForClass;
918 | // }
919 | // }
920 | //
921 | //
922 | // }
923 | //
924 | // } catch (Throwable e) {
925 | // CLogUtils.e("getHttpLoggingInterceptor 拦截器初始化出现异常 " + e.toString());
926 | // e.printStackTrace();
927 | // }
928 | // return null;
929 | // }
930 | //
931 | //
932 | // private boolean isHttpLoggingInterceptor(Class MClass) {
933 | // //Class本身是final类型 并且实现了拦截器接口,拦截器接口个数1
934 | // try {
935 | // if (Modifier.isFinal(MClass.getModifiers()) && MClass.getInterfaces().length == 1) {
936 | //
937 | // Field[] declaredFields = MClass.getDeclaredFields();
938 | // for (Field field : declaredFields) {
939 | // int setCount = 0;
940 | // int charSetCount = 0;
941 | // // private volatile Set headersToRedact = Collections.emptySet();
942 | // if (field.getType().getName().equals(Set.class.getName())
943 | // && Modifier.isPrivate(field.getModifiers())
944 | // && Modifier.isVolatile(field.getModifiers())
945 | // ) {
946 | // setCount++;
947 | // }
948 | // // private static final Charset UTF8 = Charset.forName("UTF-8");
949 | // if (field.getType().getName().equals(Charset.class.getName())
950 | // && Modifier.isPrivate(field.getModifiers())
951 | // && Modifier.isStatic(field.getModifiers())
952 | // && Modifier.isFinal(field.getModifiers())
953 | // ) {
954 | // charSetCount++;
955 | // }
956 | // if (setCount == 1 && charSetCount == 1) {
957 | // CLogUtils.e("发现HttpLoggingInterceptor名字是 " + MClass.getName());
958 | // return true;
959 | // }
960 | // }
961 | // }
962 | // } catch (Throwable e) {
963 | // e.printStackTrace();
964 | // }
965 | // return false;
966 | // }
967 | //
968 | // /**
969 | // * App okHttp混淆以后尝试获取混淆以后的拦截器
970 | // * 对HttpLoggingInterceptor 进行判断
971 | // *
972 | // * @return
973 | // */
974 | // private Object getHttpLoggingInterceptorForClass() {
975 | // for (Class MClass : mClassList) {
976 | // if (isHttpLoggingInterceptor(MClass)) {
977 | // mHttpLoggingInterceptorClass = MClass;
978 | // return getOkHttpLoggingInterceptorLogger();
979 | // }
980 | // }
981 | // return null;
982 | // }
983 | //
984 | // /**
985 | // * @return 这个App是否是被混淆的okHttp
986 | // */
987 | // private boolean isExactness() {
988 | // return OkHttpClient.getName().equals("okhttp3.OkHttpClient")
989 | // && OkHttpBuilder.getName().equals("okhttp3.OkHttpClient$Builder")
990 | // //拦截器里面常用的类不等于Null 才可以保证插件正常加载
991 | // && getClass("okio.Buffer") != null
992 | // && getClass("okio.BufferedSource") != null
993 | // && getClass("okio.GzipSource") != null
994 | // && getClass("okhttp3.Request") != null
995 | // && getClass("okhttp3.Response") != null
996 | // && getClass("okio.Okio") != null
997 | // && getClass("okio.Base64") != null
998 | // ;
999 | // }
1000 | //
1001 | //
1002 | // private Object getOkHttpLoggingInterceptorLogger() {
1003 | // try {
1004 | // if (mHttpLoggingInterceptorClass == null) {
1005 | // CLogUtils.e("getOkHttpLoggingInterceptorLogger mHttpLoggingInterceptor==null");
1006 | // return null;
1007 | // }
1008 | // CLogUtils.e("开始查找 logger 和 Leave");
1009 | //
1010 | // for (Class stringName : mClassList) {
1011 | // String ClassName = stringName.getName();
1012 | // //包含外部类 并且 是 接口
1013 | // if (ClassName.contains(mHttpLoggingInterceptorClass.getName() + "$") && stringName.isInterface()) {
1014 | // mHttpLoggingInterceptorLoggerClass = stringName;
1015 | // CLogUtils.e("找到了 mHttpLoggingInterceptorLoggerClass " + mHttpLoggingInterceptorLoggerClass.getName());
1016 | // }
1017 | // if (ClassName.contains(mHttpLoggingInterceptorClass.getName() + "$") && stringName.isEnum()) {
1018 | // mHttpLoggingInterceptorLoggerEnum = stringName;
1019 | // CLogUtils.e("找到了 mHttpLoggingInterceptorLoggerEnum " + mHttpLoggingInterceptorLoggerEnum.getName());
1020 | // }
1021 | // }
1022 | // if (mHttpLoggingInterceptorClass != null &&
1023 | // mHttpLoggingInterceptorLoggerClass != null &&
1024 | // mHttpLoggingInterceptorLoggerEnum != null) {
1025 | // return InitInterceptor2();
1026 | // } else {
1027 | // CLogUtils.e("没有找到 mHttpLoggingInterceptorLoggerClass 和 mHttpLoggingInterceptorLoggerEnum");
1028 | // }
1029 | // } catch (Throwable e) {
1030 | // e.printStackTrace();
1031 | // }
1032 | // return null;
1033 | // }
1034 | //
1035 | //
1036 | // /**
1037 | // * Hook 底层的方法
1038 | // * 这个是不管 什么 框架请求都会走的 函数
1039 | // */
1040 | // private void HookGetOutPushStream() {
1041 | //
1042 | // CLogUtils.e("开始 Hook底层 实现 ");
1043 | // //java.net.SocketInputStream
1044 | //
1045 | //
1046 | // try {
1047 | // XposedHelpers.findAndHookMethod(
1048 | // XposedHelpers.findClass("java.net.SocketOutputStream", mLoader),
1049 | // "write",
1050 | // byte[].class,
1051 | // int.class,
1052 | // int.class,
1053 | //// int.class,
1054 | // new XC_MethodHook() {
1055 | // @Override
1056 | // protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
1057 | // super.beforeHookedMethod(param);
1058 | // StringBuilder TraceString = new StringBuilder();
1059 | // if (isShowStacktTrash) {
1060 | // try {
1061 | // int b = 1 / 0;
1062 | // } catch (Throwable e) {
1063 | // StackTraceElement[] stackTrace = e.getStackTrace();
1064 | // TraceString.append(" -------------------------- >>>> " + "\n");
1065 | // for (StackTraceElement stackTraceElement : stackTrace) {
1066 | // //FileUtils.SaveString( );
1067 | // TraceString.append(" 栈信息 ").append(stackTraceElement.getClassName()).append(".").append(stackTraceElement.getMethodName()).append("行数 ").append(stackTraceElement.getLineNumber()).append("\n");
1068 | // }
1069 | // TraceString.append("<<<< -------------------------- " + "\n");
1070 | // }
1071 | // }
1072 | // TraceString.append("<<<<------------------------------>>>>> \n")
1073 | // .append(new String((byte[]) param.args[0], StandardCharsets.UTF_8))
1074 | // .append("\n <<<<------------------------------>>>>>").append("\n");
1075 | //
1076 | // CLogUtils.NetLogger(TraceString.toString());
1077 | //// FileUtils.SaveString(mSimpleDateFormat.format(new Date(System.currentTimeMillis())) + "\n" + " " +
1078 | //// TraceString.toString(), mOtherContext.getPackageName());
1079 | // }
1080 | // });
1081 | //
1082 | //
1083 | //// XposedHelpers.findAndHookMethod(httpUrlConnClass, mLoader,
1084 | //// httpUrlConnGetInputStreamMethod, new XC_MethodHook() {
1085 | //// @Override
1086 | //// protected void afterHookedMethod(XC_MethodHook.MethodHookParam param) throws IOException {
1087 | //// final Object httpUrlConnection = param.thisObject;
1088 | //// URL url = (URL) XposedHelpers.callMethod(httpUrlConnection, httpUrlConnGetUrlMethod);
1089 | //// InputStream in = (InputStream) param.getResult();
1090 | ////
1091 | //// InputStreamWrapper wrapper = CACHE.get();
1092 | ////
1093 | //// if (wrapper != null) {
1094 | //// param.setResult(wrapper);
1095 | //// return;
1096 | //// }
1097 | //// //流只能读取一次 用完在放回去
1098 | //// wrapper = new InputStreamWrapper(in) {
1099 | //// public void close() {
1100 | //// try {
1101 | //// super.close();
1102 | //// } catch (IOException e) {
1103 | //// e.printStackTrace();
1104 | //// }
1105 | //// CACHE.remove();
1106 | //// }
1107 | //// };
1108 | //// //把数据流放到指定ThreadLocal里面保存起来
1109 | //// CACHE.set(wrapper);
1110 | //// param.setResult(wrapper);
1111 | //// //判断是否保存成功
1112 | //// if (wrapper.size() > 0) {
1113 | //// //拿到解码类型
1114 | //// String contentEncoding = (String) XposedHelpers.callMethod(httpUrlConnection,
1115 | //// httpUrlConnGetContentEncodingMethod);
1116 | //// BufferedReader reader = null;
1117 | //// StringBuffer responseBody = new StringBuffer();
1118 | //// try {
1119 | //// if (contentEncoding != null) {
1120 | //// if ("gzip".equals(contentEncoding)) {
1121 | //// in = new GZIPInputStream(in);
1122 | //// }
1123 | //// CLogUtils.e("走了 gzip 解码");
1124 | ////
1125 | //// //对gzip进行解析
1126 | //// reader = new BufferedReader(new InputStreamReader(in));
1127 | ////
1128 | //// String line;
1129 | //// while ((line = reader.readLine()) != null) {
1130 | //// responseBody.append(line);
1131 | //// }
1132 | //// } else {
1133 | //// CLogUtils.e("走了 Body 解码");
1134 | ////
1135 | //// //返回的是一个HttpEngine
1136 | //// Object HttpEngine = XposedHelpers.callMethod(httpUrlConnection,
1137 | //// httpUrlConngetGetResponseMethod);
1138 | //// Object respons = XposedHelpers.callMethod(HttpEngine,
1139 | //// httpUrlConngetGetResponseMethod);
1140 | //// //先判断是否含有Body
1141 | //// boolean hasBody = (boolean) XposedHelpers.callMethod(HttpEngine,
1142 | //// "hasBody", respons);
1143 | //// if (hasBody) {
1144 | //// Object body = XposedHelpers.callMethod(respons,
1145 | //// "body");
1146 | //// String string = (String) XposedHelpers.callMethod(body,
1147 | //// "string");
1148 | //// responseBody.append(string);
1149 | ////
1150 | //// }
1151 | //// }
1152 | //// Map> requestHeadersMap =
1153 | //// (Map>) XposedHelpers.callMethod(httpUrlConnection,
1154 | //// httpUrlConngetHeaderFieldsMethod);
1155 | ////
1156 | //// StringBuilder RequestHeaders = new StringBuilder();
1157 | ////
1158 | //// if (requestHeadersMap != null) {
1159 | ////
1160 | //// for (String key : requestHeadersMap.keySet()) {
1161 | //// RequestHeaders.append(key);
1162 | //// List values = requestHeadersMap.get(key);
1163 | //// for (String value : values) {
1164 | //// RequestHeaders.append(value).append(" ");
1165 | //// }
1166 | //// RequestHeaders.append("\n");
1167 | //// }
1168 | //// }
1169 | ////
1170 | //// CLogUtils.e("当前Url \n" + url.toString() + "\n");
1171 | //// CLogUtils.e("请求头部信息 \n" + RequestHeaders.toString() + "\n");
1172 | ////
1173 | //// CLogUtils.e("当前响应 \n" + responseBody.toString() + "\n");
1174 | ////
1175 | //// StringBuilder TraceString = new StringBuilder();
1176 | ////
1177 | ////
1178 | //// if (isShowStacktTrash) {
1179 | //// try {
1180 | //// int b = 1 / 0;
1181 | //// } catch (Throwable e) {
1182 | //// StackTraceElement[] stackTrace = e.getStackTrace();
1183 | //// TraceString.append(" -------------------------- >>>> " + "\n");
1184 | //// for (StackTraceElement stackTraceElement : stackTrace) {
1185 | //// //FileUtils.SaveString( );
1186 | //// TraceString.append(" 栈信息 ").append(stackTraceElement.getClassName()).append(".").append(stackTraceElement.getMethodName()).append("行数 ").append(stackTraceElement.getLineNumber()).append("\n");
1187 | //// }
1188 | //// TraceString.append("<<<< -------------------------- " + "\n");
1189 | //// }
1190 | //// }
1191 | ////
1192 | ////// TraceString.append("<<<<------------------------------>>>>> ")
1193 | ////// .append("Url : ").append(url.toString()).append("\n")
1194 | ////// .append("请求头部信息 : ").append(RequestHeaders.toString()).append("\n")
1195 | ////// .append("响应信息 : ").append(responseBody.toString()).append("\n")
1196 | ////// .append(" <<<<------------------------------>>>>>").append("\n");
1197 | //////
1198 | ////// FileUtils.SaveString(mSimpleDateFormat.format(new Date(System.currentTimeMillis())) + "\n" + " " +
1199 | ////// TraceString.toString(), mOtherContext.getPackageName());
1200 | ////
1201 | //// } catch (Throwable e) {
1202 | //// CLogUtils.e("转换出现错误 " + e.toString() + " line " + e.getStackTrace());
1203 | ////
1204 | //// } finally {
1205 | //// try {
1206 | //// if (reader != null) {
1207 | //// reader.close();
1208 | //// }
1209 | //// } catch (Throwable e) {
1210 | //// CLogUtils.e("inputStream hook handle close reader error. url:" + url + ", contentEncoding:" + contentEncoding);
1211 | //// }
1212 | //// }
1213 | //// }
1214 | //// }
1215 | //// });
1216 | ////
1217 | ////
1218 | // } catch (Throwable e) {
1219 | // CLogUtils.e("HookGetOutPushStream " + e.toString());
1220 | // e.printStackTrace();
1221 | // }
1222 | // }
1223 | //
1224 | //
1225 | // private Object InitInterceptor2() {
1226 | // try {
1227 | //
1228 | // Object logger = Proxy.newProxyInstance(mLoader, new Class[]{mHttpLoggingInterceptorLoggerClass}, Hook.this);
1229 | //
1230 | // CLogUtils.e("拿到 动态代理的 class");
1231 | // Object loggingInterceptor = mHttpLoggingInterceptorClass.getConstructor(mHttpLoggingInterceptorLoggerClass).newInstance(logger);
1232 | // CLogUtils.e("拿到 拦截器的实体类 ");
1233 | //
1234 | // Object level = mHttpLoggingInterceptorLoggerEnum.getEnumConstants()[3];
1235 | // CLogUtils.e("拿到 Level 枚举 ");
1236 | //
1237 | // Method setLevelMethod = getSetLevelMethod(mHttpLoggingInterceptorLoggerEnum);
1238 | //
1239 | // if (setLevelMethod == null) {
1240 | // CLogUtils.e("没有找到 setLevelMethod 方法体 ");
1241 | // //HookGetOutPushStream();
1242 | // } else {
1243 | // setLevelMethod.setAccessible(true);
1244 | // Object invoke = setLevelMethod.invoke(loggingInterceptor, level);
1245 | // CLogUtils.e("调用 setLevel成功 返回对应的 对象 " + invoke.getClass().getName());
1246 | // return invoke;
1247 | // }
1248 | //
1249 | //
1250 | // } catch (Throwable e) {
1251 | // CLogUtils.e("拦截器初始化出现异常 InitInterceptor2 " + e.toString());
1252 | // e.printStackTrace();
1253 | // }
1254 | // return null;
1255 | // }
1256 | //
1257 | // @Nullable
1258 | // private Method getSetLevelMethod(Class level) {
1259 | // CLogUtils.e("getSetLevelMethod 需要的参数类型 是 " + level.getName());
1260 | //
1261 | // Method[] declaredMethods = mHttpLoggingInterceptorClass.getDeclaredMethods();
1262 | // for (Method declaredMethod : declaredMethods) {
1263 | // Class>[] parameterTypes = declaredMethod.getParameterTypes();
1264 | // CLogUtils.e("该方法的 名字是 " + declaredMethod.getName() + " 参数的 个数是 " + parameterTypes.length);
1265 | // if (parameterTypes.length == 1) {
1266 | // CLogUtils.e("长度是 1 参数类型是 " + parameterTypes[0].getName());
1267 | // //比较 传入的类型 是 HttpLoggingInterceptor.Level 类的 方法
1268 | // if (parameterTypes[0].getName().equals(level.getName())) {
1269 | // return declaredMethod;
1270 | // }
1271 | // }
1272 | // }
1273 | // return null;
1274 | // }
1275 | //
1276 | //
1277 | // private Object InitInterceptor() throws
1278 | // InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
1279 | //// CLogUtils.e("拿到 HttpLoggingInterceptor 和 logger ");
1280 | // //通过 动态代理 拿到 这个 接口 实体类
1281 | // Object logger;
1282 | //// if (isDexLoader) {
1283 | //// logger = Proxy.newProxyInstance(mDexClassLoader, new Class[]{mHttpLoggingInterceptorLoggerClass}, Hook.this);
1284 | //// } else {
1285 | // logger = Proxy.newProxyInstance(mLoader, new Class[]{mHttpLoggingInterceptorLoggerClass}, Hook.this);
1286 | //// }
1287 | //// CLogUtils.e("拿到 动态代理的 class");
1288 | // Object loggingInterceptor = mHttpLoggingInterceptorClass.getConstructor(mHttpLoggingInterceptorLoggerClass).newInstance(logger);
1289 | //// CLogUtils.e("拿到 拦截器的实体类 ");
1290 | // Object level;
1291 | //// if (isDexLoader) {
1292 | //// level = mDexClassLoader.loadClass("okhttp3.logging.HttpLoggingInterceptor$Level").getEnumConstants()[3];
1293 | //// } else {
1294 | // level = mLoader.loadClass("okhttp3.logging.HttpLoggingInterceptor$Level").getEnumConstants()[3];
1295 | //// }
1296 | //// CLogUtils.e("拿到 Level 枚举 ");
1297 | // //调用 函数
1298 | // //setLevel
1299 | // XposedHelpers.findMethodBestMatch(mHttpLoggingInterceptorClass, "setLevel",
1300 | // level.getClass()).invoke(loggingInterceptor, level);
1301 | // CLogUtils.e("拦截器实例初始化成功 ");
1302 | // return loggingInterceptor;
1303 | // }
1304 | //
1305 | // /**
1306 | // * 用 classloader 加载 这个jar 包 防止 classloader 不统一
1307 | // */
1308 | // private Object initLoggingInterceptor() {
1309 | // CLogUtils.e("开始 动态 加载 初始化 ");
1310 | //
1311 | // File dexOutputDir = mOtherContext.getDir("dex", 0);
1312 | //
1313 | // CLogUtils.e("dexOutputDir dex 666 " + dexOutputDir.getAbsolutePath());
1314 | // // 定义DexClassLoader
1315 | // // 第一个参数:是dex压缩文件的路径
1316 | // // 第二个参数:是dex解压缩后存放的目录
1317 | // // 第三个参数:是C/C++依赖的本地库文件目录,可以为null
1318 | // // 第四个参数:是上一级的类加载器
1319 | // mDexClassLoader = new DexClassLoader(INTERCEPTORPATH, dexOutputDir.getAbsolutePath(), null, mLoader);
1320 | // try {
1321 | //
1322 | // if (AddElements()) {
1323 | //
1324 | // //mHttpLoggingInterceptor = mDexClassLoader.loadClass("okhttp3.logging.HttpLoggingInterceptor");
1325 | // //mHttpLoggingInterceptorLoggerClass = mDexClassLoader.loadClass("okhttp3.logging.HttpLoggingInterceptor$Logger");
1326 | //
1327 | // mHttpLoggingInterceptorClass = mLoader.loadClass("okhttp3.logging.HttpLoggingInterceptor");
1328 | // mHttpLoggingInterceptorLoggerClass = mLoader.loadClass("okhttp3.logging.HttpLoggingInterceptor$Logger");
1329 | // if (mHttpLoggingInterceptorLoggerClass != null && mHttpLoggingInterceptorClass != null) {
1330 | // CLogUtils.e("动态 加载 classloader 成功 ");
1331 | //
1332 | // return InitInterceptor();
1333 | // } else {
1334 | // return null;
1335 | // }
1336 | // }
1337 | // return null;
1338 | //
1339 | //
1340 | // } catch (Throwable e) {
1341 | // CLogUtils.e("initLoggingInterceptor 动态加载异常 " + e.toString());
1342 | // e.printStackTrace();
1343 | // }
1344 | // return null;
1345 | //
1346 | // }
1347 | //
1348 | //
1349 | // /**
1350 | // * @return Dex 是否合并 成功
1351 | // */
1352 | // private boolean AddElements() {
1353 | // //自己的 classloader 里面的 element数组
1354 | // Object[] myDexClassLoaderElements = getMyDexClassLoaderElements();
1355 | // if (myDexClassLoaderElements == null) {
1356 | // CLogUtils.e("AddElements myDexClassLoaderElements null");
1357 | // return false;
1358 | // } else {
1359 | // CLogUtils.e("AddElements 成功 拿到 myDexClassLoaderElements 自己的Elements 长度是 " + myDexClassLoaderElements.length);
1360 | // }
1361 | // //系统的 classloader 里面的 element数组
1362 | // Object[] classLoaderElements = getClassLoaderElements();
1363 | // //将数组合并
1364 | // if (classLoaderElements == null) {
1365 | // CLogUtils.e("AddElements classLoaderElements null");
1366 | // return false;
1367 | // } else {
1368 | // CLogUtils.e("AddElements 成功 拿到 classLoaderElements 系统的Elements 长度是 " + classLoaderElements.length);
1369 | // }
1370 | //
1371 | // //DexElements合并
1372 | // Object[] combined = (Object[]) Array.newInstance(classLoaderElements.getClass().getComponentType(),
1373 | // classLoaderElements.length + myDexClassLoaderElements.length);
1374 | //
1375 | // System.arraycopy(classLoaderElements, 0, combined, 0, classLoaderElements.length);
1376 | // System.arraycopy(myDexClassLoaderElements, 0, combined, classLoaderElements.length, myDexClassLoaderElements.length);
1377 | //
1378 | //
1379 | // //Object[] dexElementsResut = concat(myDexClassLoaderElements, classLoaderElements);
1380 | //
1381 | // if ((classLoaderElements.length + myDexClassLoaderElements.length) != combined.length) {
1382 | // CLogUtils.e("合并 elements数组 失败 null");
1383 | // }
1384 | // //合并成功 重新 加载
1385 | // return SetDexElements(combined, myDexClassLoaderElements.length + classLoaderElements.length);
1386 | // }
1387 | //
1388 | // /**
1389 | // * 将 Elements 数组 set回原来的 classloader里面
1390 | // *
1391 | // * @param dexElementsResut
1392 | // */
1393 | // private boolean SetDexElements(Object[] dexElementsResut, int conunt) {
1394 | // try {
1395 | // Field pathListField = mLoader.getClass().getSuperclass().getDeclaredField("pathList");
1396 | // if (pathListField != null) {
1397 | // pathListField.setAccessible(true);
1398 | // Object dexPathList = pathListField.get(mLoader);
1399 | // Field dexElementsField = dexPathList.getClass().getDeclaredField("dexElements");
1400 | // if (dexElementsField != null) {
1401 | // dexElementsField.setAccessible(true);
1402 | // //先 重新设置一次
1403 | // dexElementsField.set(dexPathList, dexElementsResut);
1404 | // //重新 get 用
1405 | // Object[] dexElements = (Object[]) dexElementsField.get(dexPathList);
1406 | // if (dexElements.length == conunt && Arrays.hashCode(dexElements) == Arrays.hashCode(dexElementsResut)) {
1407 | // return true;
1408 | // } else {
1409 | // CLogUtils.e("合成 长度 " + dexElements.length + "传入 数组 长度 " + conunt);
1410 | //
1411 | // CLogUtils.e(" dexElements hashCode " + Arrays.hashCode(dexElements) + " " + Arrays.hashCode(dexElementsResut));
1412 | //
1413 | // return false;
1414 | // }
1415 | // } else {
1416 | // CLogUtils.e("SetDexElements 获取 dexElements == null");
1417 | // }
1418 | // } else {
1419 | // CLogUtils.e("SetDexElements 获取 pathList == null");
1420 | // }
1421 | // } catch (NoSuchFieldException e) {
1422 | // CLogUtils.e("SetDexElements NoSuchFieldException " + e.toString());
1423 | // e.printStackTrace();
1424 | // } catch (IllegalAccessException e) {
1425 | // CLogUtils.e("SetDexElements IllegalAccessException " + e.toString());
1426 | // e.printStackTrace();
1427 | // }
1428 | // return false;
1429 | // }
1430 | //
1431 | // private synchronized void getAllClassName() {
1432 | //
1433 | // //保证每次初始化 之前 保证干净
1434 | // AllClassNameList.clear();
1435 | // CLogUtils.e("开始 获取全部的类名 ");
1436 | //
1437 | // try {
1438 | // //系统的 classloader是 Pathclassloader需要 拿到他的 父类 BaseClassloader才有 pathList
1439 | // if (mLoader == null) {
1440 | // return;
1441 | // }
1442 | // Field pathListField = mLoader.getClass().getSuperclass().getDeclaredField("pathList");
1443 | // if (pathListField != null) {
1444 | // pathListField.setAccessible(true);
1445 | // Object dexPathList = pathListField.get(mLoader);
1446 | // Field dexElementsField = dexPathList.getClass().getDeclaredField("dexElements");
1447 | // if (dexElementsField != null) {
1448 | // dexElementsField.setAccessible(true);
1449 | // Object[] dexElements = (Object[]) dexElementsField.get(dexPathList);
1450 | // for (Object dexElement : dexElements) {
1451 | // Field dexFileField = dexElement.getClass().getDeclaredField("dexFile");
1452 | // if (dexFileField != null) {
1453 | // dexFileField.setAccessible(true);
1454 | // DexFile dexFile = (DexFile) dexFileField.get(dexElement);
1455 | // getDexFileClassName(dexFile);
1456 | // } else {
1457 | // CLogUtils.e("获取 dexFileField Null ");
1458 | // }
1459 | // }
1460 | // } else {
1461 | // CLogUtils.e("获取 dexElements Null ");
1462 | // }
1463 | // } else {
1464 | // CLogUtils.e("获取 pathListField Null ");
1465 | // }
1466 | // //获取 app包里的
1467 | // //DexFile df = new DexFile(mOtherContext.getPackageCodePath());
1468 | // } catch (Throwable e) {
1469 | // CLogUtils.e("getAllClassName Throwable " + e.toString());
1470 | // e.printStackTrace();
1471 | // }
1472 | // }
1473 | //
1474 | // /**
1475 | // * 数组合并
1476 | // * @param a 原数组
1477 | // * @param b 新数组
1478 | // * @return 合并以后的数组
1479 | // */
1480 | //// static Object[] concat(Object[] a, Object[] b) {
1481 | ////
1482 | //// //Object[] c= new Object[a.length+b.length];
1483 | ////
1484 | ////
1485 | //// System.arraycopy(original, 0, combined, 0, original.length);
1486 | //// System.arraycopy(extraElements, 0, combined, original.length, extraElements.length);
1487 | ////
1488 | //// return c;
1489 | ////
1490 | //// }
1491 | //
1492 | //
1493 | // /**
1494 | // * 将自己 创建的 classloader 里面的 内容添加到 原来的 classloader里面
1495 | // */
1496 | // private Object[] getMyDexClassLoaderElements() {
1497 | // try {
1498 | // Field pathListField = mDexClassLoader.getClass().getSuperclass().getDeclaredField("pathList");
1499 | // if (pathListField != null) {
1500 | // pathListField.setAccessible(true);
1501 | // Object dexPathList = pathListField.get(mDexClassLoader);
1502 | // Field dexElementsField = dexPathList.getClass().getDeclaredField("dexElements");
1503 | // if (dexElementsField != null) {
1504 | // dexElementsField.setAccessible(true);
1505 | // Object[] dexElements = (Object[]) dexElementsField.get(dexPathList);
1506 | // if (dexElements != null) {
1507 | // return dexElements;
1508 | // } else {
1509 | // CLogUtils.e("AddElements 获取 dexElements == null");
1510 | // }
1511 | // //ArrayUtils.addAll(first, second);
1512 | // } else {
1513 | // CLogUtils.e("AddElements 获取 dexElements == null");
1514 | // }
1515 | // } else {
1516 | // CLogUtils.e("AddElements 获取 pathList == null");
1517 | // }
1518 | // } catch (NoSuchFieldException e) {
1519 | // CLogUtils.e("AddElements NoSuchFieldException " + e.toString());
1520 | // e.printStackTrace();
1521 | // } catch (IllegalAccessException e) {
1522 | // CLogUtils.e("AddElements IllegalAccessException " + e.toString());
1523 | // e.printStackTrace();
1524 | // }
1525 | // return null;
1526 | // }
1527 | //
1528 | // /**
1529 | // * 获取系统的 classaLoder
1530 | // */
1531 | // private Object[] getClassLoaderElements() {
1532 | // try {
1533 | // Field pathListField = mLoader.getClass().getSuperclass().getDeclaredField("pathList");
1534 | // if (pathListField != null) {
1535 | // pathListField.setAccessible(true);
1536 | // Object dexPathList = pathListField.get(mLoader);
1537 | // Field dexElementsField = dexPathList.getClass().getDeclaredField("dexElements");
1538 | // if (dexElementsField != null) {
1539 | // dexElementsField.setAccessible(true);
1540 | // Object[] dexElements = (Object[]) dexElementsField.get(dexPathList);
1541 | // if (dexElements != null) {
1542 | // return dexElements;
1543 | // } else {
1544 | // CLogUtils.e("AddElements 获取 dexElements == null");
1545 | // }
1546 | // //ArrayUtils.addAll(first, second);
1547 | // } else {
1548 | // CLogUtils.e("AddElements 获取 dexElements == null");
1549 | // }
1550 | // } else {
1551 | // CLogUtils.e("AddElements 获取 pathList == null");
1552 | // }
1553 | // } catch (NoSuchFieldException e) {
1554 | // CLogUtils.e("AddElements NoSuchFieldException " + e.toString());
1555 | // e.printStackTrace();
1556 | // } catch (IllegalAccessException e) {
1557 | // CLogUtils.e("AddElements IllegalAccessException " + e.toString());
1558 | // e.printStackTrace();
1559 | // }
1560 | // return null;
1561 | // }
1562 | //
1563 | //}
--------------------------------------------------------------------------------