-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActivityLoader.java
More file actions
154 lines (128 loc) · 3.87 KB
/
ActivityLoader.java
File metadata and controls
154 lines (128 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// rm bin/*
// javac -target 1.6 -source 1.6 -d bin -classpath /home/ziad/downloads/android-sdk/platforms/android-23/android.jar /home/ziad/ActivityLoader.java
// /home/ziad/downloads/android-sdk/build-tools/23.0.1/dx --dex --output=use.dex bin
// adb push use.dex /sdcard/
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import android.content.Context;
import android.content.Intent;
import dalvik.system.DexClassLoader;
import java.lang.reflect.Field;
import android.widget.Toast;
public class ActivityLoader {
public ClassLoader ORIGINAL_LOADER;
public ClassLoader CUSTOM_LOADER = null;
public String zaz (Context ctx, String name, String path) {
Toast.makeText(ctx, "start", Toast.LENGTH_SHORT).show();
try {
File dex = ctx.getDir("dex", Context.MODE_PRIVATE);
dex.mkdir();
File f = new File(dex, name);
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[0xFF];
int len;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
File fo = ctx.getDir("outdex", Context.MODE_PRIVATE);
fo.mkdir();
DexClassLoader dcl = new DexClassLoader(f.getAbsolutePath(),
fo.getAbsolutePath(), null,
java.lang.ClassLoader.getSystemClassLoader());
CUSTOM_LOADER = dcl;
Toast.makeText(ctx, String.valueOf(dcl) + "loaded, try launch again", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(ctx, String.valueOf(e), Toast.LENGTH_LONG).show();
CUSTOM_LOADER = null;
return String.valueOf(e);
}
try {
Context mBase = new Smith<Context>(ctx, "mBase").get();
Object mPackageInfo = new Smith<Object>(mBase, "mPackageInfo")
.get();
Smith<ClassLoader> sClassLoader = new Smith<ClassLoader>(
mPackageInfo, "mClassLoader");
ClassLoader mClassLoader = sClassLoader.get();
ORIGINAL_LOADER = mClassLoader;
MyClassLoader cl = new MyClassLoader(mClassLoader);
sClassLoader.set(cl);
} catch (Exception e) {
e.printStackTrace();
}
return String.valueOf(ORIGINAL_LOADER);
}
class MyClassLoader extends ClassLoader {
public MyClassLoader(ClassLoader parent) {
super(parent);
}
@Override
public Class<?> loadClass(String className)
throws ClassNotFoundException {
if (CUSTOM_LOADER != null) {
if (className.startsWith("net.ziad.")) {
}
try {
Class<?> c = CUSTOM_LOADER.loadClass(className);
if (c != null)
return c;
} catch (ClassNotFoundException e) {
}
}
return super.loadClass(className);
}
}
class Smith<T> {
private Object obj;
private String fieldName;
private boolean inited;
private Field field;
public Smith(Object obj, String fieldName) {
if (obj == null) {
throw new IllegalArgumentException("obj cannot be null");
}
this.obj = obj;
this.fieldName = fieldName;
}
private void prepare() {
if (inited)
return;
inited = true;
Class<?> c = obj.getClass();
while (c != null) {
try {
Field f = c.getDeclaredField(fieldName);
f.setAccessible(true);
field = f;
return;
} catch (Exception e) {
} finally {
c = c.getSuperclass();
}
}
}
public T get() throws NoSuchFieldException, IllegalAccessException,
IllegalArgumentException {
prepare();
if (field == null)
throw new NoSuchFieldException();
try {
@SuppressWarnings("unchecked")
T r = (T) field.get(obj);
return r;
} catch (ClassCastException e) {
throw new IllegalArgumentException("unable to cast object");
}
}
public void set(T val) throws NoSuchFieldException, IllegalAccessException,
IllegalArgumentException {
prepare();
if (field == null)
throw new NoSuchFieldException();
field.set(obj, val);
}
}
}