Example usage for android.app Application getClassLoader

List of usage examples for android.app Application getClassLoader

Introduction

In this page you can find the example usage for android.app Application getClassLoader.

Prototype

@Override
    public ClassLoader getClassLoader() 

Source Link

Usage

From source file:net.wequick.small.ApkBundleLauncher.java

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

    if (sLoadedApks == null) {
        Log.e(TAG, "Could not find any APK bundles!");
        return;/*from w ww.j a  va2s  .c om*/
    }

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

    // Merge all the resources in bundles and replace the host one
    final Application app = Small.getContext();
    String[] paths = new String[apks.size() + 1];
    paths[0] = app.getPackageResourcePath(); // add host asset path
    int i = 1;
    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.mergeResources(app, sActivityThread, paths);

    // Merge all the dex into host's class loader
    ClassLoader cl = app.getClassLoader();
    i = 0;
    int N = apks.size();
    String[] dexPaths = new String[N];
    DexFile[] dexFiles = new DexFile[N];
    for (LoadedApk apk : apks) {
        dexPaths[i] = apk.path;
        dexFiles[i] = apk.dexFile;
        if (Small.getBundleUpgraded(apk.packageName)) {
            // If upgraded, delete the opt dex file for recreating
            if (apk.optDexFile.exists())
                apk.optDexFile.delete();
            Small.setBundleUpgraded(apk.packageName, false);
        }
        i++;
    }
    ReflectAccelerator.expandDexPathList(cl, dexPaths, dexFiles);

    // Expand the native library directories for host class loader if plugin has any JNIs. (#79)
    List<File> libPathList = new ArrayList<File>();
    for (LoadedApk apk : apks) {
        if (apk.libraryPath != null) {
            libPathList.add(apk.libraryPath);
        }
    }
    if (libPathList.size() > 0) {
        ReflectAccelerator.expandNativeLibraryDirectories(cl, libPathList);
    }

    // Trigger all the bundle application `onCreate' event
    for (final LoadedApk apk : apks) {
        String bundleApplicationName = apk.applicationName;
        if (bundleApplicationName == null)
            continue;

        try {
            final Class applicationClass = Class.forName(bundleApplicationName);
            Bundle.postUI(new Runnable() {
                @Override
                public void run() {
                    try {
                        BundleApplicationContext appContext = new BundleApplicationContext(app, apk);
                        Application bundleApplication = Instrumentation.newApplication(applicationClass,
                                appContext);
                        sHostInstrumentation.callApplicationOnCreate(bundleApplication);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Lazy init content providers
    if (mLazyInitProviders != null) {
        try {
            Method m = sActivityThread.getClass().getDeclaredMethod("installContentProviders", Context.class,
                    List.class);
            m.setAccessible(true);
            m.invoke(sActivityThread, app, mLazyInitProviders);
        } catch (Exception e) {
            throw new RuntimeException("Failed to lazy init content providers: " + mLazyInitProviders);
        }
    }

    // Free temporary variables
    sLoadedApks = null;
    sProviders = null;
    sActivityThread = null;
}