Search in sources :

Example 1 with DexFile

use of dalvik.system.DexFile in project jmonkeyengine by jMonkeyEngine.

the class MainActivity method onCreate.

/**
     * Called when the activity is first created.
     */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        Log.d(TAG, "Restoring selections in onCreate: " + "position: " + savedInstanceState.getInt(SELECTED_LIST_POSITION, 0) + "class: " + savedInstanceState.getString(SELECTED_APP_CLASS));
        currentPosition = savedInstanceState.getInt(SELECTED_LIST_POSITION, 0);
        currentSelection = savedInstanceState.getString(SELECTED_APP_CLASS);
        enableMouseEvents = savedInstanceState.getBoolean(ENABLE_MOUSE_EVENTS, true);
        enableJoystickEvents = savedInstanceState.getBoolean(ENABLE_JOYSTICK_EVENTS, false);
        enableKeyEvents = savedInstanceState.getBoolean(ENABLE_KEY_EVENTS, true);
        verboseLogging = savedInstanceState.getBoolean(VERBOSE_LOGGING, true);
    }
    /* Set content view and register views */
    setContentView(R.layout.test_chooser_layout);
    btnOK = (Button) findViewById(R.id.btnOK);
    btnCancel = (Button) findViewById(R.id.btnCancel);
    listClasses = (ListView) findViewById(R.id.listClasses);
    editFilterText = (EditText) findViewById(R.id.txtFilter);
    /* Define the root package to start with */
    rootPackage = "jme3test";
    /* Create an array of Strings to define which classes to exclude */
    // inner classes
    exclusions.add("$");
    // Desktop test chooser class
    exclusions.add("TestChooser");
    // Desktop test chooser class
    exclusions.add("awt");
    //        mExclusions.add("");
    /*
         * Read the class names from the dex file and filter based on
         * name and super class.
         */
    Log.d(TAG, "Composing Test list...");
    ApplicationInfo ai = this.getApplicationInfo();
    String classPath = ai.sourceDir;
    DexFile dex = null;
    Enumeration<String> apkClassNames = null;
    try {
        dex = new DexFile(classPath);
        apkClassNames = dex.entries();
        while (apkClassNames.hasMoreElements()) {
            String className = apkClassNames.nextElement();
            if (checkClassName(className) && checkClassType(className)) {
                classNames.add(className);
            }
        //            	classNames.add(className);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            dex.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
         * Create a backing Adapter for the List View from a list of the
         * classes. The list is defined by array of class names.
         */
    arrayAdapter = new CustomArrayAdapter(this, // text view to display selection
    R.layout.test_chooser_row, // array of strings to display
    classNames);
    /* Set the resource id for selected and non selected backgrounds */
    Log.d(TAG, "Setting Adapter Background Resource IDs");
    arrayAdapter.setSelectedBackgroundResource(R.drawable.selected);
    arrayAdapter.setNonSelectedBackgroundResource(R.drawable.nonselected);
    /* Attach the Adapter to the spinner */
    Log.d(TAG, "Setting ListView Adapter");
    listClasses.setAdapter(arrayAdapter);
    /* Set initial selection for the list */
    setSelection(currentPosition);
    /* Set Click and Text Changed listeners */
    listClasses.setOnItemClickListener(this);
    btnOK.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
    editFilterText.addTextChangedListener(this);
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) IOException(java.io.IOException) DexFile(dalvik.system.DexFile)

Example 2 with DexFile

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

the class TinkerParallelDexOptimizer method optimizeAllLocked.

private static boolean optimizeAllLocked(Collection<File> dexFiles, File optimizedDir, AtomicInteger successCount, ResultCallback cb) {
    final CountDownLatch lauch = new CountDownLatch(dexFiles.size());
    final ExecutorService threadPool = Executors.newCachedThreadPool();
    long startTick = System.nanoTime();
    for (File dexFile : dexFiles) {
        OptimizeWorker worker = new OptimizeWorker(dexFile, optimizedDir, successCount, lauch, cb);
        threadPool.submit(worker);
    }
    try {
        lauch.await();
        long timeCost = (System.nanoTime() - startTick) / 1000000;
        if (successCount.get() == dexFiles.size()) {
            Log.i(TAG, "All dexes are optimized successfully, cost: " + timeCost + " ms.");
            return true;
        } else {
            Log.e(TAG, "Dexes optimizing failed, some dexes are not optimized.");
            return false;
        }
    } catch (InterruptedException e) {
        Log.w(TAG, "Dex optimizing was interrupted.", e);
        return false;
    } finally {
        threadPool.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) DexFile(dalvik.system.DexFile) File(java.io.File)

Example 3 with DexFile

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

the class DexDiffPatchInternal method patchDexExtractViaDexDiff.

private static boolean patchDexExtractViaDexDiff(Context context, String patchVersionDirectory, String meta, final File patchFile) {
    String dir = patchVersionDirectory + "/" + DEX_PATH + "/";
    if (!extractDexDiffInternals(context, dir, meta, patchFile, TYPE_DEX)) {
        TinkerLog.w(TAG, "patch recover, extractDiffInternals fail");
        return false;
    }
    final Tinker manager = Tinker.with(context);
    File dexFiles = new File(dir);
    File[] files = dexFiles.listFiles();
    optFiles.clear();
    if (files != null) {
        final String optimizeDexDirectory = patchVersionDirectory + "/" + DEX_OPTIMIZE_PATH + "/";
        File optimizeDexDirectoryFile = new File(optimizeDexDirectory);
        if (!optimizeDexDirectoryFile.exists() && !optimizeDexDirectoryFile.mkdirs()) {
            TinkerLog.w(TAG, "patch recover, make optimizeDexDirectoryFile fail");
            return false;
        }
        // add opt files
        for (File file : files) {
            String outputPathName = SharePatchFileUtil.optimizedPathFor(file, optimizeDexDirectoryFile);
            optFiles.add(new File(outputPathName));
        }
        TinkerLog.w(TAG, "patch recover, try to optimize dex file count:%d", files.length);
        // only use parallel dex optimizer for art
        if (ShareTinkerInternals.isVmArt()) {
            failOptDexFile.clear();
            // try parallel dex optimizer
            TinkerParallelDexOptimizer.optimizeAll(files, optimizeDexDirectoryFile, new TinkerParallelDexOptimizer.ResultCallback() {

                long startTime;

                @Override
                public void onStart(File dexFile, File optimizedDir) {
                    startTime = System.currentTimeMillis();
                    TinkerLog.i(TAG, "start to parallel optimize dex %s, size: %d", dexFile.getPath(), dexFile.length());
                }

                @Override
                public void onSuccess(File dexFile, File optimizedDir, File optimizedFile) {
                    // Do nothing.
                    TinkerLog.i(TAG, "success to parallel optimize dex %s, opt file size: %d, use time %d", dexFile.getPath(), optimizedFile.length(), (System.currentTimeMillis() - startTime));
                }

                @Override
                public void onFailed(File dexFile, File optimizedDir, Throwable thr) {
                    TinkerLog.i(TAG, "fail to parallel optimize dex %s use time %d", dexFile.getPath(), (System.currentTimeMillis() - startTime));
                    failOptDexFile.add(dexFile);
                }
            });
            // try again
            for (File retryDexFile : failOptDexFile) {
                try {
                    String outputPathName = SharePatchFileUtil.optimizedPathFor(retryDexFile, optimizeDexDirectoryFile);
                    if (!SharePatchFileUtil.isLegalFile(retryDexFile)) {
                        manager.getPatchReporter().onPatchDexOptFail(patchFile, retryDexFile, optimizeDexDirectory, retryDexFile.getName(), new TinkerRuntimeException("retry dex optimize file is not exist, name: " + retryDexFile.getName()));
                        return false;
                    }
                    TinkerLog.i(TAG, "try to retry dex optimize file, path: %s, size: %d", retryDexFile.getPath(), retryDexFile.length());
                    long start = System.currentTimeMillis();
                    DexFile.loadDex(retryDexFile.getAbsolutePath(), outputPathName, 0);
                    TinkerLog.i(TAG, "success retry dex optimize file, path: %s, opt file size: %d, use time: %d", retryDexFile.getPath(), new File(outputPathName).length(), (System.currentTimeMillis() - start));
                } catch (Throwable e) {
                    TinkerLog.e(TAG, "retry dex optimize or load failed, path:" + retryDexFile.getPath());
                    manager.getPatchReporter().onPatchDexOptFail(patchFile, retryDexFile, optimizeDexDirectory, retryDexFile.getName(), e);
                    return false;
                }
            }
        // for dalvik, machine hardware performance is much worse than art machine
        } else {
            for (File file : files) {
                try {
                    String outputPathName = SharePatchFileUtil.optimizedPathFor(file, optimizeDexDirectoryFile);
                    long start = System.currentTimeMillis();
                    DexFile.loadDex(file.getAbsolutePath(), outputPathName, 0);
                    TinkerLog.i(TAG, "success single dex optimize file, path: %s, opt file size: %d, use time: %d", file.getPath(), new File(outputPathName).length(), (System.currentTimeMillis() - start));
                } catch (Throwable e) {
                    TinkerLog.e(TAG, "single dex optimize or load failed, path:" + file.getPath());
                    manager.getPatchReporter().onPatchDexOptFail(patchFile, file, optimizeDexDirectory, file.getName(), e);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) TinkerParallelDexOptimizer(com.tencent.tinker.loader.TinkerParallelDexOptimizer) Tinker(com.tencent.tinker.lib.tinker.Tinker) ZipFile(java.util.zip.ZipFile) DexFile(dalvik.system.DexFile) File(java.io.File)

Example 4 with DexFile

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

the class KernalBundle method patchKernalDex.

public void patchKernalDex() throws Exception {
    DexFile[] dexFile = archive.getOdexFile();
    if (dexFile != null || archive.getLibraryDirectory().exists()) {
        installKernalBundle(KernalConstants.baseContext.getClassLoader(), archive);
        Class FrameworkPropertiesClazz = KernalConstants.baseContext.getClassLoader().loadClass("android.taobao.atlas.framework.FrameworkProperties");
        Field versionField = FrameworkPropertiesClazz.getDeclaredField("version");
        versionField.setAccessible(true);
        String version = (String) versionField.get(FrameworkPropertiesClazz.newInstance());
        if (!KernalVersionManager.instance().CURRENT_VERSIONAME.equals(version)) {
            if (isDeubgMode()) {
                Log.e("KernalBundle", "main dex is not match, awo test?");
            } else {
                throw new RuntimeException("maindex version is not mismatch");
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) DexFile(dalvik.system.DexFile)

Example 5 with DexFile

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

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)

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