Search in sources :

Example 36 with DexFile

use of dalvik.system.DexFile in project atlas by alibaba.

the class NClassLoader method replacePathClassLoader.

public static void replacePathClassLoader(Context base, ClassLoader original) throws Exception {
    NClassLoader loader = new NClassLoader(".", original.getParent());
    Field pathListField = findField(original, "pathList");
    pathListField.setAccessible(true);
    Object originPathListObject = pathListField.get(original);
    Field definingContextField = findField(originPathListObject, "definingContext");
    definingContextField.set(originPathListObject, loader);
    Field loadPathList = findField(loader, "pathList");
    //just use PathClassloader's pathList
    loadPathList.set(loader, originPathListObject);
    //we must recreate dexFile due to dexCache
    List<File> additionalClassPathEntries = new ArrayList<File>();
    Field dexElement = findField(originPathListObject, "dexElements");
    Object[] originDexElements = (Object[]) dexElement.get(originPathListObject);
    for (Object element : originDexElements) {
        DexFile dexFile = (DexFile) findField(element, "dexFile").get(element);
        additionalClassPathEntries.add(new File(dexFile.getName()));
    //protect for java.lang.AssertionError: Failed to close dex file in finalizer.
    //            oldDexFiles.add(dexFile);
    }
    Method makePathElements = findMethod(originPathListObject, "makePathElements", List.class, File.class, List.class);
    ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
    Object[] newDexElements = (Object[]) makePathElements.invoke(originPathListObject, additionalClassPathEntries, null, suppressedExceptions);
    dexElement.set(originPathListObject, newDexElements);
    Field mPackageInfoField = base.getClass().getDeclaredField("mPackageInfo");
    mPackageInfoField.setAccessible(true);
    Object loadedApk = mPackageInfoField.get(base);
    Field classloaderField = loadedApk.getClass().getDeclaredField("mClassLoader");
    classloaderField.setAccessible(true);
    classloaderField.set(loadedApk, loader);
    Thread.currentThread().setContextClassLoader(loader);
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) IOException(java.io.IOException) DexFile(dalvik.system.DexFile) File(java.io.File) DexFile(dalvik.system.DexFile)

Example 37 with DexFile

use of dalvik.system.DexFile in project android_frameworks_base by ParanoidAndroid.

the class ClassPathPackageInfoSource method findClassesInApk.

/**
     * Finds all classes and sub packages that are below the packageName and
     * add them to the respective sets. Searches the package in a single apk file.
     */
private void findClassesInApk(String apkPath, String packageName, Set<String> classNames, Set<String> subpackageNames) throws IOException {
    DexFile dexFile = null;
    try {
        dexFile = new DexFile(apkPath);
        Enumeration<String> apkClassNames = dexFile.entries();
        while (apkClassNames.hasMoreElements()) {
            String className = apkClassNames.nextElement();
            if (className.startsWith(packageName)) {
                String subPackageName = packageName;
                int lastPackageSeparator = className.lastIndexOf('.');
                if (lastPackageSeparator > 0) {
                    subPackageName = className.substring(0, lastPackageSeparator);
                }
                if (subPackageName.length() > packageName.length()) {
                    subpackageNames.add(subPackageName);
                } else if (isToplevelClass(className)) {
                    classNames.add(className);
                }
            }
        }
    } catch (IOException e) {
        if (false) {
            Log.w("ClassPathPackageInfoSource", "Error finding classes at apk path: " + apkPath, e);
        }
    } finally {
        if (dexFile != null) {
        // Todo: figure out why closing causes a dalvik error resulting in vm shutdown.
        //                dexFile.close();
        }
    }
}
Also used : IOException(java.io.IOException) DexFile(dalvik.system.DexFile)

Example 38 with DexFile

use of dalvik.system.DexFile in project tinker by Tencent.

the class AndroidNClassLoader method createAndroidNClassLoader.

private static AndroidNClassLoader createAndroidNClassLoader(PathClassLoader original) throws Exception {
    //let all element ""
    AndroidNClassLoader androidNClassLoader = new AndroidNClassLoader("", original);
    Field originPathList = ShareReflectUtil.findField(original, "pathList");
    Object originPathListObject = originPathList.get(original);
    //should reflect definingContext also
    Field originClassloader = ShareReflectUtil.findField(originPathListObject, "definingContext");
    originClassloader.set(originPathListObject, androidNClassLoader);
    //copy pathList
    Field pathListField = ShareReflectUtil.findField(androidNClassLoader, "pathList");
    //just use PathClassloader's pathList
    pathListField.set(androidNClassLoader, originPathListObject);
    //we must recreate dexFile due to dexCache
    List<File> additionalClassPathEntries = new ArrayList<>();
    Field dexElement = ShareReflectUtil.findField(originPathListObject, "dexElements");
    Object[] originDexElements = (Object[]) dexElement.get(originPathListObject);
    for (Object element : originDexElements) {
        DexFile dexFile = (DexFile) ShareReflectUtil.findField(element, "dexFile").get(element);
        additionalClassPathEntries.add(new File(dexFile.getName()));
        //protect for java.lang.AssertionError: Failed to close dex file in finalizer.
        oldDexFiles.add(dexFile);
    }
    Method makePathElements = ShareReflectUtil.findMethod(originPathListObject, "makePathElements", List.class, File.class, List.class);
    ArrayList<IOException> suppressedExceptions = new ArrayList<>();
    Object[] newDexElements = (Object[]) makePathElements.invoke(originPathListObject, additionalClassPathEntries, null, suppressedExceptions);
    dexElement.set(originPathListObject, newDexElements);
    return androidNClassLoader;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) IOException(java.io.IOException) DexFile(dalvik.system.DexFile) File(java.io.File) DexFile(dalvik.system.DexFile)

Example 39 with DexFile

use of dalvik.system.DexFile in project android_frameworks_base by ResurrectionRemix.

the class RunTestCommand method addTestClassesFromJars.

/**
     * Add test classes from jars passed on the command line. Use this if nothing was explicitly
     * specified on the command line.
     */
private void addTestClassesFromJars() {
    String jars = mParams.getString(JARS_PARAM);
    if (jars == null)
        return;
    String[] jarFileNames = jars.split(JARS_SEPARATOR);
    for (String fileName : jarFileNames) {
        fileName = fileName.trim();
        if (fileName.isEmpty())
            continue;
        try {
            DexFile dexFile = new DexFile(fileName);
            for (Enumeration<String> e = dexFile.entries(); e.hasMoreElements(); ) {
                String className = e.nextElement();
                if (isTestClass(className)) {
                    mTestClasses.add(className);
                }
            }
            dexFile.close();
        } catch (IOException e) {
            Log.w(LOGTAG, String.format("Could not read %s: %s", fileName, e.getMessage()));
        }
    }
}
Also used : IOException(java.io.IOException) DexFile(dalvik.system.DexFile)

Example 40 with DexFile

use of dalvik.system.DexFile in project Unblock163MusicClient-Xposed by bin456789.

the class MultiDexHelper method getAllClasses.

/**
     * get all the classes name in "classes.dex", "classes2.dex", ....
     *
     * @param context the application context
     * @return all the classes name
     * @throws PackageManager.NameNotFoundException
     * @throws IOException
     */
static List<String> getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException {
    List<String> classNames = new ArrayList<>();
    for (String path : getSourcePaths(context)) {
        try {
            DexFile dexfile;
            String pathTmp = null;
            if (path.endsWith(EXTRACTED_SUFFIX)) {
                //NOT use new DexFile(path), because it will throw "permission error in /data/dalvik-cache"
                pathTmp = path + ".tmp";
                dexfile = DexFile.loadDex(path, pathTmp, 0);
            } else {
                dexfile = new DexFile(path);
            }
            Enumeration<String> dexEntries = dexfile.entries();
            while (dexEntries.hasMoreElements()) {
                classNames.add(dexEntries.nextElement());
            }
            if (pathTmp != null)
                Utility.deleteFile(new File(pathTmp));
        } catch (IOException e) {
            throw new IOException("Error at loading dex file '" + path + "'");
        }
    }
    return classNames;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) DexFile(dalvik.system.DexFile) File(java.io.File) DexFile(dalvik.system.DexFile)

Aggregations

DexFile (dalvik.system.DexFile)59 File (java.io.File)32 IOException (java.io.IOException)28 ArrayList (java.util.ArrayList)17 Field (java.lang.reflect.Field)13 ZipFile (java.util.zip.ZipFile)13 Method (java.lang.reflect.Method)11 DexClassLoader (dalvik.system.DexClassLoader)9 Enumeration (java.util.Enumeration)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ApplicationInfo (android.content.pm.ApplicationInfo)3 NClassLoader (android.taobao.atlas.startup.NClassLoader)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 Application (android.app.Application)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Context (android.content.Context)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Main (com.android.dx.command.dexer.Main)1 Action (com.jia.base.annotation.Action)1