Search in sources :

Example 1 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class DelegateClassLoader method loadFromInstalledBundles.

static Class<?> loadFromInstalledBundles(String className, boolean safe) throws ClassNotFoundException {
    Class<?> clazz = null;
    List<Bundle> bundles = Framework.getBundles();
    String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(className);
    BundleImpl bundle = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
    if (bundle != null) {
        if (!Framework.isDeubgMode()) {
            bundle.optDexFile();
        }
        bundle.startBundle();
        ClassLoader classloader = bundle.getClassLoader();
        try {
            if (classloader != null) {
                clazz = classloader.loadClass(className);
                if (clazz != null) {
                    return clazz;
                }
            }
        } catch (ClassNotFoundException e) {
        }
        if ((Thread.currentThread().getId() == Looper.getMainLooper().getThread().getId())) {
            Throwable ex = new Throwable();
            ex.fillInStackTrace();
            Log.e("MainThreadFindClass", String.format("can not findClass %s from %s in UI thread ", className, bundle));
            ex.printStackTrace();
        }
        if (safe) {
            ComponentName component = new ComponentName(RuntimeVariables.androidApplication.getPackageName(), className);
            if (isProvider(component)) {
                return Atlas.class.getClassLoader().loadClass("android.taobao.atlas.util.FakeProvider");
            } else if (isReceiver(component)) {
                return Atlas.class.getClassLoader().loadClass("android.taobao.atlas.util.FakeReceiver");
            } else {
                throw new ClassNotFoundException("Can't find class " + className + " in BundleClassLoader: " + bundle.getLocation() + " [" + (bundles == null ? 0 : bundles.size()) + "]" + (classloader == null ? "classloader is null" : "classloader not null") + " packageversion " + getPackageVersion() + FileUtils.getAvailableDisk());
            }
        }
    }
    /*
         * The layout may uses the designated classes, in this case,
         * the class not exist in components, we need search all bundles
         * to find the class.
         */
    if (bundles != null && !bundles.isEmpty()) {
        for (Bundle b : Framework.getBundles()) {
            bundle = (BundleImpl) b;
            if (bundle.getArchive().isDexOpted()) {
                ClassLoader classloader = bundle.getClassLoader();
                try {
                    if (classloader != null) {
                        clazz = classloader.loadClass(className);
                        if (clazz != null) {
                            return clazz;
                        }
                    }
                } catch (ClassNotFoundException e) {
                }
            }
        }
    }
    return clazz;
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl) Atlas(android.taobao.atlas.framework.Atlas) Bundle(org.osgi.framework.Bundle) ComponentName(android.content.ComponentName)

Example 2 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class InstrumentationHook method ValidateActivityResource.

private boolean ValidateActivityResource(Activity activity) {
    String exceptionString = null;
    String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(activity.getLocalClassName());
    BundleImpl b = (BundleImpl) Framework.getBundle(bundleName);
    String bundlePath = null;
    if (b != null) {
        bundlePath = b.getArchive().getArchiveFile().getAbsolutePath();
    }
    Resources resource = null;
    if (AtlasHacks.ContextThemeWrapper_mResources != null) {
        resource = AtlasHacks.ContextThemeWrapper_mResources.get(activity);
    } else {
        resource = activity.getResources();
    }
    Resources resource_runtime = RuntimeVariables.delegateResources;
    if (resource == resource_runtime) {
        return true;
    }
    List<String> paths = getAssetPathFromResources(resource);
    String pathsOfHis = DelegateResources.getCurrentAssetpathStr(RuntimeVariables.androidApplication.getAssets());
    List<String> pathsRuntime = getAssetPathFromResources(resource_runtime);
    if ((bundlePath != null) && (paths != null) && !paths.contains(bundlePath)) {
        exceptionString += "(1.1) Activity Resources path not contains:" + b.getArchive().getArchiveFile().getAbsolutePath();
        if (!pathsOfHis.contains(bundlePath)) {
            exceptionString += "(1.2) paths in history not contains:" + b.getArchive().getArchiveFile().getAbsolutePath();
        }
        if (!pathsRuntime.contains(bundlePath)) {
            exceptionString += "(1.3) paths in runtime not contains:" + b.getArchive().getArchiveFile().getAbsolutePath();
        }
        if (b.getArchive().getArchiveFile().exists() == false) {
            exceptionString += "(1.4) Bundle archive file not exist:" + b.getArchive().getArchiveFile().getAbsolutePath();
        }
        exceptionString += "(1.5) Activity Resources paths length:" + paths.size();
    }
    if (exceptionString != null) {
        return false;
    }
    return true;
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl) Resources(android.content.res.Resources)

Example 3 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class InstrumentationHook method onException.

@Override
public boolean onException(Object obj, Throwable e) {
    if (obj instanceof BroadcastReceiver && (obj.getClass().getClassLoader().getClass().getName().equals(BundleClassLoader.class.getName()))) {
        try {
            Field locationField = BundleClassLoader.class.getDeclaredField("location");
            locationField.setAccessible(true);
            String location = (String) locationField.get(obj.getClass().getClassLoader());
            if (location != null) {
                BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(location);
                if (impl != null && !impl.checkValidate()) {
                    e.printStackTrace();
                    return true;
                }
            }
        } catch (Throwable e2) {
        }
    }
    return mBase.onException(obj, e);
}
Also used : Field(java.lang.reflect.Field) BundleImpl(android.taobao.atlas.framework.BundleImpl) BundleClassLoader(android.taobao.atlas.framework.BundleClassLoader) BroadcastReceiver(android.content.BroadcastReceiver)

Example 4 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class BundleArchive method downgradeRevision.

public static boolean downgradeRevision(String location, File bundleDir, boolean forceDelete) throws IOException {
    File[] revisionFiles = bundleDir.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String filename) {
            if (filename.startsWith(REVISION_DIRECTORY) && !new File(dir + File.separator + filename, DEPRECATED_MARK).exists()) {
                return true;
            }
            return false;
        }
    });
    if (revisionFiles != null && revisionFiles.length > 0) {
        File deprecatedDir = revisionFiles[revisionFiles.length - 1];
        if (forceDelete) {
            try {
                BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(location);
                if (impl != null && impl.getArchive().getCurrentRevision() != null) {
                    if (impl.getArchive().getCurrentRevision().getRevisionDir().equals(deprecatedDir)) {
                        return true;
                    }
                }
            } catch (Throwable e) {
            }
            //deprecatedDir.delete();
            Framework.deleteDirectory(deprecatedDir);
        }
        if (deprecatedDir.exists()) {
            File deprecatedFile = new File(deprecatedDir, DEPRECATED_MARK);
            deprecatedFile.createNewFile();
            if (!deprecatedFile.exists()) {
                return false;
            }
        }
        Log.d("BundleArchive", "downgrade " + bundleDir);
        return true;
    }
    return false;
}
Also used : FilenameFilter(java.io.FilenameFilter) BundleImpl(android.taobao.atlas.framework.BundleImpl) File(java.io.File)

Example 5 with BundleImpl

use of android.taobao.atlas.framework.BundleImpl in project atlas by alibaba.

the class ActivityThreadHook method handleService.

private void handleService(final Message message) throws Exception {
    Class ReceiverData = Class.forName("android.app.ActivityThread$CreateServiceData");
    final Field info_field = ReceiverData.getDeclaredField("info");
    info_field.setAccessible(true);
    final ServiceInfo info = (ServiceInfo) info_field.get(message.obj);
    String componentName = info.name;
    String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(componentName);
    if (!TextUtils.isEmpty(bundleName)) {
        BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
        if (impl != null && impl.checkValidate()) {
            mActivityThreadHandler.handleMessage(message);
            if (sDelayServiceMessageList != null) {
                sDelayServiceMessageList.remove(message);
            }
            executeDelayMsg();
        } else {
            if (sDelayServiceMessageList == null) {
                sDelayServiceMessageList = new ArrayList<Message>();
                sDelayServiceMessageList.add(Message.obtain(message));
            }
            BundleUtil.checkBundleStateAsync(bundleName, new Runnable() {

                @Override
                public void run() {
                    try {
                        executeDelayMsg();
                    } catch (Throwable e) {
                        throw new RuntimeException(e);
                    }
                }
            }, null);
        }
    } else {
        mActivityThreadHandler.handleMessage(message);
        if (sDelayServiceMessageList != null) {
            sDelayServiceMessageList.remove(message);
        }
        executeDelayMsg();
    }
}
Also used : ServiceInfo(android.content.pm.ServiceInfo) Field(java.lang.reflect.Field) BundleImpl(android.taobao.atlas.framework.BundleImpl) Message(android.os.Message)

Aggregations

BundleImpl (android.taobao.atlas.framework.BundleImpl)21 ResolveInfo (android.content.pm.ResolveInfo)5 Bundle (org.osgi.framework.Bundle)5 ArrayList (java.util.ArrayList)4 ComponentName (android.content.ComponentName)3 Resources (android.content.res.Resources)3 BundleClassLoader (android.taobao.atlas.framework.BundleClassLoader)3 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Intent (android.content.Intent)2 Activity (android.app.Activity)1 Application (android.app.Application)1 BroadcastReceiver (android.content.BroadcastReceiver)1 ComponentCallbacks (android.content.ComponentCallbacks)1 ActivityInfo (android.content.pm.ActivityInfo)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 ServiceInfo (android.content.pm.ServiceInfo)1 AssetManager (android.content.res.AssetManager)1 Configuration (android.content.res.Configuration)1 XmlResourceParser (android.content.res.XmlResourceParser)1