Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazyload Function Beta #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Android/DevSample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.android.tools.build:gradle:2.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand All @@ -24,10 +24,11 @@ task clean(type: Delete) {
apply plugin: 'net.wequick.small'

small {
buildToAssets = true
buildToAssets = false
android {
compileSdkVersion = 24
buildToolsVersion = "23.0.3"
supportVersion = "23.4.0"
}
strictSplitResources=false
}
2 changes: 1 addition & 1 deletion Android/DevSample/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
2 changes: 1 addition & 1 deletion Android/DevSample/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def externalModules = [
':app', ':app+stub',
':app.main', ':app.home', ':app.detail', ':app.mine', ':app.ok-if-stub',
':web.about',
':lib.utils', ':lib.style', ':lib.afterutils', ':lib.analytics',
':lib.utils', ':lib.style', ':lib.analytics',
':jni_plugin'
] as String[]

Expand Down
2 changes: 1 addition & 1 deletion Android/DevSample/small/base.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ configurations {
}

dependencies {
// testCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
provided 'com.android.support:support-v4:23.2.1'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
import android.app.Instrumentation;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.ProviderInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.IBinder;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Message;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
Expand Down Expand Up @@ -641,10 +641,41 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}
}

@Override
public void postLazySetUp() {
super.postLazySetUp();
long lStartTime = System.currentTimeMillis();
if (sLoadedApks == null) {
Log.e(TAG, "Could not find LazyLoad APK bundles!");
return;
}

Collection<LoadedApk> apks = sLoadedApks.values();

// Merge lazy load bundle's resources and replace the host one
final Application app = Small.getContext();
String[] paths = new String[apks.size() + 1];
int i = 0;
for (LoadedApk apk : apks) {
if (apk.nonResources) continue; // ignores the empty entry to fix #62
paths[i++] = apk.path; // add plugin asset path
}
if (i != paths.length) {
paths = Arrays.copyOf(paths, i);
}
ReflectAccelerator.addAssetPaths(app.getAssets(), paths);

mergeWithOutAssets(app, apks);
long lEndTime = System.currentTimeMillis();
long difference = lEndTime - lStartTime;
System.out.println("postLazySetUp's Elapsed milliseconds: "
+ difference);
}

@Override
public void postSetUp() {
super.postSetUp();

long lStartTime = System.currentTimeMillis();
if (sLoadedApks == null) {
Log.e(TAG, "Could not find any APK bundles!");
return;
Expand All @@ -666,9 +697,20 @@ public void postSetUp() {
}
ReflectAccelerator.mergeResources(app, sActivityThread, paths);

mergeWithOutAssets(app, apks);
long lEndTime = System.currentTimeMillis();
long difference = lEndTime - lStartTime;
System.out.println("postSetUp's Elapsed milliseconds: "
+ difference);
}

/**
* just put code together
*/
private void mergeWithOutAssets(final Application app, Collection<LoadedApk> apks) {
// Merge all the dex into host's class loader
ClassLoader cl = app.getClassLoader();
i = 0;
int i = 0;
int N = apks.size();
String[] dexPaths = new String[N];
DexFile[] dexFiles = new DexFile[N];
Expand Down Expand Up @@ -733,6 +775,7 @@ public void run() {
}

// Free temporary variables
mLazyInitProviders=null;
sLoadedApks = null;
sProviders = null;
sActivityThread = null;
Expand All @@ -758,7 +801,7 @@ public File getExtractFile(Bundle bundle, String entryName) {
}

@Override
public void loadBundle(Bundle bundle) {
public void loadBundle(final Bundle bundle) {
String packageName = bundle.getPackageName();

BundleParser parser = bundle.getParser();
Expand Down Expand Up @@ -786,7 +829,12 @@ public void loadBundle(Bundle bundle) {
@Override
public void run() {
try {
long lStartTime = System.currentTimeMillis();
fApk.dexFile = DexFile.loadDex(fApk.path, fApk.optDexFile.getPath(), 0);
long lEndTime = System.currentTimeMillis();
long difference = lEndTime - lStartTime;
System.out.println(bundle.getPackageName() + "'s Elapsed milliseconds: "
+ difference);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -807,7 +855,8 @@ public void run() {
}

// Record activities for intent redirection
if (sLoadedActivities == null) sLoadedActivities = new ConcurrentHashMap<String, ActivityInfo>();
if (sLoadedActivities == null)
sLoadedActivities = new ConcurrentHashMap<String, ActivityInfo>();
for (ActivityInfo ai : pluginInfo.activities) {
sLoadedActivities.put(ai.name, ai);
}
Expand Down
115 changes: 107 additions & 8 deletions Android/DevSample/small/src/main/java/net/wequick/small/Bundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
Expand Down Expand Up @@ -88,8 +87,11 @@ private static final class Manifest {

// Thread & Handler
private static final int MSG_COMPLETE = 1;
private static final int MSG_LAZY_LOAD_COMPLETE = 2;
private static LoadBundleHandler sHandler;
private static LazyLoadBundleHandler sLazyHandler;
private static LoadBundleThread sThread;
private static LazyLoadBundleThread sLazyThread;

private String mPackageName;
private String uriString;
Expand All @@ -102,6 +104,7 @@ private static final class Manifest {
private HashMap<String, String> rules;
private int versionCode;
private String versionName;
private boolean isLazyLoad=false;

private BundleLauncher mApplicableLauncher = null;

Expand All @@ -113,6 +116,7 @@ private static final class Manifest {
private boolean launchable = true;
private boolean enabled = true;
private boolean patching = false;
private boolean loaded=false;

private String entrance = null; // Main activity for `apk bundle', index page for `web bundle'

Expand Down Expand Up @@ -220,6 +224,22 @@ public static boolean is64bit() {
return sIs64bit;
}

public static void loadLazyBundles(Small.OnLazyCompleteListener listener){
boolean synchronous = (listener == null);
if (synchronous) {
lazyLoadBundles();
return;
}

// Asynchronous
if (sLazyThread == null) {
sLazyThread = new LazyLoadBundleThread();
sLazyHandler = new LazyLoadBundleHandler(listener);
sLazyThread.start();
}
}


/**
* Load bundles from manifest
*/
Expand Down Expand Up @@ -366,7 +386,7 @@ protected static Bundle getLaunchableBundle(Uri uri) {
if (sPreloadBundles != null) {
for (Bundle bundle : sPreloadBundles) {
if (bundle.matchesRule(uri)) {
if (bundle.mApplicableLauncher == null) {
if (bundle.mApplicableLauncher == null&&!bundle.isLazyLoad()) {
break;
}

Expand Down Expand Up @@ -538,6 +558,9 @@ private void initWithMap(JSONObject map) throws JSONException {
if (map.has("type")) {
this.type = map.getString("type");
}
if (map.has("lazyload")) {
this.isLazyLoad = map.getBoolean("lazyload");
}

this.rules = new HashMap<String, String>();
// Default rules to visit entrance page of bundle
Expand Down Expand Up @@ -621,6 +644,14 @@ protected void setExtractPath(File path) {
this.mExtractPath = path;
}

public boolean isLoaded() {
return loaded;
}

public void setLoaded(boolean loaded) {
this.loaded = loaded;
}

protected String getType() {
return type;
}
Expand Down Expand Up @@ -678,6 +709,14 @@ public String getVersionName() {
return versionName;
}

public boolean isLazyLoad() {
return isLazyLoad;
}

public void setLazyLoad(boolean lazyLoad) {
isLazyLoad = lazyLoad;
}

protected boolean isLaunchable() {
return launchable && enabled;
}
Expand Down Expand Up @@ -748,8 +787,33 @@ public void run() {
}
}

private static class LazyLoadBundleThread extends Thread {
@Override
public void run() {
lazyLoadBundles();
sLazyHandler.obtainMessage(MSG_LAZY_LOAD_COMPLETE).sendToTarget();
}
}

private static final int LOADING_TIMEOUT_MINUTES = 5;

private static List<Bundle> unLoadBundle;

private static void lazyLoadBundles() {
if (unLoadBundle == null) {
unLoadBundle = new ArrayList<>();
}
for (Bundle bundle : sPreloadBundles) {
if (bundle.isLazyLoad()) {
unLoadBundle.add(bundle);
bundle.setLazyLoad(false);
bundle.prepareForLaunch();
}
}

processBundlesLoad(unLoadBundle,true);
}

private static void loadBundles(List<Bundle> bundles) {
sPreloadBundles = bundles;

Expand All @@ -758,6 +822,13 @@ private static void loadBundles(List<Bundle> bundles) {
bundle.prepareForLaunch();
}

processBundlesLoad(bundles,false);
}

/**
* just put code together
*/
private static void processBundlesLoad(List<Bundle> bundles, boolean isLazyLoad){
// Handle I/O
if (sIOActions != null) {
ExecutorService executor = Executors.newFixedThreadPool(sIOActions.size());
Expand Down Expand Up @@ -788,7 +859,11 @@ private static void loadBundles(List<Bundle> bundles) {

// Notify `postSetUp' to all launchers
for (BundleLauncher launcher : sBundleLaunchers) {
launcher.postSetUp();
if(isLazyLoad){
launcher.postLazySetUp();
}else{
launcher.postSetUp();
}
}

// Wait for the things to be done on UI thread after `postSetUp`,
Expand All @@ -803,12 +878,15 @@ private static void loadBundles(List<Bundle> bundles) {

// Free all unused temporary variables
for (Bundle bundle : bundles) {
if (bundle.parser != null) {
bundle.parser.close();
bundle.parser = null;
if(!bundle.isLazyLoad()){
bundle.setLoaded(true);
if (bundle.parser != null) {
bundle.parser.close();
bundle.parser = null;
}
bundle.mBuiltinFile = null;
bundle.mExtractPath = null;
}
bundle.mBuiltinFile = null;
bundle.mExtractPath = null;
}
}

Expand Down Expand Up @@ -868,5 +946,26 @@ public void handleMessage(Message msg) {
}
}
}
private static class LazyLoadBundleHandler extends Handler {
private Small.OnLazyCompleteListener mLazyListener;

public LazyLoadBundleHandler(Small.OnLazyCompleteListener listener) {
mLazyListener = listener;
}

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_LAZY_LOAD_COMPLETE:
if (mLazyListener != null) {
mLazyListener.onComplete();
}
mLazyListener = null;
sLazyThread = null;
sLazyThread = null;
break;
}
}
}

}
Loading