工具类

package com.example.hotfix;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DexToImage {
    private static String TAG="DexToImage";
    public byte[][] dextoimage(Context context, int rawDexFileId, int drawableResourceId) {
        byte[][] result = new byte[3][];

        try {
            // 1. 读取DEX文件并转换为字节数组
            InputStream dexInputStream = context.getResources().openRawResource(rawDexFileId);
            File dexTempFile = new File(context.getCacheDir(), "classes.dex");
            writeInputStreamToFile(dexInputStream, dexTempFile);
            byte[] dexBytes = readBytesFromFile(dexTempFile);

            int a=dexBytes.length;
            Log.e(TAG, "dex数组长度为:"+a);
            Log.e(TAG, "dex数组为:"+dexBytes);
            result[0] = dexBytes;

            // 2. 读取图片文件并转换为字节数组
            int drawid=R.drawable.test;
            byte[] imageBytes = drawableToByteArray(context,drawid);
            int b=imageBytes.length;
            Log.e(TAG, "图片数组长度为:"+b);
            result[1] = imageBytes;

            // 3. 合并两个字节数组
            byte[] combinedBytes = mergeByteArrays(imageBytes, dexBytes);
            int c=combinedBytes.length;
            Log.e(TAG, "合并数组长度为:"+c);
            result[2] = combinedBytes;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }
    private static void writeInputStreamToFile(InputStream inputStream, File file) throws IOException {
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static byte[] readBytesFromFile(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        fis.close();
        return bos.toByteArray();
    }

    private static byte[] mergeByteArrays(byte[] array1, byte[] array2) {
        byte[] mergedArray = new byte[array1.length + array2.length];
        System.arraycopy(array1, 0, mergedArray, 0, array1.length);
        System.arraycopy(array2, 0, mergedArray, array1.length, array2.length);
        return mergedArray;
    }

    public static byte[] drawableToByteArray(Context context, int drawableId) {
        Resources resources = context.getResources();
        Bitmap bitmap = BitmapFactory.decodeResource(resources, drawableId);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    }

    public static String getRawResourcePath(Context context, String rawFileName) {
        Resources resources = context.getResources();
        int rawId = resources.getIdentifier(rawFileName, "raw", context.getPackageName());
        if (rawId == 0) {
            // Raw资源不存在
            Log.e(TAG, "DEX NULL--");
            return null;
        }
        return resources.getResourceName(rawId);
    }
}
public static void imagedexfile(Context context,File file) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException {
    String imagedexfile= file.getAbsolutePath();
    Log.e(TAG, "imagedexfile路径为: "+imagedexfile);
    injectDexToClassLoader(context, imagedexfile);

}

MainActivity

public static void imagefix(){
    int rawDexFileId=R.raw.classes;
    int drawableResourceId = R.drawable.test;
    byte[][] result = new DexToImage().dextoimage(context,rawDexFileId,drawableResourceId);
    try {
        int dexlenth=result[0].length;
        File dexfile=ImageToDex(dexlenth,result[2],result[1]);
        try {
            new MyFix().imagedexfile(context,dexfile);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

public static File byteArrayToDexFile(Context context,byte[] bytes) throws IOException {
    File dexFile = new File(context.getCacheDir(), "classes.dex");
    FileOutputStream fos = new FileOutputStream(dexFile);
    try {
        fos.write(bytes);
        return dexFile;
    } finally {
        fos.close();
    }
}
public static File ImageToDex(int dexLength, byte[] combinedBytes, byte[] imageBytes) throws IOException {
    // 假设 combinedBytes 是合并后的图片字节数组,其中包含了 dex 文件数据和图片数据
    byte[] dexBytes = new byte[dexLength];  // dexLength 是 dex 文件数据的长度
    Log.e(TAG, "dex byte数组长度为:"+dexBytes.length);
    Log.e(TAG, "dex byte数组为:"+dexBytes);
    System.arraycopy(combinedBytes, imageBytes.length, dexBytes, 0, dexLength);
    File dexfile=byteArrayToDexFile(context,dexBytes);
    return dexfile;
}