Skip to content

Commit

Permalink
Provide a choice for developer to decide to use which mode to load sp…
Browse files Browse the repository at this point in the history
…lit, including single and multiple class loader.
  • Loading branch information
kissonchan committed Oct 12, 2019
1 parent 120a309 commit ff12409
Show file tree
Hide file tree
Showing 24 changed files with 1,046 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.iqiyi.android.qigsaw.core.SplitConfiguration;
import com.iqiyi.android.qigsaw.core.Qigsaw;
import com.iqiyi.android.qigsaw.core.splitload.SplitLoad;
import com.iqiyi.qigsaw.sample.downloader.SampleDownloader;
import com.iqiyi.qigsaw.sample.reporter.SampleLogger;
import com.iqiyi.qigsaw.sample.reporter.SampleSplitInstallReporter;
Expand All @@ -22,6 +23,7 @@ protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(base);
SplitConfiguration configuration = SplitConfiguration.newBuilder()
.splitLoadMode(SplitLoad.MULTIPLE_CLASSLOADER)
.forbiddenWorkProcesses(forbiddenWorkProcesses)
.logger(new SampleLogger())
.loadReporter(new SampleSplitLoadReporter(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ private void onBaseContextAttached() {
SplitUpdateReporterManager.install(splitConfiguration.updateReporter == null ? new DefaultSplitUpdateReporter(context) : splitConfiguration.updateReporter);
//init SplitLoadManager and hook PatchCLassLoader.
boolean qigsawMode = SplitBaseInfoProvider.isQigsawAssembleMode();
SplitLoadManagerService.install(context, currentProcessName, qigsawMode, splitConfiguration.forbiddenWorkProcesses);
SplitLoadManagerService.install(context, currentProcessName, splitConfiguration.splitLoadMode, qigsawMode, splitConfiguration.forbiddenWorkProcesses);
SplitLoadManagerService.getInstance().injectPathClassloader();
AABExtension.getInstance().createAndActiveSplitApplication(context, qigsawMode);
SplitCompat.install(context);
}

private void onCreated() {
AABExtension.getInstance().onApplicationCreate();
SplitLoadManagerService.getInstance().loadInstalledSplitsInitially();
SplitLoadManagerService.getInstance().loadInstalledSplitsWhenAppLaunches();
//only work in main process!
if (context.getPackageName().equals(currentProcessName)) {
SplitApkInstaller.install(context, downloader, splitConfiguration.obtainUserConfirmationDialogClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@
import android.support.annotation.NonNull;

import com.iqiyi.android.qigsaw.core.common.SplitLog;
import com.iqiyi.android.qigsaw.core.splitload.SplitLoad;
import com.iqiyi.android.qigsaw.core.splitreport.SplitInstallReporter;
import com.iqiyi.android.qigsaw.core.splitreport.SplitLoadReporter;
import com.iqiyi.android.qigsaw.core.splitreport.SplitUpdateReporter;

public class SplitConfiguration {

/**
* You can decide to use single or multiple class loader mode to load splits, see {@link SplitLoad} to know more details.
*/
final int splitLoadMode;

/**
* processes which are forbidden to load all installed splits during application launch time, if null all processes would load.
*/
Expand Down Expand Up @@ -69,6 +75,7 @@ public static SplitConfiguration.Builder newBuilder() {
}

private SplitConfiguration(Builder builder) {
this.splitLoadMode = builder.splitLoadMode;
this.forbiddenWorkProcesses = builder.forbiddenWorkProcesses;
this.manifestPackageName = builder.manifestPackageName;
this.installReporter = builder.installReporter;
Expand All @@ -79,6 +86,8 @@ private SplitConfiguration(Builder builder) {

public static class Builder {

private int splitLoadMode = SplitLoad.MULTIPLE_CLASSLOADER;

private String[] forbiddenWorkProcesses;

private String manifestPackageName;
Expand All @@ -103,6 +112,11 @@ public Builder logger(@NonNull SplitLog.Logger logger) {
return this;
}

public Builder splitLoadMode(@SplitLoad.SplitLoadMode int splitLoadMode) {
this.splitLoadMode = splitLoadMode;
return this;
}

public Builder forbiddenWorkProcesses(@NonNull String[] forbiddenWorkProcesses) {
this.forbiddenWorkProcesses = forbiddenWorkProcesses;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.iqiyi.android.qigsaw.core.common.SplitLog;
import com.iqiyi.android.qigsaw.core.splitload.SplitApplicationLoaders;
import com.iqiyi.android.qigsaw.core.splitload.SplitDexClassLoader;
import com.iqiyi.android.qigsaw.core.splitload.SplitLoad;
import com.iqiyi.android.qigsaw.core.splitload.SplitLoadManagerService;
import com.iqiyi.android.qigsaw.core.splitreport.SplitInstallError;
import com.iqiyi.android.qigsaw.core.splitrequest.splitinfo.SplitInfo;
import com.iqiyi.android.qigsaw.core.splitrequest.splitinfo.SplitInfoManager;
Expand Down Expand Up @@ -86,7 +88,9 @@ public InstallResult install(SplitInfo info) throws InstallException {
SplitPathManager.require().getSplitOptDir(info),
splitLibDir
);
SplitApplicationLoaders.getInstance().addClassLoader(dexClassLoader);
if (SplitLoadManagerService.getInstance().splitLoadMode() == SplitLoad.MULTIPLE_CLASSLOADER) {
SplitApplicationLoaders.getInstance().addClassLoader(dexClassLoader);
}
createInstalledMark(info);
return new InstallResult(info.getSplitName(), sourceApk, addedDexPaths, checkDependenciesInstalledStatus(info));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
* MIT License
*
* Copyright (c) 2019-present, iQIYI, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.iqiyi.android.qigsaw.core.splitload;

import android.support.annotation.Nullable;
Expand Down
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);
}
}

}
Loading

0 comments on commit ff12409

Please sign in to comment.