Search in sources :

Example 41 with DexFile

use of dalvik.system.DexFile in project aware-client by denzilferreira.

the class Aware method isClassAvailable.

/**
 * Given a package and class name, check if the class exists or not.
 *
 * @param package_name
 * @param class_name
 * @return true if exists, false otherwise
 */
public static boolean isClassAvailable(Context context, String package_name, String class_name) {
    if (context.getResources().getBoolean(R.bool.standalone)) {
        try {
            Class.forName(package_name + "." + class_name);
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    } else {
        try {
            Context package_context = context.createPackageContext(package_name, Context.CONTEXT_IGNORE_SECURITY + Context.CONTEXT_INCLUDE_CODE);
            DexFile df = new DexFile(package_context.getPackageCodePath());
            for (Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
                String className = iter.nextElement();
                if (className.contains(class_name))
                    return true;
            }
            return false;
        } catch (IOException | NameNotFoundException e) {
            return false;
        }
    }
}
Also used : Context(android.content.Context) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) IOException(java.io.IOException) DexFile(dalvik.system.DexFile)

Example 42 with DexFile

use of dalvik.system.DexFile in project EasyRouter by Xiasm.

the class ClassUtils method getFileNameByPackageName.

public static Set<String> getFileNameByPackageName(Application context, final String packageName) throws PackageManager.NameNotFoundException, InterruptedException {
    final Set<String> classNames = new HashSet<>();
    List<String> paths = getSourcePaths(context);
    // 使用同步计数器判断均处理完成
    final CountDownLatch countDownLatch = new CountDownLatch(paths.size());
    ThreadPoolExecutor threadPoolExecutor = DefaultPoolExecutor.newDefaultPoolExecutor(paths.size());
    for (final String path : paths) {
        threadPoolExecutor.execute(new Runnable() {

            @Override
            public void run() {
                DexFile dexFile = null;
                try {
                    // 加载 apk中的dex 并遍历 获得所有包名为 {packageName} 的类
                    dexFile = new DexFile(path);
                    Enumeration<String> dexEntries = dexFile.entries();
                    while (dexEntries.hasMoreElements()) {
                        String className = dexEntries.nextElement();
                        if (!TextUtils.isEmpty(className) && className.startsWith(packageName)) {
                            classNames.add(className);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (null != dexFile) {
                        try {
                            dexFile.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    // 释放一个
                    countDownLatch.countDown();
                }
            }
        });
    }
    countDownLatch.wait();
    return classNames;
}
Also used : Enumeration(java.util.Enumeration) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DexFile(dalvik.system.DexFile) HashSet(java.util.HashSet)

Example 43 with DexFile

use of dalvik.system.DexFile in project sugar by satyan.

the class MultiDexHelper method getSourcePaths.

/**
 * get all the dex path
 *
 * @return all the dex path, including the ones in the newly added instant-run folder
 * @throws PackageManager.NameNotFoundException
 * @throws IOException
 */
public static List<String> getSourcePaths() throws PackageManager.NameNotFoundException, IOException {
    ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), 0);
    File sourceApk = new File(applicationInfo.sourceDir);
    File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);
    // default instant-run dir
    File instantRunDir = new File(applicationInfo.dataDir, INSTANT_RUN_DEX_DIR_PATH);
    List<String> sourcePaths = new ArrayList<>();
    // add the default apk path
    sourcePaths.add(applicationInfo.sourceDir);
    if (instantRunDir.exists()) {
        // check if app using instant run
        for (final File dexFile : instantRunDir.listFiles()) {
            // add all sources from instan-run
            sourcePaths.add(dexFile.getAbsolutePath());
        }
    }
    // the prefix of extracted file, ie: test.classes
    String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;
    // the total dex numbers
    int totalDexNumber = getMultiDexPreferences().getInt(KEY_DEX_NUMBER, 1);
    for (int secondaryNumber = 2; secondaryNumber <= totalDexNumber; secondaryNumber++) {
        // for each dex file, ie: test.classes2.zip, test.classes3.zip...
        String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX;
        File extractedFile = new File(dexDir, fileName);
        if (extractedFile.isFile()) {
            sourcePaths.add(extractedFile.getAbsolutePath());
        // we ignore the verify zip part
        }
    }
    return sourcePaths;
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) ArrayList(java.util.ArrayList) DexFile(dalvik.system.DexFile) File(java.io.File)

Example 44 with DexFile

use of dalvik.system.DexFile in project RocooFix by dodola.

the class RocooFix method applyPatchRuntime.

/**
 * 从指定目录加载补丁
 *
 * @param context
 * @param dexPath
 */
public static void applyPatchRuntime(Context context, String dexPath) {
    if (context == null) {
        return;
    } else {
        context = context.getApplicationContext();
    }
    try {
        File file = new File(dexPath);
        File optfile = new File(mOptDir, file.getName());
        final DexFile dexFile = DexFile.loadDex(file.getAbsolutePath(), optfile.getAbsolutePath(), Context.MODE_PRIVATE);
        ClassLoader classLoader = context.getClassLoader();
        ClassLoader patchClassLoader = new ClassLoader(classLoader) {

            @Override
            protected Class<?> findClass(String className) throws ClassNotFoundException {
                Class<?> clazz = dexFile.loadClass(className, this);
                if (clazz == null && (className.startsWith("com.dodola.rocoofix") || className.startsWith("com.lody.legend") || className.startsWith("com.alipay.euler.andfix"))) {
                    return Class.forName(className);
                }
                if (clazz == null) {
                    throw new ClassNotFoundException(className);
                }
                return clazz;
            }
        };
        Enumeration<String> entrys = dexFile.entries();
        Class<?> clazz = null;
        while (entrys.hasMoreElements()) {
            String entry = entrys.nextElement();
            clazz = dexFile.loadClass(entry, patchClassLoader);
            if (clazz != null) {
                fixClass(clazz, classLoader);
            }
        }
    } catch (IOException e) {
    }
}
Also used : IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) DexFile(dalvik.system.DexFile) File(java.io.File) DexFile(dalvik.system.DexFile)

Example 45 with DexFile

use of dalvik.system.DexFile in project AndroidLife by CaMnter.

the class ClassUtils method tryLoadInstantRunDexFile.

/**
 * Get instant run dex path, used to catch the branch usingApkSplits=false.
 *
 * 尝试读取 instant run 的全部 dex 文件
 *
 * >= 5.0
 * 直接获取 applicationInfo.splitSourceDirs
 *
 * < 5.0
 * 1. 开始反射实例化 instant run 包的 Paths
 * 2. 反射调用 Paths # getDexFileDirectory 获取到 dex 文件夹
 * 3. 然后拿到每个 .dex 文件
 */
private static List<String> tryLoadInstantRunDexFile(ApplicationInfo applicationInfo) {
    List<String> instantRunSourcePaths = new ArrayList<>();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && null != applicationInfo.splitSourceDirs) {
        // add the splite apk, normally for InstantRun, and newest version.
        instantRunSourcePaths.addAll(Arrays.asList(applicationInfo.splitSourceDirs));
        Log.d("ARouter", "Found InstantRun support");
    } else {
        try {
            // This man is reflection from Google instant run sdk, he will tell me where the dex files go.
            Class pathsByInstantRun = Class.forName("com.android.tools.fd.runtime.Paths");
            Method getDexFileDirectory = pathsByInstantRun.getMethod("getDexFileDirectory", String.class);
            String instantRunDexPath = (String) getDexFileDirectory.invoke(null, applicationInfo.packageName);
            File instantRunFilePath = new File(instantRunDexPath);
            if (instantRunFilePath.exists() && instantRunFilePath.isDirectory()) {
                File[] dexFile = instantRunFilePath.listFiles();
                for (File file : dexFile) {
                    if (null != file && file.exists() && file.isFile() && file.getName().endsWith(".dex")) {
                        instantRunSourcePaths.add(file.getAbsolutePath());
                    }
                }
                Log.d("ARouter", "Found InstantRun support");
            }
        } catch (Exception e) {
            Log.e("ARouter", "InstantRun support error, " + e.getMessage());
        }
    }
    return instantRunSourcePaths;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) DexFile(dalvik.system.DexFile) File(java.io.File) IOException(java.io.IOException)

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