dalvik.system.PathClassLoader Java Examples

The following examples show how to use dalvik.system.PathClassLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WatchFaceUtil.java    From APDE with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Detect whether the sketch is a canvas- or GLES-based watchface (i.e. which renderer it uses).
 * We do this by checking the base class of the service.
 *
 * @param context
 * @return an integer corresponding to the service type (CANVAS, GLES, or FAILURE)
 */
private static int detectServiceType(Context context) {
	File apkFile = getSketchApk(context);
	
	if (apkFile.exists()) {
		// Really important to set up our class loader with the default one as parent
		// Otherwise we get two copies of loaded classes and things get ugly
		PathClassLoader classLoader = new PathClassLoader(apkFile.getAbsolutePath(), PApplet.class.getClassLoader());
		String classPath = getSketchPackage(context) + ".MainService";
		
		try {
			Class<?> service = Class.forName(classPath, true, classLoader);
			
			if (PWatchFaceCanvas.class.isAssignableFrom(service)) {
				return CANVAS;
			} else if (PWatchFaceGLES.class.isAssignableFrom(service)) {
				return GLES;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	return FAILURE;
}
 
Example #2
Source File: ClassLoaderInjectHelper.java    From Neptune with Apache License 2.0 6 votes vote down vote up
/**
 * api >= 14时,注入jar
 *
 * @param context application object
 * @param dexPath lib path
 * @return inject object
 */
private static InjectResult injectAboveEqualApiLevel14(Context context, String dexPath, String soPath) {
    PathClassLoader pathClassLoader = (PathClassLoader) context.getClassLoader();
    File optDir = PluginInstaller.getPluginInjectRootPath(context);
    FileUtils.checkOtaFileValid(optDir, new File(dexPath));
    DexClassLoader dexClassLoader = new DexClassLoader(dexPath, optDir.getAbsolutePath(), soPath,
            pathClassLoader);
    InjectResult result = null;

    // If version > 22 LOLLIPOP_MR1
    if (Build.VERSION.SDK_INT > 22) {
        result = injectAboveApiLevel22(pathClassLoader, dexClassLoader);
    } else {
        result = injectAboveEqualApiLevel14(pathClassLoader, dexClassLoader);
    }

    return result;
}
 
Example #3
Source File: HookLoader.java    From QNotified with GNU General Public License v3.0 6 votes vote down vote up
/**
     * 安装app以后,系统会在/data/app/下备份了一份.apk文件,通过动态加载这个apk文件,调用相应的方法
     * 这样就可以实现,只需要第一次重启,以后修改hook代码就不用重启了
     *
     * @param context           context参数
     * @param modulePackageName 当前模块的packageName
     * @param handleHookClass   指定由哪一个类处理相关的hook逻辑
     * @param loadPackageParam  传入XC_LoadPackage.LoadPackageParam参数
     * @throws Throwable 抛出各种异常,包括具体hook逻辑的异常,寻找apk文件异常,反射加载Class异常等
     */
    private void invokeHandleHookMethod(Context context, String modulePackageName, String handleHookClass, String handleHookMethod, XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
//        File apkFile = findApkFileBySDK(modulePackageName);//会受其它Xposed模块hook 当前宿主程序的SDK_INT的影响
//        File apkFile = findApkFile(modulePackageName);
        //原来的两种方式不是很好,改用这种新的方式
        File apkFile = findApkFile(context, modulePackageName);
        if (apkFile == null) {
            throw new RuntimeException("!!!!!寻找模块apk失败");
        }
        //加载指定的hook逻辑处理类,并调用它的handleHook方法
        PathClassLoader pathClassLoader = new PathClassLoader(apkFile.getAbsolutePath(), XposedBridge.class.getClassLoader());
        Class<?> cls = Class.forName(handleHookClass, true, pathClassLoader);
		/*Utils.log(apkFile.getAbsolutePath());
		 Utils.log(cls.toString());
		 Utils.log(pathClassLoader.toString());*/
        Object instance = cls.newInstance();
        //instance.handleLoadPackage(loadPackageParam);
        Method method = cls.getDeclaredMethod(handleHookMethod, XC_LoadPackage.LoadPackageParam.class);
        method.invoke(instance, loadPackageParam);
    }
 
Example #4
Source File: InitInjector.java    From XposedHider with GNU General Public License v3.0 6 votes vote down vote up
private void invokeHandleHookMethod(
        Context context,
        String modulePackageName,
        String handleHookClass,
        String handleHookMethod,
        XC_LoadPackage.LoadPackageParam loadPackageParam
) throws Throwable {
    // 原来的两种方式不是很好,改用这种新的方式
    File apkFile = findApkFile(context, modulePackageName);
    if (apkFile == null) {
        throw new RuntimeException("Cannot find the module APK.");
    }
    // 加载指定的hook逻辑处理类,并调用它的handleHook方法
    PathClassLoader pathClassLoader =
            new PathClassLoader(apkFile.getAbsolutePath(), ClassLoader.getSystemClassLoader());
    Class<?> cls = Class.forName(handleHookClass, true, pathClassLoader);
    Object instance = cls.newInstance();
    Method method = cls.getDeclaredMethod(handleHookMethod,
            Context.class, XC_LoadPackage.LoadPackageParam.class);
    method.invoke(instance, context, loadPackageParam);
}
 
Example #5
Source File: HotXposedInit.java    From BiliRoaming with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
    disableModulesUpdatedNotification(lpparam);

    if (!HOST_PACKAGE.equals(lpparam.packageName)
            && !BuildConfig.APPLICATION_ID.equals(lpparam.packageName)) return;

    File moduleApkFile = getModuleApkFile();
    if (!moduleApkFile.exists()) return;

    PathClassLoader classLoader =
            new PathClassLoader(moduleApkFile.getAbsolutePath(), lpparam.getClass().getClassLoader());
    Class<?> xposedInitClass = classLoader.loadClass(REAL_XPOSED_INIT);
    if (xposedInitClass != null) {
        callMethod(xposedInitClass.newInstance(), "handleLoadPackage", lpparam);
    }
}
 
Example #6
Source File: WXSoInstallMgrSdk.java    From weex-uikit with MIT License 6 votes vote down vote up
/**
 *
 * @param libName lib name
 * @param size  the right size of lib
 * @return true for valid  ; false for InValid
 */
static boolean checkSoIsValid(String libName, int size) {
  Context context = mContext;
  if (null == context) {
    return false;
  }
  try{
    long start=System.currentTimeMillis();
    if(WXSoInstallMgrSdk.class.getClassLoader() instanceof PathClassLoader ) {

      String path = ((PathClassLoader) (WXSoInstallMgrSdk.class.getClassLoader())).findLibrary(libName);
      File file = new File(path);

      if (!file.exists() || size == file.length()) {
        WXLogUtils.w("weex so size check path :" + path+"   "+(System.currentTimeMillis() - start));
        return true;
      } else {
        return false;
      }
    }
  }catch(Throwable e ){
    WXLogUtils.e("weex so size check fail exception :"+e.getMessage());
  }

  return true;
}
 
Example #7
Source File: VDUtility.java    From NewsMe with Apache License 2.0 6 votes vote down vote up
/**
 * 用反射方式加载类
 * 
 * @param context
 * @param path
 * @return
 */
public static Object loadClass(Context context, String path) {
	try {
		String dexPath = context.getApplicationInfo().sourceDir;
		PathClassLoader pathClassLoader = new PathClassLoader(dexPath,
				context.getClassLoader());
		Class<?> c = Class.forName(path, true, pathClassLoader);
		Object ret = c.newInstance();
		return ret;
	} catch (InstantiationException ex1) {
		ex1.printStackTrace();
	} catch (IllegalAccessException ex2) {
		ex2.printStackTrace();
	} catch (ClassNotFoundException ex3) {
		ex3.printStackTrace();
	}

	return null;
}
 
Example #8
Source File: HotFix.java    From Android-Tech with Apache License 2.0 6 votes vote down vote up
private static void injectInAliyunOs(Context context, String patchDexFile, String patchClassName)
        throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException, NoSuchFieldException {
    PathClassLoader obj = (PathClassLoader) context.getClassLoader();
    String replaceAll = new File(patchDexFile).getName().replaceAll("\\.[a-zA-Z0-9]+", ".lex");
    Class cls = Class.forName("dalvik.system.LexClassLoader");
    Object newInstance =
            cls.getConstructor(new Class[]{String.class, String.class, String.class, ClassLoader.class}).newInstance(
                    new Object[]{context.getDir("dex", 0).getAbsolutePath() + File.separator + replaceAll,
                            context.getDir("dex", 0).getAbsolutePath(), patchDexFile, obj});
    cls.getMethod("loadClass", new Class[]{String.class}).invoke(newInstance, new Object[]{patchClassName});
    setField(obj, PathClassLoader.class, "mPaths",
            appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(newInstance, cls, "mRawDexPath")));
    setField(obj, PathClassLoader.class, "mFiles",
            combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(newInstance, cls, "mFiles")));
    setField(obj, PathClassLoader.class, "mZips",
            combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(newInstance, cls, "mZips")));
    setField(obj, PathClassLoader.class, "mLexs",
            combineArray(getField(obj, PathClassLoader.class, "mLexs"), getField(newInstance, cls, "mDexs")));
}
 
Example #9
Source File: HotFix.java    From Android-Tech with Apache License 2.0 6 votes vote down vote up
@TargetApi(14)
private static void injectBelowApiLevel14(Context context, String str, String str2)
        throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    PathClassLoader obj = (PathClassLoader) context.getClassLoader();
    DexClassLoader dexClassLoader =
            new DexClassLoader(str, context.getDir("dex", 0).getAbsolutePath(), str, context.getClassLoader());
    dexClassLoader.loadClass(str2);
    setField(obj, PathClassLoader.class, "mPaths",
            appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(dexClassLoader, DexClassLoader.class,
                            "mRawDexPath")
            ));
    setField(obj, PathClassLoader.class, "mFiles",
            combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(dexClassLoader, DexClassLoader.class,
                            "mFiles")
            ));
    setField(obj, PathClassLoader.class, "mZips",
            combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(dexClassLoader, DexClassLoader.class,
                    "mZips")));
    setField(obj, PathClassLoader.class, "mDexs",
            combineArray(getField(obj, PathClassLoader.class, "mDexs"), getField(dexClassLoader, DexClassLoader.class,
                    "mDexs")));
    obj.loadClass(str2);
}
 
Example #10
Source File: FreelineCore.java    From freeline with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void injectHackDex(Context context, PathClassLoader origin) {
    File hackDex = new File(getDynamicCacheDir(), "hackload.dex");
    if (!hackDex.exists() || hackDex.length() < 100) {
        try {
            copyAssets(context, "hackload.dex", hackDex.getAbsolutePath());
            Log.i(TAG, "copy hackload dex from assets success");
        } catch (Exception e) {
            printStackTrace(e);
        }
    }
    if (hackDex.exists() && hackDex.length() > 100) {
        File opt = new File(getDynamicCacheDir(), "opt");
        if (!opt.exists()) {
            opt.mkdirs();
        }
        DexUtils.inject(origin, hackDex, opt);
        Log.i(TAG, "load hackload,dex size:" + hackDex.length());
    }
}
 
Example #11
Source File: HotFix.java    From SDKHoxFix with Apache License 2.0 6 votes vote down vote up
private static void injectInAliyunOs(Context context, String patchDexFile, String patchClassName)
    throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException, NoSuchFieldException {
    PathClassLoader obj = (PathClassLoader) context.getClassLoader();
    String replaceAll = new File(patchDexFile).getName().replaceAll("\\.[a-zA-Z0-9]+", ".lex");
    Class cls = Class.forName("dalvik.system.LexClassLoader");
    Object newInstance =
        cls.getConstructor(new Class[] {String.class, String.class, String.class, ClassLoader.class}).newInstance(
            new Object[] {context.getDir("dex", 0).getAbsolutePath() + File.separator + replaceAll,
                context.getDir("dex", 0).getAbsolutePath(), patchDexFile, obj});
    cls.getMethod("loadClass", new Class[] {String.class}).invoke(newInstance, new Object[] {patchClassName});
    setField(obj, PathClassLoader.class, "mPaths",
        appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(newInstance, cls, "mRawDexPath")));
    setField(obj, PathClassLoader.class, "mFiles",
        combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(newInstance, cls, "mFiles")));
    setField(obj, PathClassLoader.class, "mZips",
        combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(newInstance, cls, "mZips")));
    setField(obj, PathClassLoader.class, "mLexs",
        combineArray(getField(obj, PathClassLoader.class, "mLexs"), getField(newInstance, cls, "mDexs")));
}
 
Example #12
Source File: ClassLoader.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Encapsulates the set of parallel capable loader types.
 */
private static ClassLoader createSystemClassLoader() {
    String classPath = System.getProperty("java.class.path", ".");
    String librarySearchPath = System.getProperty("java.library.path", "");

    // String[] paths = classPath.split(":");
    // URL[] urls = new URL[paths.length];
    // for (int i = 0; i < paths.length; i++) {
    // try {
    // urls[i] = new URL("file://" + paths[i]);
    // }
    // catch (Exception ex) {
    // ex.printStackTrace();
    // }
    // }
    //
    // return new java.net.URLClassLoader(urls, null);

    // TODO Make this a java.net.URLClassLoader once we have those?
    return new PathClassLoader(classPath, librarySearchPath, BootClassLoader.getInstance());
}
 
Example #13
Source File: WatchFaceUtil.java    From APDE with GNU General Public License v2.0 6 votes vote down vote up
public static PApplet loadSketchPApplet(Context context, boolean gles) {
	File apkFile = getSketchApk(context);
	
	if (apkFile.exists()) {
		// Really important to set up our class loader with the default one as parent
		// Otherwise we get two copies of loaded classes and things get ugly
		PathClassLoader classLoader = new PathClassLoader(apkFile.getAbsolutePath(), PApplet.class.getClassLoader());
		String classPath = getSketchPackage(context) + "." + getSketchClass(context);
		
		try {
			Class<?> sketch = Class.forName(classPath, true, classLoader);
			
			if (PApplet.class.isAssignableFrom(sketch)) {
				return (PApplet) sketch.newInstance();
			}
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | ClassCastException e) {
			e.printStackTrace();
		}
	}
	
	// Default wallpaper if things fail
	return getDefaultSketch(gles);
}
 
Example #14
Source File: DelegatingClassLoader.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Clear the existing delegate and return the new one, populated with the given dex files
 *
 * @param dexJars the .dex.jar files which will be managed by our delegate
 */
void resetDelegate(List<File> dexJars) {
  mDelegate = new PathClassLoader("", "", this);
  mManagedClassesToDexFile.clear();
  for (File dexJar : dexJars) {
    try {
      final File optFile = new File(mDexOptDir, dexJar.getName());
      DexFile dexFile = DexFile.loadDex(dexJar.getCanonicalPath(), optFile.getCanonicalPath(), 0);
      final Enumeration<String> entries = dexFile.entries();
      while (entries.hasMoreElements()) {
        mManagedClassesToDexFile.put(entries.nextElement(), dexFile);
      }
    } catch (IOException e) {
      // Pass for now
    }
  }
}
 
Example #15
Source File: DexInjector.java    From dalvik_patch with The Unlicense 6 votes vote down vote up
private static synchronized Boolean injectAboveEqualApiLevel14(
        String dexPath, String defaultDexOptPath, String nativeLibPath, String dummyClassName) {
    Log.i(TAG, "--> injectAboveEqualApiLevel14");
    PathClassLoader pathClassLoader = (PathClassLoader) DexInjector.class.getClassLoader();
    DexClassLoader dexClassLoader = new DexClassLoader(dexPath, defaultDexOptPath, nativeLibPath, pathClassLoader);
    try {
        dexClassLoader.loadClass(dummyClassName);
        Object dexElements = combineArray(
                getDexElements(getPathList(pathClassLoader)),
                getDexElements(getPathList(dexClassLoader)));

        Object pathList = getPathList(pathClassLoader);
        setField(pathList, pathList.getClass(), "dexElements", dexElements);
    } catch (Throwable e) {
        e.printStackTrace();
        return false;
    }
    Log.i(TAG, "<-- injectAboveEqualApiLevel14 End.");
    return true;
}
 
Example #16
Source File: HotFix.java    From HotFix with MIT License 6 votes vote down vote up
@TargetApi(14)
private static void injectBelowApiLevel14(Context context, String str, String str2)
    throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    PathClassLoader obj = (PathClassLoader) context.getClassLoader();
    DexClassLoader dexClassLoader =
        new DexClassLoader(str, context.getDir("dex", 0).getAbsolutePath(), str, context.getClassLoader());
    dexClassLoader.loadClass(str2);
    setField(obj, PathClassLoader.class, "mPaths",
        appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(dexClassLoader, DexClassLoader.class,
                "mRawDexPath")
        ));
    setField(obj, PathClassLoader.class, "mFiles",
        combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(dexClassLoader, DexClassLoader.class,
                "mFiles")
        ));
    setField(obj, PathClassLoader.class, "mZips",
        combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(dexClassLoader, DexClassLoader.class,
            "mZips")));
    setField(obj, PathClassLoader.class, "mDexs",
        combineArray(getField(obj, PathClassLoader.class, "mDexs"), getField(dexClassLoader, DexClassLoader.class,
            "mDexs")));
    obj.loadClass(str2);
}
 
Example #17
Source File: HotFix.java    From HotFix with MIT License 6 votes vote down vote up
private static void injectInAliyunOs(Context context, String patchDexFile, String patchClassName)
    throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException,
    InstantiationException, NoSuchFieldException {
    PathClassLoader obj = (PathClassLoader) context.getClassLoader();
    String replaceAll = new File(patchDexFile).getName().replaceAll("\\.[a-zA-Z0-9]+", ".lex");
    Class cls = Class.forName("dalvik.system.LexClassLoader");
    Object newInstance =
        cls.getConstructor(new Class[] {String.class, String.class, String.class, ClassLoader.class}).newInstance(
            new Object[] {context.getDir("dex", 0).getAbsolutePath() + File.separator + replaceAll,
                context.getDir("dex", 0).getAbsolutePath(), patchDexFile, obj});
    cls.getMethod("loadClass", new Class[] {String.class}).invoke(newInstance, new Object[] {patchClassName});
    setField(obj, PathClassLoader.class, "mPaths",
        appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(newInstance, cls, "mRawDexPath")));
    setField(obj, PathClassLoader.class, "mFiles",
        combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(newInstance, cls, "mFiles")));
    setField(obj, PathClassLoader.class, "mZips",
        combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(newInstance, cls, "mZips")));
    setField(obj, PathClassLoader.class, "mLexs",
        combineArray(getField(obj, PathClassLoader.class, "mLexs"), getField(newInstance, cls, "mDexs")));
}
 
Example #18
Source File: ReflectionUtil.java    From Aria with Apache License 2.0 6 votes vote down vote up
/**
 * 加载指定的反射类
 */
public static Class<?> loadClass(Context context, String ClassName) {
  String packageName = AndroidUtils.getPackageName(context);
  String sourcePath = AndroidUtils.getSourcePath(context, packageName);
  if (!TextUtils.isEmpty(sourcePath)) {
    PathClassLoader cl =
        new PathClassLoader(sourcePath, "/data/app/", ClassLoader.getSystemClassLoader());
    try {
      return cl.loadClass(ClassName);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  } else {
    FL.e(TAG, "没有【" + sourcePath + "】目录");
  }
  return null;
}
 
Example #19
Source File: HotFix.java    From SDKHoxFix with Apache License 2.0 6 votes vote down vote up
@TargetApi(14)
private static void injectBelowApiLevel14(Context context, String str, String str2)
    throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    PathClassLoader obj = (PathClassLoader) context.getClassLoader();
    DexClassLoader dexClassLoader =
        new DexClassLoader(str, context.getDir("dex", 0).getAbsolutePath(), str, context.getClassLoader());
    dexClassLoader.loadClass(str2);
    setField(obj, PathClassLoader.class, "mPaths",
        appendArray(getField(obj, PathClassLoader.class, "mPaths"), getField(dexClassLoader, DexClassLoader.class,
                "mRawDexPath")
        ));
    setField(obj, PathClassLoader.class, "mFiles",
        combineArray(getField(obj, PathClassLoader.class, "mFiles"), getField(dexClassLoader, DexClassLoader.class,
                "mFiles")
        ));
    setField(obj, PathClassLoader.class, "mZips",
        combineArray(getField(obj, PathClassLoader.class, "mZips"), getField(dexClassLoader, DexClassLoader.class,
            "mZips")));
    setField(obj, PathClassLoader.class, "mDexs",
        combineArray(getField(obj, PathClassLoader.class, "mDexs"), getField(dexClassLoader, DexClassLoader.class,
            "mDexs")));
    obj.loadClass(str2);
}
 
Example #20
Source File: FreelineCore.java    From freeline with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void injectDex(PathClassLoader origin) {
    String dexDirPath = getDynamicDexDirPath();
    if (!TextUtils.isEmpty(dexDirPath)) {
        File dexDir = new File(dexDirPath);
        if (dexDir.isDirectory()) {
            File[] dexFiles = dexDir.listFiles();

            if (dexFiles.length > 0) {
                File opt = new File(getDynamicDexOptPath());
                if (!opt.exists()) {
                    opt.mkdirs();
                }
                for (File dexFile : dexFiles) {
                    String dirName = generateStringMD5(dexFile.getName());
                    File dexOptDir = new File(opt, dirName);
                    if (!dexOptDir.exists()) {
                        dexOptDir.mkdirs();
                    }
                    DexUtils.inject(origin, dexFile, dexOptDir);
                }
                Log.i(TAG, "find increment package");
            }
        }
    }
}
 
Example #21
Source File: FreelineCore.java    From freeline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void injectHackNativeLib(Context context, PathClassLoader classLoader) {
    // 修复so patch不生效问题
    try {
        NativeUtils.installNativeLibraryPath(classLoader, new File(getDynamicNativeDir()), false);
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
    //NativeUtils.injectHackNativeLib(getDynamicNativeDir(), classLoader);
}
 
Example #22
Source File: HotFix.java    From SDKHoxFix with Apache License 2.0 5 votes vote down vote up
private static void injectAboveEqualApiLevel14(Context context, String str, String str2)
    throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    PathClassLoader pathClassLoader = (PathClassLoader) context.getClassLoader();
    Object a = combineArray(getDexElements(getPathList(pathClassLoader)),
        getDexElements(getPathList(
            new DexClassLoader(str, context.getDir("dex", 0).getAbsolutePath(), str, context.getClassLoader()))));
    Object a2 = getPathList(pathClassLoader);
    setField(a2, a2.getClass(), "dexElements", a);
    pathClassLoader.loadClass(str2);
}
 
Example #23
Source File: ClassLoaderInjectHelper.java    From Neptune with Apache License 2.0 5 votes vote down vote up
/**
 * 阿里云系统注入jar
 *
 * @param context application object
 * @param dexPath lib path
 * @return inject result
 */
private static InjectResult injectInAliyunOs(Context context, String dexPath, String soPath) {
    InjectResult result = null;
    PathClassLoader localClassLoader = (PathClassLoader) context.getClassLoader();
    File optDir = PluginInstaller.getPluginInjectRootPath(context);
    FileUtils.checkOtaFileValid(optDir, new File(dexPath));
    new DexClassLoader(dexPath, optDir.getAbsolutePath(), soPath, localClassLoader);
    String lexFileName = new File(dexPath).getName();
    lexFileName = lexFileName.replaceAll("\\.[a-zA-Z0-9]+", ".lex");
    try {
        Class<?> classLexClassLoader = Class.forName("dalvik.system.LexClassLoader");
        Constructor<?> constructorLexClassLoader = classLexClassLoader.getConstructor(String.class, String.class, String.class,
                ClassLoader.class);
        Object localLexClassLoader = constructorLexClassLoader.newInstance(
                optDir.getAbsolutePath() + File.separator + lexFileName, optDir.getAbsolutePath(),
                soPath, localClassLoader);
        setField(localClassLoader, PathClassLoader.class, "mPaths",
                appendArray(getField(localClassLoader, PathClassLoader.class, "mPaths"),
                        getField(localLexClassLoader, classLexClassLoader, "mRawDexPath")));
        setField(localClassLoader, PathClassLoader.class, "mFiles",
                combineArray(getField(localClassLoader, PathClassLoader.class, "mFiles"),
                        getField(localLexClassLoader, classLexClassLoader, "mFiles")));
        setField(localClassLoader, PathClassLoader.class, "mZips",
                combineArray(getField(localClassLoader, PathClassLoader.class, "mZips"),
                        getField(localLexClassLoader, classLexClassLoader, "mZips")));
        setField(localClassLoader, PathClassLoader.class, "mLexs",
                combineArray(getField(localClassLoader, PathClassLoader.class, "mLexs"),
                        getField(localLexClassLoader, classLexClassLoader, "mDexs")));

        result = makeInjectResult(true, null);
    } catch (Exception e) {
        result = makeInjectResult(false, e);
        e.printStackTrace();
    }

    return result;
}
 
Example #24
Source File: DexResolver.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Dex[] addFile(File file) throws Exception{
    if(oneFileLoaders ==null) oneFileLoaders =new ArrayList<>();
    BaseDexClassLoader loader=new PathClassLoader(file.getPath(),DexResolver.class.getClassLoader());
    int beforeSize=dexes.size();
    loadClassLoader(loader);

    if(dexes.size()==beforeSize){
        throw new IOException("No dex file found in path:"+file);
    }
    oneFileLoaders.add(loader);
    return dexes.subList(beforeSize,dexes.size()).toArray(new Dex[0]);
}
 
Example #25
Source File: ShieldClassLoader.java    From ApkShield with GNU General Public License v2.0 5 votes vote down vote up
public ShieldClassLoader(
		String desDexPath ,
		Context context ,
		String dexPath ,
		PathClassLoader parent )
{
	super( dexPath , parent );
	File file = new File( desDexPath );
	this.context = context;
	this.mClassLoader = parent;
	//  		
	try
	{
		List<String> libraryPathElements;
		//			Class cls = mClassLoader.getClass();
		Class cls = PathClassLoader.class;
		Field[] fields = cls.getDeclaredFields();
		Method[] methods = cls.getDeclaredMethods();
		//			Field field = mClassLoader.getClass().getDeclaredField( "pathList" );			
		//			field.setAccessible( true );
		//			Object pathList = field.get( mClassLoader );
		//			field = pathList.getClass().getDeclaredField( "nativeLibraryDirectories" );
		//			field.setAccessible( true );
		//			File[] path = (File[])field.get( pathList );
		//拿到originalclassloader  
		//			List<String> libraryPath = (List<String>)field.get( mClassLoader );
		//====================================
		File dexOutputDir = context.getDir( "dex" , 0 );
		mDexClassLoader = new ShieldDexClassLoader( file.getAbsolutePath() , dexOutputDir.getAbsolutePath() , file.getAbsolutePath() , (PathClassLoader)context.getClassLoader() );
	}
	catch( Exception e )
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	// TODO Auto-generated constructor stub
}
 
Example #26
Source File: ShieldDexClassLoader.java    From ApkShield with GNU General Public License v2.0 5 votes vote down vote up
public ShieldDexClassLoader(
		String dexPath ,
		String optimizedDirectory ,
		String libraryPath ,
		PathClassLoader parent )
{		
	super( dexPath , optimizedDirectory , libraryPath , parent );
	// TODO Auto-generated constructor stub
}
 
Example #27
Source File: ShieldDexClassLoader.java    From ApkShield with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String findLibrary(
		String name )
{
	// TODO Auto-generated method stub
	Log.d( "MM" , "ShieldDexClassLoader findLibrary name " + name );
	String libpath = super.findLibrary( name );
	if(TextUtils.isEmpty( libpath ))
	{
		libpath = ((PathClassLoader)this.getParent()).findLibrary( name );
	}				
	return libpath;
}
 
Example #28
Source File: ApplicationLoaders.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @hide
 */
void addNative(ClassLoader classLoader, Collection<String> libPaths) {
    if (!(classLoader instanceof PathClassLoader)) {
        throw new IllegalStateException("class loader is not a PathClassLoader");
    }
    final PathClassLoader baseDexClassLoader = (PathClassLoader) classLoader;
    baseDexClassLoader.addNativePath(libPaths);
}
 
Example #29
Source File: SystemClassLoaderAdder.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the paths in {@code newClassLoader} to the paths in {@code systemClassLoader}. This works
 * with versions of Android that have {@link BaseDexClassLoader}.
 *
 * @param newClassLoader the class loader with the new paths
 * @param systemClassLoader the system class loader
 */
private void addNewClassLoaderToSystemClassLoaderWithBaseDex(
    DexClassLoader newClassLoader, PathClassLoader systemClassLoader)
    throws NoSuchFieldException, IllegalAccessException {
  Object currentElementsArray = getDexElementsArray(getDexPathList(systemClassLoader));
  Object newElementsArray = getDexElementsArray(getDexPathList(newClassLoader));
  Object mergedElementsArray = mergeArrays(currentElementsArray, newElementsArray);
  setDexElementsArray(getDexPathList(systemClassLoader), mergedElementsArray);
}
 
Example #30
Source File: SystemClassLoaderAdder.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Installs a list of .dex.jar files into the application class loader.
 *
 * @param appClassLoader The application ClassLoader, which can be retrieved by calling {@code
 *     getClassLoader} on the application Context.
 * @param optimizedDirectory Directory for storing optimized dex files.
 * @param dexJars The list of .dex.jar files to load.
 */
static void installDexJars(
    ClassLoader appClassLoader, File optimizedDirectory, List<File> dexJars) {
  SystemClassLoaderAdder classLoaderAdder = new SystemClassLoaderAdder();

  for (File dexJar : dexJars) {
    DexClassLoader newClassLoader =
        new DexClassLoader(
            dexJar.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, appClassLoader);
    classLoaderAdder.addPathsOfClassLoaderToSystemClassLoader(
        newClassLoader, (PathClassLoader) appClassLoader);
  }
}