Skip to content

Commit abc9462

Browse files
committed
Add R Rewritter
1 parent b7b6b3b commit abc9462

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

PluginCore/src/com/plugin/util/ResourceUtil.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import com.plugin.core.PluginPublicXmlConst;
66

7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
710
/**
811
* Created by cailiming
912
*/
@@ -84,4 +87,35 @@ public static boolean isMainResId(int resid) {
8487
//默认宿主的资源id以0x7f3X开头
8588
return PluginPublicXmlConst.resourceMap.get(resid>>16) != null;
8689
}
90+
91+
public static void rewriteRValues(ClassLoader cl, String packageName, int id) {
92+
final Class<?> rClazz;
93+
try {
94+
rClazz = cl.loadClass(packageName + ".R");
95+
} catch (ClassNotFoundException e) {
96+
LogUtil.d("No resource references to update in package " + packageName);
97+
return;
98+
}
99+
100+
final Method callback;
101+
try {
102+
callback = rClazz.getMethod("onResourcesLoaded", int.class);
103+
} catch (NoSuchMethodException e) {
104+
// No rewriting to be done.
105+
return;
106+
}
107+
108+
Throwable cause;
109+
try {
110+
callback.invoke(null, id);
111+
return;
112+
} catch (IllegalAccessException e) {
113+
cause = e;
114+
} catch (InvocationTargetException e) {
115+
cause = e.getCause();
116+
}
117+
118+
throw new RuntimeException("Failed to rewrite resource references for " + packageName,
119+
cause);
120+
}
87121
}

0 commit comments

Comments
 (0)