2016-04-08 5 views
9

मैं एक पैकेज है कि कुछ इस तरह देखा में सभी वर्गों की एक सूची प्राप्त करने के लिए कुछ कोड था लोड नहीं करने के लिए:एंड्रॉयड स्टूडियो 2.0 त्वरित रन DexFile का कारण बनता है सभी वर्गों

try { 
    DexFile df = new DexFile(context.getPackageCodePath()); 
    for (Enumeration<String> iter = df.entries(); iter.hasMoreElements();) { 
     String s = iter.nextElement(); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

हालांकि इस कोड काम बंद कर दिया है के बाद से मैंने अपने एंड्रॉइड स्टूडियो को संस्करण 2.0 में अपग्रेड किया। मैंने पाया है कि अपराधी तत्काल रन है। यदि मैं ऐप डीबग करता हूं तो मैं देख सकता हूं कि उदाहरण के बिना, डीएक्सफ़ाइल वैरिएबल, डीएफ में कक्षा के नामों की एक सूची है (उनमें से 4,000 से अधिक)। जब तत्काल रन चालू होता है, तो मुझे केवल 30 या उससे अधिक वर्ग के नाम मिलते हैं, और जिन कक्षाओं को मैं ढूंढ रहा हूं वे वहां नहीं हैं। मुझे लगता है कि इसमें बहु डेक्स के साथ कुछ करना है, लेकिन मुझे यकीन नहीं है कि इंस्टेंट रन कवर के तहत कैसे काम कर रहा है (मेरा ऐप मल्टीडेक्स का उपयोग नहीं करता है)।

किसी को भी पता है कि कैसे मैं इस तरह की कक्षाओं की एक सूची प्राप्त कर सकते हैं के साथ त्वरित रन पर दिया? या क्या किसी को पता है कि मैं इस व्यवहार को क्यों देख रहा हूं (इसे समझना अच्छा होगा)?

+0

कृपया इस [जवाब] एक नज़र (http://stackoverflow.com/questions/36572515/dexfile-in-2-0-versions-of-android-studio-and ले -gradle)। InstantRun DexFile तर्क तोड़ दिया। – Sol

उत्तर

4

हम DEX अनुप्रयोग डेटा पथ में तत्काल रन द्वारा निर्मित फ़ाइलों को संभाल सकता है।

public class MultiDexHelper { 

private static final String EXTRACTED_NAME_EXT = ".classes"; 
private static final String EXTRACTED_SUFFIX = ".zip"; 

private static final String SECONDARY_FOLDER_NAME = "code_cache" + File.separator + 
     "secondary-dexes"; 

private static final String PREFS_FILE = "multidex.version"; 
private static final String KEY_DEX_NUMBER = "dex.number"; 

private static SharedPreferences getMultiDexPreferences(Context context) { 
    return context.getSharedPreferences(PREFS_FILE, Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? 
      Context.MODE_PRIVATE : 
      Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS); 
} 

/** 
* get all the dex path 
* 
* @param context the application context 
* @return all the dex path 
* @throws PackageManager.NameNotFoundException 
* @throws IOException 
*/ 
public static List<String> getSourcePaths(Context context) throws PackageManager.NameNotFoundException, IOException { 
    ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0); 
    File sourceApk = new File(applicationInfo.sourceDir); 
    File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME); 

    if (LogUtil.isDebugModeEnable()) { 
     LogUtil.d("MultiDexHelper", 
        "getSourcePaths sourceDir=" + applicationInfo.sourceDir + ", dataDir=" + applicationInfo.dataDir); 
    } 

    List<String> sourcePaths = new ArrayList<String>(); 
    sourcePaths.add(applicationInfo.sourceDir); //add the default apk path 

    //the prefix of extracted file, ie: test.classes 
    String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT; 
    //the total dex numbers 
    int totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1); 

    if (LogUtil.isDebugModeEnable()) { 
     LogUtil.d("MultiDexHelper", "getSourcePaths totalDexNumber=" + totalDexNumber); 
    } 

    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 
     } else { 
      throw new IOException("Missing extracted secondary dex file '" + 
              extractedFile.getPath() + "'"); 
     } 
    } 
    try { 
     // handle dex files built by instant run 
     File instantRunFilePath = new File(applicationInfo.dataDir, 
              "files" + File.separator + "instant-run" + File.separator + "dex"); 
     if (LogUtil.isDebugModeEnable()) { 
      LogUtil.d("MultiDexHelper", "getSourcePaths instantRunFile exists=" + instantRunFilePath.exists() + ", isDirectory=" 
        + instantRunFilePath.isDirectory() + ", getAbsolutePath=" + instantRunFilePath.getAbsolutePath()); 
     } 
     if (instantRunFilePath.exists() && instantRunFilePath.isDirectory()) { 
      File[] sliceFiles = instantRunFilePath.listFiles(); 
      for (File sliceFile : sliceFiles) { 
       if (null != sliceFile && sliceFile.exists() && sliceFile.isFile() && sliceFile.getName().endsWith(".dex")) { 
        sourcePaths.add(sliceFile.getAbsolutePath()); 
       } 
      } 
     } 
    } catch (Throwable e) { 
     LogUtil.e("MultiDexHelper", "getSourcePaths parse instantRunFilePath exception", e); 
    } 

    return sourcePaths; 
} 

// /** 
// * 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 
// */ 
// public static List<String> getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException { 
//  List<String> classNames = new ArrayList<String>(); 
//  for (String path : getSourcePaths(context)) { 
//   try { 
//    DexFile dexfile = null; 
//    if (path.endsWith(EXTRACTED_SUFFIX)) { 
//     //NOT use new DexFile(path), because it will throw "permission error in /data/dalvik-cache" 
//     dexfile = DexFile.loadDex(path, path + ".tmp", 0); 
//    } else { 
//     dexfile = new DexFile(path); 
//    } 
//    Enumeration<String> dexEntries = dexfile.entries(); 
//    while (dexEntries.hasMoreElements()) { 
//     classNames.add(dexEntries.nextElement()); 
//    } 
//   } catch (IOException e) { 
//    throw new IOException("Error at loading dex file '" + 
//           path + "'"); 
//   } 
//  } 
//  return classNames; 
// } 

/** 
* scan parent class's sub classes 
* 
* @param context 
* @param packageName 
* @param parentClass 
* @param <T> 
* @return 
*/ 
public static <T> Set<Class<? extends T>> scanClasses(Context context, String packageName, Class<T> parentClass) { 
    Set<Class<? extends T>> classes = new HashSet<Class<? extends T>>(); 
    try { 
     ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
     for (String path : getSourcePaths(context)) { 
      if (LogUtil.isDebugModeEnable()) { 
       LogUtil.d("MultiDexHelper", "scanClasses path=" + path); 
      } 
      try { 
       DexFile dexfile = null; 
       if (path.endsWith(EXTRACTED_SUFFIX)) { 
        //NOT use new DexFile(path), because it will throw "permission error in /data/dalvik-cache" 
        dexfile = DexFile.loadDex(path, path + ".tmp", 0); 
       } else { 
        dexfile = new DexFile(path); 
       } 
       Enumeration<String> dexEntries = dexfile.entries(); 
       while (dexEntries.hasMoreElements()) { 
        String className = dexEntries.nextElement(); 
        if (LogUtil.isDebugModeEnable()) { 
         LogUtil.d("MultiDexHelper", "scanClasses className=" + className); 
        } 
        if (className.toLowerCase().startsWith(packageName.toLowerCase())) { 
         Class clazz = classLoader.loadClass(className); 
         if (LogUtil.isDebugModeEnable()) { 
          LogUtil.d("MultiDexHelper", 
             "scanClasses clazz=" + clazz + ", parentClass=" + parentClass + ", equals=" + clazz 
               .getSuperclass().equals(parentClass)); 
         } 
         if (clazz.getSuperclass().equals(parentClass)) { 
          classes.add(clazz); 
         } 
        } 
       } 
      } catch (Throwable e) { 
       LogUtil.e("MultiDexHelper", "scanClasses Error at loading dex file '" + 
         path + "'", e); 
      } 
     } 
    } catch (Throwable e) { 
     LogUtil.e("MultiDexHelper", "scanClasses exception", e); 
    } 
    return classes; 
} 

}

संबंधित मुद्दे