-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a choice for developer to decide to use which mode to load sp…
…lit, including single and multiple class loader.
- Loading branch information
1 parent
120a309
commit ff12409
Showing
24 changed files
with
1,046 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...loader/src/main/java/com/iqiyi/android/qigsaw/core/splitload/SplitApplicationLoaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
...litloader/src/main/java/com/iqiyi/android/qigsaw/core/splitload/SplitCompatDexLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
package com.iqiyi.android.qigsaw.core.splitload; | ||
|
||
import android.os.Build; | ||
|
||
import com.iqiyi.android.qigsaw.core.common.SplitLog; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Source code from Tinker | ||
*/ | ||
final class SplitCompatDexLoader { | ||
|
||
private static final String TAG = "SplitCompatDexLoader"; | ||
|
||
private static int sPatchDexCount = 0; | ||
|
||
static void load(ClassLoader classLoader, File dexOptDir, List<File> files) | ||
throws Throwable { | ||
if (!files.isEmpty()) { | ||
if (Build.VERSION.SDK_INT >= 23) { | ||
V23.load(classLoader, files, dexOptDir); | ||
} else if (Build.VERSION.SDK_INT >= 19) { | ||
V19.load(classLoader, files, dexOptDir); | ||
} else if (Build.VERSION.SDK_INT >= 14) { | ||
V14.load(classLoader, files, dexOptDir); | ||
} else { | ||
throw new UnsupportedOperationException("don't support under SDK version 14!"); | ||
} | ||
sPatchDexCount = files.size(); | ||
} | ||
} | ||
|
||
static void unLoad(ClassLoader classLoader) throws Throwable { | ||
if (sPatchDexCount <= 0) { | ||
return; | ||
} | ||
if (Build.VERSION.SDK_INT >= 14) { | ||
Field pathListField = HiddenApiReflection.findField(classLoader, "pathList"); | ||
Object dexPathList = pathListField.get(classLoader); | ||
HiddenApiReflection.reduceFieldArray(dexPathList, "dexElements", sPatchDexCount); | ||
} else { | ||
throw new RuntimeException("don't support under SDK version 14!"); | ||
} | ||
} | ||
|
||
/** | ||
* Installer for platform versions 19. | ||
*/ | ||
private static final class V23 { | ||
|
||
private static void load(ClassLoader loader, List<File> additionalClassPathEntries, | ||
File optimizedDirectory) | ||
throws IllegalArgumentException, IllegalAccessException, | ||
NoSuchFieldException, InvocationTargetException, NoSuchMethodException, IOException { | ||
/* The patched class loader is expected to be a descendant of | ||
* dalvik.system.BaseDexClassLoader. We modify its | ||
* dalvik.system.DexPathList pathList field to append additional DEX | ||
* file entries. | ||
*/ | ||
Field pathListField = HiddenApiReflection.findField(loader, "pathList"); | ||
Object dexPathList = pathListField.get(loader); | ||
ArrayList<IOException> suppressedExceptions = new ArrayList<>(); | ||
HiddenApiReflection.expandFieldArray(dexPathList, "dexElements", makePathElements(dexPathList, | ||
new ArrayList<>(additionalClassPathEntries), optimizedDirectory, | ||
suppressedExceptions)); | ||
if (suppressedExceptions.size() > 0) { | ||
for (IOException e : suppressedExceptions) { | ||
SplitLog.e(TAG, "Exception in makePathElement", e); | ||
throw e; | ||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* A wrapper around | ||
* {@code private static final dalvik.system.DexPathList#makePathElements}. | ||
*/ | ||
private static Object[] makePathElements( | ||
Object dexPathList, ArrayList<File> files, File optimizedDirectory, | ||
ArrayList<IOException> suppressedExceptions) | ||
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { | ||
|
||
Method makePathElements; | ||
try { | ||
makePathElements = HiddenApiReflection.findMethod(dexPathList, "makePathElements", List.class, File.class, | ||
List.class); | ||
} catch (NoSuchMethodException e) { | ||
SplitLog.e(TAG, "NoSuchMethodException: makePathElements(List,File,List) failure"); | ||
try { | ||
makePathElements = HiddenApiReflection.findMethod(dexPathList, "makePathElements", ArrayList.class, File.class, ArrayList.class); | ||
} catch (NoSuchMethodException e1) { | ||
SplitLog.e(TAG, "NoSuchMethodException: makeDexElements(ArrayList,File,ArrayList) failure"); | ||
try { | ||
SplitLog.w(TAG, "NoSuchMethodException: try use v19 instead"); | ||
return V19.makeDexElements(dexPathList, files, optimizedDirectory, suppressedExceptions); | ||
} catch (NoSuchMethodException e2) { | ||
SplitLog.e(TAG, "NoSuchMethodException: makeDexElements(List,File,List) failure"); | ||
throw e2; | ||
} | ||
} | ||
} | ||
|
||
return (Object[]) makePathElements.invoke(dexPathList, files, optimizedDirectory, suppressedExceptions); | ||
} | ||
} | ||
|
||
/** | ||
* Installer for platform versions 19. | ||
*/ | ||
private static final class V19 { | ||
|
||
private static void load(ClassLoader loader, List<File> additionalClassPathEntries, | ||
File optimizedDirectory) | ||
throws IllegalArgumentException, IllegalAccessException, | ||
NoSuchFieldException, InvocationTargetException, NoSuchMethodException, IOException { | ||
/* The patched class loader is expected to be a descendant of | ||
* dalvik.system.BaseDexClassLoader. We modify its | ||
* dalvik.system.DexPathList pathList field to append additional DEX | ||
* file entries. | ||
*/ | ||
Field pathListField = HiddenApiReflection.findField(loader, "pathList"); | ||
Object dexPathList = pathListField.get(loader); | ||
ArrayList<IOException> suppressedExceptions = new ArrayList<>(); | ||
HiddenApiReflection.expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList, | ||
new ArrayList<>(additionalClassPathEntries), optimizedDirectory, | ||
suppressedExceptions)); | ||
if (suppressedExceptions.size() > 0) { | ||
for (IOException e : suppressedExceptions) { | ||
SplitLog.e(TAG, "Exception in makeDexElement", e); | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A wrapper around | ||
* {@code private static final dalvik.system.DexPathList#makeDexElements}. | ||
*/ | ||
private static Object[] makeDexElements( | ||
Object dexPathList, ArrayList<File> files, File optimizedDirectory, | ||
ArrayList<IOException> suppressedExceptions) | ||
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { | ||
|
||
Method makeDexElements; | ||
try { | ||
makeDexElements = HiddenApiReflection.findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class, | ||
ArrayList.class); | ||
} catch (NoSuchMethodException e) { | ||
SplitLog.e(TAG, "NoSuchMethodException: makeDexElements(ArrayList,File,ArrayList) failure"); | ||
try { | ||
makeDexElements = HiddenApiReflection.findMethod(dexPathList, "makeDexElements", List.class, File.class, List.class); | ||
} catch (NoSuchMethodException e1) { | ||
SplitLog.e(TAG, "NoSuchMethodException: makeDexElements(List,File,List) failure"); | ||
throw e1; | ||
} | ||
} | ||
|
||
return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory, suppressedExceptions); | ||
} | ||
} | ||
|
||
/** | ||
* Installer for platform versions 14, 15, 16, 17 and 18. | ||
*/ | ||
private static final class V14 { | ||
|
||
private static void load(ClassLoader loader, List<File> additionalClassPathEntries, | ||
File optimizedDirectory) | ||
throws IllegalArgumentException, IllegalAccessException, | ||
NoSuchFieldException, InvocationTargetException, NoSuchMethodException { | ||
/* The patched class loader is expected to be a descendant of | ||
* dalvik.system.BaseDexClassLoader. We modify its | ||
* dalvik.system.DexPathList pathList field to append additional DEX | ||
* file entries. | ||
*/ | ||
Field pathListField = HiddenApiReflection.findField(loader, "pathList"); | ||
Object dexPathList = pathListField.get(loader); | ||
HiddenApiReflection.expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList, | ||
new ArrayList<>(additionalClassPathEntries), optimizedDirectory)); | ||
} | ||
|
||
/** | ||
* A wrapper around | ||
* {@code private static final dalvik.system.DexPathList#makeDexElements}. | ||
*/ | ||
private static Object[] makeDexElements( | ||
Object dexPathList, ArrayList<File> files, File optimizedDirectory) | ||
throws IllegalAccessException, InvocationTargetException, | ||
NoSuchMethodException { | ||
Method makeDexElements = | ||
HiddenApiReflection.findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class); | ||
return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.