Search in sources :

Example 1 with DelegateClassLoader

use of android.taobao.atlas.runtime.DelegateClassLoader in project atlas by alibaba.

the class AndroidHack method injectResources.

//    public static void injectApplication(String packageName, Application application) throws Exception {
//        Object activityThread = getActivityThread();
//        if (activityThread == null) {
//            throw new Exception("Failed to get ActivityThread.sCurrentActivityThread");
//        }
//
//        Object loadedApk = getLoadedApk(application,activityThread, application.getPackageName());
//        if (loadedApk == null) {
//            throw new Exception("Failed to get ActivityThread.mLoadedApk");
//        }
//
////        try{
////            Field field = activityThread.getClass().getDeclaredField("mAllApplications");
////            field.setAccessible(true);
////            ArrayList<Application> mAllApplications = (ArrayList<Application>)field.get(activityThread);
////            mAllApplications.add(RuntimeVariables.androidApplication);
////        }catch(Throwable e){}
//        AtlasHacks.LoadedApk_mApplication.set(loadedApk,application);
//        AtlasHacks.ActivityThread_mInitialApplication.set(activityThread,application);
//    }
public static void injectResources(Application application, Resources resources) throws Exception {
    Object activityThread = getActivityThread();
    if (activityThread == null) {
        throw new Exception("Failed to get ActivityThread.sCurrentActivityThread");
    }
    Object loadedApk = getLoadedApk(application, activityThread, application.getPackageName());
    if (loadedApk == null) {
        loadedApk = createNewLoadedApk(application, activityThread);
        if (loadedApk == null) {
            throw new RuntimeException(" Failed to get ActivityThread.mLoadedApk");
        }
        ClassLoader classLoader = AtlasHacks.LoadedApk_mClassLoader.get(loadedApk);
        if (!(classLoader instanceof DelegateClassLoader)) {
            AtlasHacks.LoadedApk_mClassLoader.set(loadedApk, RuntimeVariables.delegateClassLoader);
        }
    }
    //AtlasHacks.LoadedApk_mResources.on(loadedApk).set(resources);
    AtlasHacks.LoadedApk_mResources.set(loadedApk, resources);
    //AtlasHacks.ContextImpl_mResources.on(application.getBaseContext()).set(resources);
    AtlasHacks.ContextImpl_mResources.set(application.getBaseContext(), resources);
    //AtlasHacks.ContextImpl_mTheme.on(application.getBaseContext()).set(null);
    AtlasHacks.ContextImpl_mTheme.set(application.getBaseContext(), null);
    try {
        Collection<WeakReference<Resources>> references = null;
        if (Build.VERSION.SDK_INT <= 18) {
            HashMap<?, WeakReference<Resources>> map = (HashMap<?, WeakReference<Resources>>) sActiveResourcesField.get(activityThread);
            references = map.values();
        } else if (Build.VERSION.SDK_INT < 24) {
            Object sResourcesManager = sgetInstanceMethod.invoke(sResourcesManagerClazz);
            ArrayMap<?, WeakReference<Resources>> activeResources = (ArrayMap<?, WeakReference<Resources>>) sActiveResourcesField.get(sResourcesManager);
            references = activeResources.values();
        }
        if (Build.VERSION.SDK_INT < 24) {
            for (WeakReference<Resources> wr : references) {
                Resources res = wr.get();
                if (res != null) {
                    sAssetsField.set(res, resources.getAssets());
                    res.updateConfiguration(resources.getConfiguration(), resources.getDisplayMetrics());
                }
            }
        }
    //            if(Build.VERSION.SDK_INT>=24){
    //                Object sResourcesManager = sgetInstanceMethod.invoke(sResourcesManagerClazz);
    //                WeakHashMap<IBinder, Object> activityResourceReferences ;
    //                activityResourceReferences= (WeakHashMap<IBinder, Object> )sActiveResourcesField.get(sResourcesManager);
    //                Collection<Object> mActivityResourcesReferences = activityResourceReferences.values();
    //                if(mActivityResourcesReferences!=null){
    //                    for(Object activityResourceReference : mActivityResourcesReferences){
    //                        ArrayList<WeakReference<Resources>> resList= (ArrayList<WeakReference<Resources>>)activityResourceReference.getClass().getDeclaredField("activityResources")
    //                                .get(activityResourceReference);
    //                        if(resList!=null){
    //                            for(WeakReference<Resources> ref : resList){
    //
    //                            }
    //                        }
    //                    }
    //                }
    //
    //
    //            }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ArrayMap(android.util.ArrayMap) WeakReference(java.lang.ref.WeakReference) DelegateClassLoader(android.taobao.atlas.runtime.DelegateClassLoader) DelegateClassLoader(android.taobao.atlas.runtime.DelegateClassLoader) DelegateResources(android.taobao.atlas.runtime.DelegateResources) Resources(android.content.res.Resources)

Example 2 with DelegateClassLoader

use of android.taobao.atlas.runtime.DelegateClassLoader in project atlas by alibaba.

the class Atlas method init.

/**
     * Init the framework
     * 
     * @param application
     * @return
     * @throws Exception
     */
public void init(Application application, boolean reset) throws AssertionArrayException, Exception {
    if (application == null) {
        throw new RuntimeException("application is null");
    }
    ApplicationInfo app_info = application.getApplicationInfo();
    sAPKSource = app_info.sourceDir;
    boolean DEBUG = (app_info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    RuntimeVariables.androidApplication = application;
    RuntimeVariables.delegateResources = application.getResources();
    Framework.containerVersion = RuntimeVariables.sInstalledVersionName;
    ClassLoader cl = Atlas.class.getClassLoader();
    Framework.systemClassLoader = cl;
    // defineAndVerify
    String packageName = application.getPackageName();
    // 
    DelegateClassLoader newClassLoader = new DelegateClassLoader(cl);
    // init RuntimeVariables
    RuntimeVariables.delegateClassLoader = newClassLoader;
    //        try{
    //        	RuntimeVariables.delegateResources = getResources(application);
    //        }catch(Throwable e){
    //            e.printStackTrace();
    //        }
    // inject DelegateClassLoader & DelegateResources & InstrumentationHook
    AndroidHack.injectClassLoader(packageName, newClassLoader);
    AndroidHack.injectInstrumentationHook(new InstrumentationHook(AndroidHack.getInstrumentation(), application.getBaseContext()));
    // add listeners
    bundleLifecycleHandler = new BundleLifecycleHandler();
    Framework.syncBundleListeners.add(bundleLifecycleHandler);
    frameworkLifecycleHandler = new FrameworkLifecycleHandler();
    Framework.frameworkListeners.add(frameworkLifecycleHandler);
    /**
         * TODO NEED CHECK
         * 解决某些自带LBE机制无法hook execstartactivity以及service start service的hook
         */
    try {
        ActivityManagrHook activityManagerProxy = new ActivityManagrHook();
        Object gDefault = AtlasHacks.ActivityManagerNative_gDefault.get(AtlasHacks.ActivityManagerNative.getmClass());
        AtlasHacks.Singleton_mInstance.hijack(gDefault, activityManagerProxy);
    } catch (Throwable e) {
    }
    AndroidHack.hackH();
}
Also used : BundleLifecycleHandler(android.taobao.atlas.runtime.BundleLifecycleHandler) ActivityManagrHook(android.taobao.atlas.runtime.ActivityManagrHook) ApplicationInfo(android.content.pm.ApplicationInfo) InstrumentationHook(android.taobao.atlas.runtime.InstrumentationHook) DelegateClassLoader(android.taobao.atlas.runtime.DelegateClassLoader) DelegateClassLoader(android.taobao.atlas.runtime.DelegateClassLoader) FrameworkLifecycleHandler(android.taobao.atlas.runtime.FrameworkLifecycleHandler)

Aggregations

DelegateClassLoader (android.taobao.atlas.runtime.DelegateClassLoader)2 ApplicationInfo (android.content.pm.ApplicationInfo)1 Resources (android.content.res.Resources)1 ActivityManagrHook (android.taobao.atlas.runtime.ActivityManagrHook)1 BundleLifecycleHandler (android.taobao.atlas.runtime.BundleLifecycleHandler)1 DelegateResources (android.taobao.atlas.runtime.DelegateResources)1 FrameworkLifecycleHandler (android.taobao.atlas.runtime.FrameworkLifecycleHandler)1 InstrumentationHook (android.taobao.atlas.runtime.InstrumentationHook)1 ArrayMap (android.util.ArrayMap)1 WeakReference (java.lang.ref.WeakReference)1 HashMap (java.util.HashMap)1 WeakHashMap (java.util.WeakHashMap)1