由于需要在android项目中修改dex文件,所以自己实现了一个dex文件头修复。代码如下,可以直接拿来用。比较简单所以就不注释了。
public class FixDexHeaderUtil {
public static void fix(String file) {
byte[] fBytes = readFile(file);
fix(fBytes);
saveFile(fBytes, file);
}
private static byte[] readFile(String file) {
FileInputStream fis = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
fis = new FileInputStream(file);
if (fis != null && bos != null) {
int len = -1;
byte[] buf = new byte[512];
while ((len = fis.read(buf)) != -1) {
bos.write(buf, 0, len);
bos.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
byte[] fBytes = bos.toByteArray();
if (bos != null) {
try {
bos.close();
}