content: package org.lsposed.lspatch.metaloader;
  
  import android.annotation.SuppressLint;
  import android.app.AppComponentFactory;
  import android.util.Log;
  
  import org.lsposed.lspatch.share.Constants;
  
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.lang.reflect.Method;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Objects;
  
  @SuppressLint("UnsafeDynamicallyLoadedCode")
  public class LSPAppComponentFactoryStub extends AppComponentFactory {
  
      private static final String TAG = "OPatch-MetaLoader";
      private static final Map<String, String> archToLib = new HashMap<String, String>(4);
  
      public static byte[] dex;
  
      static {
          try {
              archToLib.put("arm", "armeabi-v7a");
              archToLib.put("arm64", "arm64-v8a");
              archToLib.put("x86", "x86");
              archToLib.put("x86_64", "x86_64");
  
              Log.i(TAG, "App ClassLoader:" + LSPAppComponentFactoryStub.class.getClassLoader());
  
              var cl = Objects.requireNonNull(LSPAppComponentFactoryStub.class.getClassLoader());
              Class<?> VMRuntime = Class.forName("dalvik.system.VMRuntime");
              Method getRuntime = VMRuntime.getDeclaredMethod("getRuntime");
              getRuntime.setAccessible(true);
              Method vmInstructionSet = VMRuntime.getDeclaredMethod("vmInstructionSet");
              vmInstructionSet.setAccessible(true);
              String arch = (String) vmInstructionSet.invoke(getRuntime.invoke(null));
              String libName = archToLib.get(arch);
  
              Log.i(TAG, "Bootstrap loader from embedment");
              try (var is = cl.getResourceAsStream(Constants.LOADER_DEX_ASSET_PATH);
                   var os = new ByteArrayOutputStream()) {
                  transfer(is, os);
                  dex = os.toByteArray();
              }
              String soPath = cl.getResource("assets/lspatch/so/" + libName + "/liblspatch.so").getPath().substring(5);
  
              System.load(soPath);
          } catch (Throwable e) {
              throw new ExceptionInInitializerError(e);
          }
      }
  
      private static void transfer(InputStream is, OutputStream os) throws IOException {
          byte[] buffer = new byte[8192];
          int n;
          while (-1 != (n = is.read(buffer))) {
              os.write(buffer, 0, n);
          }
      }
  }
  