Search in sources :

Example 31 with DexFile

use of dalvik.system.DexFile in project Headline by kb18519142009.

the class BaseApplication method getAllActivities.

/**
 * 初始化 路由表
 */
private void getAllActivities() {
    try {
        DexFile dexFile = new DexFile(this.getPackageResourcePath());
        Enumeration entries = dexFile.entries();
        while (entries.hasMoreElements()) {
            String entryName = (String) entries.nextElement();
            // 开始匹配Activity
            if (entryName.contains("activity") && entryName.contains("Activity")) {
                // 通过反射获得Activity类
                Class entryClass = Class.forName(entryName);
                if (entryClass.isAnnotationPresent(Action.class)) {
                    Action action = (Action) entryClass.getAnnotation(Action.class);
                    this.mapping.put(action.value(), entryClass);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : Action(com.jia.base.annotation.Action) Enumeration(java.util.Enumeration) IOException(java.io.IOException) DexFile(dalvik.system.DexFile)

Example 32 with DexFile

use of dalvik.system.DexFile in project flyway by flyway.

the class AndroidScanner method scanForClasses.

public Class<?>[] scanForClasses(Location location, Class<?> implementedInterface) throws Exception {
    String pkg = location.getPath().replace("/", ".");
    List<Class> classes = new ArrayList<Class>();
    DexFile dex = new DexFile(context.getApplicationInfo().sourceDir);
    Enumeration<String> entries = dex.entries();
    while (entries.hasMoreElements()) {
        String className = entries.nextElement();
        if (className.startsWith(pkg)) {
            Class<?> clazz = classLoader.loadClass(className);
            if (Modifier.isAbstract(clazz.getModifiers())) {
                LOG.debug("Skipping abstract class: " + className);
                continue;
            }
            if (!implementedInterface.isAssignableFrom(clazz)) {
                continue;
            }
            try {
                ClassUtils.instantiate(className, classLoader);
            } catch (Exception e) {
                throw new FlywayException("Unable to instantiate class: " + className);
            }
            classes.add(clazz);
            LOG.debug("Found class: " + className);
        }
    }
    return classes.toArray(new Class<?>[classes.size()]);
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ArrayList(java.util.ArrayList) DexFile(dalvik.system.DexFile) FlywayException(org.flywaydb.core.api.FlywayException)

Example 33 with DexFile

use of dalvik.system.DexFile in project cucumber-jvm by cucumber.

the class DexClassFinderTest method beforeEachTest.

@Before
public void beforeEachTest() throws IOException {
    dexFile = new DexFile("notImportant");
    dexClassFinder = new DexClassFinder(dexFile);
}
Also used : DexFile(dalvik.system.DexFile) ShadowDexFile(cucumber.runtime.android.shadow.ShadowDexFile) Before(org.junit.Before)

Example 34 with DexFile

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

the class BundleArchiveRevision method findClass.

Class<?> findClass(String className, ClassLoader cl) throws ClassNotFoundException {
    try {
        Class<?> clazz = null;
        if (AtlasHacks.LexFile != null && AtlasHacks.LexFile.getmClass() != null) {
            if (dexClassLoader == null) {
                File libDir = new File(RuntimeVariables.androidApplication.getFilesDir().getParentFile(), "lib");
                dexClassLoader = new DexClassLoader(bundleFile.getAbsolutePath(), revisionDir.getAbsolutePath(), libDir.getAbsolutePath(), cl) {

                    @Override
                    public String findLibrary(String name) {
                        String path = super.findLibrary(name);
                        if (TextUtils.isEmpty(path)) {
                            String fileName = System.mapLibraryName(name);
                            File soFile = findSoLibrary(fileName);
                            if (soFile != null && soFile.exists()) {
                                return soFile.getAbsolutePath();
                            }
                            try {
                                return (String) AtlasHacks.ClassLoader_findLibrary.invoke(Framework.getSystemClassLoader(), name);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return null;
                        } else {
                            return path;
                        }
                    }
                };
            }
            clazz = (Class<?>) AtlasHacks.DexClassLoader_findClass.invoke(dexClassLoader, className);
            return clazz;
        }
        if (isDexOpted() == false) {
            optDexFile();
        }
        if (dexFile == null) {
            //                loadDex(new File(revisionDir, BUNDLE_ODEX_FILE));
            optDexFile();
        }
        clazz = dexFile.loadClass(className, cl);
        return clazz;
    } catch (IllegalArgumentException e) {
    } catch (InvocationTargetException e) {
    } catch (Exception e) {
        if (e instanceof ClassNotFoundException) {
        //do nothing
        } else if (e instanceof DexLoadException) {
            throw (DexLoadException) e;
        } else {
            Log.e("Framework", "Exception while find class in archive revision: " + bundleFile.getAbsolutePath(), e);
        }
    }
    return null;
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) DexFile(dalvik.system.DexFile) ZipFile(java.util.zip.ZipFile) MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 35 with DexFile

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

the class BundleArchiveRevision method optDexFile.

public synchronized void optDexFile() {
    if (isDexOpted()) {
        return;
    }
    if (AtlasHacks.LexFile != null && AtlasHacks.LexFile.getmClass() != null) {
        //yunos
        // TODO: need also cover logic of filelocks for YunOS.
        new DexClassLoader(bundleFile.getAbsolutePath(), revisionDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
        isDexOptDone = true;
        return;
    }
    File odexFile = new File(revisionDir, BUNDLE_ODEX_FILE);
    long START = 0;
    START = System.currentTimeMillis();
    try {
        if (AtlasFileLock.getInstance().LockExclusive(odexFile) == false) {
            Log.e("Framework", "Failed to get file lock for " + bundleFile.getAbsolutePath());
        }
        if (dexFile == null) {
            dexFile = com.taobao.android.runtime.RuntimeUtils.loadDex(RuntimeVariables.androidApplication, bundleFile.getAbsolutePath(), odexFile.getAbsolutePath(), 0);
        //                dexFile = DexFile.loadDex(bundleFile.getAbsolutePath(), odexFile.getAbsolutePath(), 0);
        }
        //9月份版本明天发布先不集成
        //            isDexOptDone = checkDexValid(dexFile);
        isDexOptDone = true;
    } catch (IOException e) {
        AtlasMonitor.getInstance().trace(AtlasMonitor.DEXOPT_FAIL, location, AtlasMonitor.DEXOPT_FAIL_MSG, FileUtils.getDataAvailableSpace());
        if (odexFile.exists()) {
            odexFile.delete();
        }
        Log.e("Framework", "Failed optDexFile '" + bundleFile.getAbsolutePath() + "' >>> ", e);
    } finally {
        AtlasFileLock.getInstance().unLock(odexFile);
    }
    Log.e("Framework", "bundle archieve dexopt bundle " + bundleFile.getAbsolutePath() + " cost time = " + (System.currentTimeMillis() - START) + " ms");
    return;
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) DexFile(dalvik.system.DexFile) ZipFile(java.util.zip.ZipFile)

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