Search in sources :

Example 6 with DexClassLoader

use of dalvik.system.DexClassLoader in project AndroidDynamicLoader by mmin18.

the class FragmentLoader method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    if ("com.dianping.intent.action.LOAD_FRAGMENT".equals(getIntent().getAction())) {
        // we need to setup environment before super.onCreate
        try {
            String path = getIntent().getStringExtra("path");
            InputStream ins = MyApplication.instance().getAssets().open(path);
            byte[] bytes = new byte[ins.available()];
            ins.read(bytes);
            ins.close();
            File f = new File(MyApplication.instance().getFilesDir(), "dex");
            f.mkdir();
            f = new File(f, "FL_" + Integer.toHexString(path.hashCode()) + ".apk");
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(bytes);
            fos.close();
            File fo = new File(MyApplication.instance().getFilesDir(), "dexout");
            fo.mkdir();
            DexClassLoader dcl = new DexClassLoader(f.getAbsolutePath(), fo.getAbsolutePath(), null, super.getClassLoader());
            cl = dcl;
            try {
                AssetManager am = (AssetManager) AssetManager.class.newInstance();
                am.getClass().getMethod("addAssetPath", String.class).invoke(am, f.getAbsolutePath());
                asm = am;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            Resources superRes = super.getResources();
            res = new Resources(asm, superRes.getDisplayMetrics(), superRes.getConfiguration());
            thm = res.newTheme();
            thm.setTo(super.getTheme());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    super.onCreate(savedInstanceState);
    FrameLayout rootView = new FrameLayout(this);
    rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    rootView.setId(android.R.id.primary);
    setContentView(rootView);
    if (savedInstanceState != null)
        return;
    if ("com.dianping.intent.action.LOAD_FRAGMENT".equals(getIntent().getAction())) {
        try {
            String fragmentClass = getIntent().getStringExtra("class");
            Fragment f = (Fragment) getClassLoader().loadClass(fragmentClass).newInstance();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(android.R.id.primary, f);
            ft.commit();
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    } else {
        Fragment f = new ListApkFragment();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(android.R.id.primary, f);
        ft.commit();
    }
}
Also used : AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream) ViewGroup(android.view.ViewGroup) DexClassLoader(dalvik.system.DexClassLoader) Fragment(android.app.Fragment) FragmentManager(android.app.FragmentManager) FragmentTransaction(android.app.FragmentTransaction) FileOutputStream(java.io.FileOutputStream) FrameLayout(android.widget.FrameLayout) Resources(android.content.res.Resources) File(java.io.File)

Example 7 with DexClassLoader

use of dalvik.system.DexClassLoader in project AndroidDynamicLoader by mmin18.

the class ActivityLoader method onListItemClick.

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    if (position == 0) {
        Intent i = new Intent("com.dianping.intent.action.SAMPLE_ACTIVITY");
        startActivity(i);
        return;
    }
    if (position == 1) {
        MyApplication.CUSTOM_LOADER = null;
        return;
    }
    Map<String, String> item = data.get(position);
    String title = item.get("title");
    String path = item.get("path");
    try {
        File dex = getDir("dex", Context.MODE_PRIVATE);
        dex.mkdir();
        File f = new File(dex, title);
        InputStream fis = getAssets().open(path);
        FileOutputStream fos = new FileOutputStream(f);
        byte[] buffer = new byte[0xFF];
        int len;
        while ((len = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        fis.close();
        fos.close();
        File fo = getDir("outdex", Context.MODE_PRIVATE);
        fo.mkdir();
        DexClassLoader dcl = new DexClassLoader(f.getAbsolutePath(), fo.getAbsolutePath(), null, MyApplication.ORIGINAL_LOADER.getParent());
        MyApplication.CUSTOM_LOADER = dcl;
        Toast.makeText(this, title + " loaded, try launch again", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(this, "Unable to load " + title, Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        MyApplication.CUSTOM_LOADER = null;
    }
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) DexClassLoader(dalvik.system.DexClassLoader) Intent(android.content.Intent) File(java.io.File)

Example 8 with DexClassLoader

use of dalvik.system.DexClassLoader in project dynamic-load-apk by singwhatiwanna.

the class DLPluginManager method createDexClassLoader.

private DexClassLoader createDexClassLoader(String dexPath) {
    File dexOutputDir = mContext.getDir("dex", Context.MODE_PRIVATE);
    dexOutputPath = dexOutputDir.getAbsolutePath();
    DexClassLoader loader = new DexClassLoader(dexPath, dexOutputPath, mNativeLibDir, mContext.getClassLoader());
    return loader;
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) File(java.io.File)

Example 9 with DexClassLoader

use of dalvik.system.DexClassLoader in project freeline by alibaba.

the class DexUtils method inject.

public static boolean inject(PathClassLoader classLoader, File dex, File opt) {
    Log.i(TAG, dex.getAbsolutePath() + " dex length: " + dex.length());
    Log.i(TAG, opt.getAbsolutePath() + " opt length: " + opt.length());
    DexFile[] dexFiles = null;
    Field pathListField = null;
    Field fDexElements = null;
    Object dstObject = null;
    try {
        Object newDexElements;
        int dexLength;
        if (VERSION.SDK_INT >= 14) {
            pathListField = ReflectUtil.fieldGetOrg(classLoader, Class.forName("dalvik.system.BaseDexClassLoader"), "pathList");
            fDexElements = ReflectUtil.fieldGetOrg(pathListField.get(classLoader), "dexElements");
            Object e = fDexElements.get(pathListField.get(classLoader));
            dstObject = e;
            dexFiles = new DexFile[Array.getLength(e)];
            for (int i = 0; i < Array.getLength(e); ++i) {
                newDexElements = Array.get(e, i);
                dexFiles[i] = (DexFile) ReflectUtil.fieldGet(newDexElements, "dexFile");
            }
        } else {
            pathListField = ReflectUtil.fieldGetOrg(classLoader, "mDexs");
            dstObject = pathListField.get(classLoader);
            dexFiles = new DexFile[Array.getLength(dstObject)];
            for (dexLength = 0; dexLength < Array.getLength(dstObject); ++dexLength) {
                dexFiles[dexLength] = (DexFile) Array.get(dstObject, dexLength);
            }
        }
        dexLength = Array.getLength(dstObject) + 1;
        newDexElements = Array.newInstance(fDexElements.getType().getComponentType(), dexLength);
        DexClassLoader dynamicDex = new DexClassLoader(dex.getAbsolutePath(), opt.getAbsolutePath(), null, classLoader.getParent());
        Log.i(TAG, "after opt, dex len:" + dex.length() + "; opt len:" + opt.length());
        Object pathList = pathListField.get(dynamicDex);
        Object dexElements = fDexElements.get(pathList);
        Object firstDexElement = Array.get(dexElements, 0);
        Array.set(newDexElements, 0, firstDexElement);
        for (int i = 0; i < dexLength - 1; ++i) {
            Object element = Array.get(dstObject, i);
            Array.set(newDexElements, i + 1, element);
        }
        if (VERSION.SDK_INT >= 14) {
            fDexElements.set(pathListField.get(classLoader), newDexElements);
        } else {
            pathListField.set(classLoader, newDexElements);
        }
        return true;
    } catch (Exception e) {
        Log.e(TAG, "fail to override classloader " + classLoader + " with " + dex.getAbsolutePath(), e);
        return false;
    }
}
Also used : Field(java.lang.reflect.Field) DexClassLoader(dalvik.system.DexClassLoader) DexFile(dalvik.system.DexFile)

Example 10 with DexClassLoader

use of dalvik.system.DexClassLoader in project fino by sysdream.

the class InspectionStub method loadMacro.

/**
     * @see IInspectionService.loadMacro
     */
public int loadMacro(final String name, final String dex) throws RemoteException {
    /*
	 * Then try and load the class
	 */
    try {
        String tmpname = UUID.randomUUID().toString() + ".jar";
        final File internal = new File(this.context.getDir("dex", Context.MODE_PRIVATE), tmpname);
        final File optimized = this.context.getDir("outdex", Context.MODE_PRIVATE);
        final FileOutputStream fos = new FileOutputStream(internal);
        fos.write(Base64.decode(dex, Base64.DEFAULT));
        fos.close();
        DexClassLoader loader = new DexClassLoader(internal.getAbsolutePath(), optimized.getAbsolutePath(), null, this.context.getClassLoader());
        Class clazz = loader.loadClass(name);
        return pushObject(clazz);
    } catch (Exception e) {
        //TODO debug
        e.printStackTrace();
        return -1;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) DexClassLoader(dalvik.system.DexClassLoader) File(java.io.File) RemoteException(android.os.RemoteException)

Aggregations

DexClassLoader (dalvik.system.DexClassLoader)19 File (java.io.File)7 DexFile (dalvik.system.DexFile)4 Resources (android.content.res.Resources)3 RemoteException (android.os.RemoteException)3 FileOutputStream (java.io.FileOutputStream)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 ContextWrapper (android.content.ContextWrapper)2 Intent (android.content.Intent)2 AssetManager (android.content.res.AssetManager)2 PathClassLoader (dalvik.system.PathClassLoader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Field (java.lang.reflect.Field)2 ZipFile (java.util.zip.ZipFile)2 TargetApi (android.annotation.TargetApi)1 Fragment (android.app.Fragment)1 FragmentManager (android.app.FragmentManager)1 FragmentTransaction (android.app.FragmentTransaction)1 Context (android.content.Context)1