Search in sources :

Example 1 with DelegateResources

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

the class AndroidHack method createNewLoadedApk.

/**
     * 用getPackageInfoNoCheck 创建一个新的LoadedApk
     * @param application
     * @param activityThread
     * @return
     */
public static Object createNewLoadedApk(Application application, Object activityThread) {
    try {
        PackageManager manager = RuntimeVariables.androidApplication.getPackageManager();
        ApplicationInfo info = manager.getApplicationInfo(RuntimeVariables.androidApplication.getPackageName(), PackageManager.GET_ACTIVITIES);
        String currentSource = info != null ? info.sourceDir : null;
        if (Atlas.sAPKSource == null || currentSource == null || !currentSource.equals(Atlas.sAPKSource)) {
            Log.e("AndroidHack", Atlas.sAPKSource + " | " + currentSource);
            ActivityTaskMgr.getInstance().clearActivityStack();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
            return null;
        }
        ApplicationInfo ai = application.getPackageManager().getApplicationInfo(application.getPackageName(), PackageManager.GET_META_DATA | PackageManager.GET_SHARED_LIBRARY_FILES);
        PackageManager packageManager = application.getPackageManager();
        Resources mResources = application.getResources();
        Method getCompatibilityInfo = null;
        if (mResources instanceof DelegateResources) {
            getCompatibilityInfo = mResources.getClass().getSuperclass().getDeclaredMethod("getCompatibilityInfo");
        } else {
            getCompatibilityInfo = findMethod(mResources, "getCompatibilityInfo");
        }
        getCompatibilityInfo.setAccessible(true);
        Class ComplatibilityInfoClass = Class.forName("android.content.res.CompatibilityInfo");
        Object compatibilityInfo = getCompatibilityInfo.invoke(application.getResources());
        Class<?>[] args = { ApplicationInfo.class, ComplatibilityInfoClass };
        Method getPackageInfoNoCheck = AtlasHacks.ActivityThread.getmClass().getDeclaredMethod("getPackageInfoNoCheck", args);
        getPackageInfoNoCheck.setAccessible(true);
        Object loadedApk = getPackageInfoNoCheck.invoke(activityThread, ai, compatibilityInfo);
        _mLoadedApk = loadedApk;
        Field mApplicationField = _mLoadedApk.getClass().getDeclaredField("mApplication");
        mApplicationField.setAccessible(true);
        mApplicationField.set(_mLoadedApk, RuntimeVariables.androidApplication);
        return loadedApk;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) PackageManager(android.content.pm.PackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) DelegateResources(android.taobao.atlas.runtime.DelegateResources) Resources(android.content.res.Resources) Method(java.lang.reflect.Method) DelegateResources(android.taobao.atlas.runtime.DelegateResources)

Aggregations

ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageManager (android.content.pm.PackageManager)1 Resources (android.content.res.Resources)1 DelegateResources (android.taobao.atlas.runtime.DelegateResources)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1