最近尝试热修复,反射调用补丁类,发现android O的类提前加载了,但没有初始化,所以出现了补丁无法生效的问题,对比一下之前版本,发现Android 6.0 执行到指定位置才去加载,并且初始化,因此想法去证实这个问题。
一、为什么要注入ClassLoader?
- 观察Android中类的加载时机、类的机制、定为加载问题
- 观察补丁包或者插件加载机制
二、替换方式
默认形式
PathClassloader.parent <- PathClassloader
方案一:
PathClassloader.parent <- Hook ClassLoader <- PathClassloader
这种方案实现比较简单
public class DelegateClassLoader extends PathClassLoader {
private ClassLoader mPathClassLoader;
private DelegateClassLoader(String dexPath,ClassLoader parentClassLoader) {
super(dexPath, parentClassLoader);
mPathClassLoader = getClass().getClassLoader();
}
//PathClassloader.parent <- PathClassloader ====> PathClassloader.parent <- Hook ClassLoader <- PathClassloader
public static synchronized void hook(ClassLoader pathClassLoader) throws NoSuchFieldException, IllegalAccessException {
ClassLoader classLoader = new DelegateClassLoader("",pathClassLoader.getParent());
Field parentField = ClassLoader.class.getDeclaredField("parent");
parentField.setAccessible(true);
parentField.set(pathClassLoader,classLoader);
Thread.currentThread().setContextClassLoader(classLoader);
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
return super.loadClass(name, resolve);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
return super.findClass(name);
}
}
调用方式如下
public class BaseHotfixApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
try {
DelegateClassLoader.hook(base.getClassLoader());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void onCreate() {
super.onCreate();
}
}
方案二:
PathClassloader.parent <- PathClassloader <- Hook ClassLoader
本方案比较局限,但同样比较流行,来自Tinker的方案,本方案是为了解决热修复补丁包在Android N以后版本的混编问题
final class NewClassLoaderInjector {
public static ClassLoader inject(Application app, ClassLoader oldClassLoader, File dexOptDir,
boolean useDLC, List<File> patchedDexes) throws Throwable {
final String[] patchedDexPaths = new String[patchedDexes.size()];
for (int i = 0; i < patchedDexPaths.length; ++i) {
patchedDexPaths[i] = patchedDexes.get(i).getAbsolutePath();
}
final ClassLoader newClassLoader = createNewClassLoader(oldClassLoader,
dexOptDir, useDLC, patchedDexPaths);
doInject(app, newClassLoader);
return newClassLoader;
}
public static void triggerDex2Oat(Context context, File dexOptDir, boolean useDLC,
String... dexPaths) throws Throwable {
final ClassLoader triggerClassLoader = createNewClassLoader(context.getClassLoader(), dexOptDir, useDLC, dexPaths);
}
@SuppressWarnings("unchecked")
private static ClassLoader createNewClassLoader(ClassLoader oldClassLoader,
File dexOptDir,
boolean useDLC,
String... patchDexPaths) throws Throwable {
final Field pathListField = findField(
Class.forName("dalvik.system.BaseDexClassLoader", false, oldClassLoader),
"pathList");
final Object oldPathList = pathListField.get(oldClassLoader);
final StringBuilder dexPathBuilder = new StringBuilder();
final boolean hasPatchDexPaths = patchDexPaths != null && patchDexPaths.length > 0;
if (hasPatchDexPaths) {
for (int i = 0; i < patchDexPaths.length; ++i) {
if (i > 0) {
dexPathBuilder.append(File.pathSeparator);
}
dexPathBuilder.append(patchDexPaths[i]);
}
}
final String combinedDexPath = dexPathBuilder.toString();
final Field nativeLibraryDirectoriesField = findField(oldPathList.getClass(), "nativeLibraryDirectories");
List<File> oldNativeLibraryDirectories = null;
if (nativeLibraryDirectoriesField.getType().isArray()) {
oldNativeLibraryDirectories = Arrays.asList((File[]) nativeLibraryDirectoriesField.get(oldPathList));
} else {
oldNativeLibraryDirectories = (List<File>) nativeLibraryDirectoriesField.get(oldPathList);
}
final StringBuilder libraryPathBuilder = new StringBuilder();
boolean isFirstItem = true;
for (File libDir : oldNativeLibraryDirectories) {
if (libDir == null) {
continue;
}
if (isFirstItem) {
isFirstItem = false;
} else {
libraryPathBuilder.append(File.pathSeparator);
}
libraryPathBuilder.append(libDir.getAbsolutePath());
}
final String combinedLibraryPath = libraryPathBuilder.toString();
ClassLoader result = null;
if (useDLC && Build.VERSION.SDK_INT >= 27) {
result = new DelegateLastClassLoader(combinedDexPath, combinedLibraryPath, ClassLoader.getSystemClassLoader());
final Field parentField = ClassLoader.class.getDeclaredField("parent");
parentField.setAccessible(true);
parentField.set(result, oldClassLoader);
} else {
result = new TinkerClassLoader(combinedDexPath, dexOptDir, combinedLibraryPath, oldClassLoader);
}
// 'EnsureSameClassLoader' mechanism which is first introduced in Android O
// may cause exception if we replace definingContext of old classloader.
if (Build.VERSION.SDK_INT < 26) {
findField(oldPathList.getClass(), "definingContext").set(oldPathList, result);
}
return result;
}
private static void doInject(Application app, ClassLoader classLoader) throws Throwable {
Thread.currentThread().setContextClassLoader(classLoader);
final Context baseContext = (Context) findField(app.getClass(), "mBase").get(app);
try {
findField(baseContext.getClass(), "mClassLoader").set(baseContext, classLoader);
} catch (Throwable ignored) {
// There's no mClassLoader field in ContextImpl before Android O.
// However we should try our best to replace this field in case some
// customized system has one.
}
final Object basePackageInfo = findField(baseContext.getClass(), "mPackageInfo").get(baseContext);
findField(basePackageInfo.getClass(), "mClassLoader").set(basePackageInfo, classLoader);
if (Build.VERSION.SDK_INT < 27) {
final Resources res = app.getResources();
try {
findField(res.getClass(), "mClassLoader").set(res, classLoader);
final Object drawableInflater = findField(res.getClass(), "mDrawableInflater").get(res);
if (drawableInflater != null) {
findField(drawableInflater.getClass(), "mClassLoader").set(drawableInflater, classLoader);
}
} catch (Throwable ignored) {
// Ignored.
}
}
}
private static Field findField(Class<?> clazz, String name) throws Throwable {
Class<?> currClazz = clazz;
while (true) {
try {
final Field result = currClazz.getDeclaredField(name);
result.setAccessible(true);
return result;
} catch (Throwable ignored) {
if (currClazz == Object.class) {
throw new NoSuchFieldException("Cannot find field "
+ name + " in class " + clazz.getName() + " and its super classes.");
} else {
currClazz = currClazz.getSuperclass();
}
}
}
}
private NewClassLoaderInjector() {
throw new UnsupportedOperationException();
}
}
ClassLoader定义如下
@Keep
@SuppressLint("NewApi")
public final class TinkerClassLoader extends BaseDexClassLoader {
private final ClassLoader mOriginAppClassLoader;
TinkerClassLoader(String dexPath, File optimizedDir, String libraryPath, ClassLoader originAppClassLoader) {
super(dexPath, optimizedDir, libraryPath, ClassLoader.getSystemClassLoader());
mOriginAppClassLoader = originAppClassLoader;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
Class<?> cl = null;
try {
cl = super.findClass(name);
} catch (ClassNotFoundException ignored) {
cl = null;
}
if (cl != null) {
return cl;
} else {
return mOriginAppClassLoader.loadClass(name);
}
}
@Override
public URL getResource(String name) {
// The lookup order we use here is the same as for classes.
URL resource = Object.class.getClassLoader().getResource(name);
if (resource != null) {
return resource;
}
resource = findResource(name);
if (resource != null) {
return resource;
}
return mOriginAppClassLoader.getResource(name);
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
@SuppressWarnings("unchecked")
final Enumeration<URL>[] resources = (Enumeration<URL>[]) new Enumeration<?>[] {
Object.class.getClassLoader().getResources(name),
findResources(name),
mOriginAppClassLoader.getResources(name)
};
return new CompoundEnumeration<>(resources);
}
@Keep
class CompoundEnumeration<E> implements Enumeration<E> {
private Enumeration<E>[] enums;
private int index = 0;
public CompoundEnumeration(Enumeration<E>[] enums) {
this.enums = enums;
}
@Override
public boolean hasMoreElements() {
while (index < enums.length) {
if (enums[index] != null && enums[index].hasMoreElements()) {
return true;
}
index++;
}
return false;
}
@Override
public E nextElement() {
if (!hasMoreElements()) {
throw new NoSuchElementException();
}
return enums[index].nextElement();
}
}
}