content: package com.zy.hotfix.insert_dex;
  
  import com.zy.hotfix.utils.Reflect;
  
  import java.lang.reflect.Array;
  
  import dalvik.system.DexClassLoader;
  import dalvik.system.PathClassLoader;
  
  public class InsertDexUtils {
  
      public static void injectDexAtFirst(String dexPath, String defaultDexOptPath) throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
          DexClassLoader dexClassLoader = new DexClassLoader(dexPath, defaultDexOptPath, dexPath, getPathClassLoader());
          Object baseDexElements = getDexElements(getPathList(getPathClassLoader()));
          Object newDexElements = getDexElements(getPathList(dexClassLoader));
          Object allDexElements = combineArray(newDexElements, baseDexElements);
          Object pathList = getPathList(getPathClassLoader());
          Reflect.on(pathList).set("dexElements", allDexElements);
      }
  
      private static PathClassLoader getPathClassLoader() {
          PathClassLoader pathClassLoader = (PathClassLoader) InsertDexUtils.class.getClassLoader();
          return pathClassLoader;
      }
  
      private static Object getDexElements(Object paramObject)
              throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException {
          return Reflect.on(paramObject).get("dexElements");
      }
  
      private static Object getPathList(Object baseDexClassLoader)
              throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
          return Reflect.on(baseDexClassLoader).get("pathList");
      }
  
      private static Object combineArray(Object firstArray, Object secondArray) {
          Class<?> localClass = firstArray.getClass().getComponentType();
          int firstArrayLength = Array.getLength(firstArray);
          int allLength = firstArrayLength + Array.getLength(secondArray);
          Object result = Array.newInstance(localClass, allLength);
          for (int k = 0; k < allLength; ++k) {
              if (k < firstArrayLength) {
                  Array.set(result, k, Array.get(firstArray, k));
              } else {
                  Array.set(result, k, Array.get(secondArray, k - firstArrayLength));
              }
          }
          return result;
      }
  
  }
  