【网站源码 带采集】【app换皮肤源码】【文本过滤系统源码】pathclassloader源码
1.pathclassloaderådexclassloaderçåºå«
2.DexClassLoaderåPathClassLoaderçåºå«
pathclassloaderådexclassloaderçåºå«
pathclassloaderådexclassloaderçåºå«å¦ä¸ï¼
1.DexClassLoaderåPathClassLoaderé½å±äºç¬¦åå亲å§æ´¾æ¨¡åçç±»å è½½å¨ï¼å 为å®ä»¬æ²¡æéè½½loadClassæ¹æ³ï¼ãä¹å°±æ¯è¯´ï¼å®ä»¬å¨å è½½ä¸ä¸ªç±»ä¹åï¼åå»æ£æ¥èªå·±ä»¥åèªå·±ä»¥ä¸çç±»å è½½å¨æ¯å¦å·²ç»å è½½äºè¿ä¸ªç±»ãå¦æå·²ç»å è½½è¿äºï¼å°±ä¼ç´æ¥å°ä¹è¿åï¼èä¸ä¼éå¤å è½½ã
2.DexClassLoaderåPathClassLoaderå ¶å®é½æ¯éè¿DexFileè¿ä¸ªç±»æ¥å®ç°ç±»å è½½çãè¿ééè¦é¡ºä¾¿æä¸ä¸çæ¯ï¼Dalvikèææºè¯å«çæ¯dexæ件ï¼èä¸æ¯classæ件ãå æ¤ï¼æ们ä¾ç±»å è½½çæ件ä¹åªè½æ¯dexæ件ï¼æè å å«ædexæ件ç.apkæ.jaræ件ã
3.PathClassLoaderæ¯éè¿æé å½æ°new DexFile(path)æ¥äº§çDexFile对象çï¼èDexClassLoaderåæ¯éè¿å ¶éææ¹æ³loadDexï¼path,网站源码 带采集 outpath, 0ï¼å¾å°DexFile对象ãè¿ä¸¤è çåºå«å¨äºDexClassLoaderéè¦æä¾ä¸ä¸ªå¯åçoutpathè·¯å¾ï¼ç¨æ¥éæ¾.apkå æè .jarå ä¸çdexæ件ãæ¢ä¸ªè¯´æ³æ¥è¯´ï¼å°±æ¯PathClassLoaderä¸è½ä¸»å¨ä»zipå ä¸éæ¾åºdexï¼å æ¤åªæ¯æç´æ¥æä½dexæ ¼å¼æ件ï¼æè å·²ç»å®è£ çapkï¼å 为已ç»å®è£ çapkå¨cacheä¸åå¨ç¼åçdexæ件ï¼ãèDexClassLoaderå¯ä»¥æ¯æ.apkã.jarå.dexæ件ï¼å¹¶ä¸ä¼å¨æå®çoutpathè·¯å¾éæ¾åºdexæ件ã
4.å¦å¤ï¼PathClassLoaderå¨å 载类æ¶è°ç¨çæ¯DexFileçloadClassBinaryNameï¼èDexClassLoaderè°ç¨çæ¯loadClassãå æ¤ï¼å¨ä½¿ç¨PathClassLoaderæ¶ç±»å ¨åéè¦ç¨â/âæ¿æ¢â.â
DexClassLoaderåPathClassLoaderçåºå«
å¨ä½¿ç¨Javaèææºæ¶ï¼æ们ç»å¸¸èªå®ä¹ç»§æ¿èªClassLoaderçç±»å è½½å¨ãç¶åéè¿defineClassæ¹æ³æ¥ä»ä¸ä¸ªäºè¿å¶æµä¸å è½½Classãèå¨Androidä¸æ们æ æ³è¿ä¹ä½¿ç¨ï¼Androidä¸ClassLoaderçdefineClassæ¹æ³å ·ä½æ¯è°ç¨VMClassLoaderçdefineClassæ¬å°éææ¹æ³ãèè¿ä¸ªæ¬å°æ¹æ³ä»ä¹é½æ²¡åï¼åªæ¯æåºäºä¸ä¸ªâUnsupportedOperationExceptionâå¼å¸¸ã
æ¢ç¶å¨Dalvikèææºéï¼ClassLoaderä¸å¥½ç¨ï¼é£ä¹Androidå®æ¹ä¸ºäºè§£å³è¿ä¸ªé®é¢ï¼å¸®æ们ä»ClassLoaderä¸æ´¾çåºäºä¸¤ä¸ªç±»ï¼DexClassLoaderåPathClassLoaderãåä¸ç两è å¾åï¼é£ä¹ç©¶ç«äºè å¨ä½¿ç¨ä¸é¢æä½ä¸åï¼è¿éæå大家ä¸èµ·æ¢è®¨ä¸ä¸ã
é¦å æ¥çä¸ä¸äºè çæé æ¹æ³
DexClassLoader
public DexClassLoader (String dexPath, String dexOutputDir, String libPath, ClassLoader parent)
åæ°è¯¦è§£ï¼
dexPathï¼dexæ件路å¾å表ï¼å¤ä¸ªè·¯å¾ä½¿ç¨â:âåé
dexOutputDirï¼ç»è¿ä¼åçdexæ件ï¼odexï¼æ件è¾åºç®å½
libPathï¼å¨æåºè·¯å¾ï¼å°è¢«æ·»å å°appå¨æåºæ索路å¾å表ä¸ï¼
parentï¼è¿æ¯ä¸ä¸ªClassLoaderï¼è¿ä¸ªåæ°ç主è¦ä½ç¨æ¯ä¿çjavaä¸ClassLoaderçå§ææºå¶ï¼ä¼å ç¶ç±»å è½½å¨å è½½classesï¼ç±ä¸èä¸çå è½½æºå¶ï¼é²æ¢éå¤å 载类åèç ï¼
DexClassLoaderæ¯ä¸ä¸ªå¯ä»¥ä»å å«classes.dexå®ä½ç.jaræ.apkæ件ä¸å è½½classesçç±»å è½½å¨ãå¯ä»¥ç¨äºå®ç°dexçå¨æå è½½ã代ç çæ´æ°ççãè¿ä¸ªç±»å è½½å¨å¿ é¡»è¦ä¸ä¸ªappçç§æãå¯åç®å½æ¥ç¼åç»è¿ä¼åçclassesï¼odexæ件ï¼ï¼ä½¿ç¨Context.getDir(String, int)æ¹æ³å¯ä»¥å建ä¸ä¸ªè¿æ ·çç®å½ï¼ä¾å¦ï¼
File dexOutputDir = context.getDir(âdexâ, 0);
PathClassLoader
PathClassLoaderæä¾ä¸¤ä¸ªå¸¸ç¨æé æ¹æ³
public PathClassLoader (String path, ClassLoader parent)
public PathClassLoader (String path, String libPath, ClassLoader parent)
åæ°è¯¦è§£ï¼
pathï¼æ件æè ç®å½çå表
libPathï¼å å«libåºçç®å½å表
parentï¼ç¶ç±»å è½½å¨
PathClassLoaderæä¾ä¸ä¸ªç®åçClassLoaderå®ç°ï¼å¯ä»¥æä½å¨æ¬å°æ件系ç»çæ件å表æç®å½ä¸çclassesï¼ä½ä¸å¯ä»¥ä»ç½ç»ä¸å è½½classesã
为äºä¾¿äºç解ï¼æ们æ¥çä¸ä¸äºè çæºç ï¼
è¿éåå¾çæè¿°
// DexClassLoader.java
public class DexClassLoader extends BaseDexClassLoader {
public DexClassLoader(String dexPath, String optimizedDirectory,
String libraryPath, ClassLoader parent) {
super(dexPath, new File(optimizedDirectory), libraryPath, parent);
}
}
// çæææï¼ç´åæ¬æ¥çæå µ/mynameishuangshuai
// PathClassLoader.java
public class PathClassLoader extends BaseDexClassLoader {
public PathClassLoader(String dexPath, ClassLoader parent) {
super(dexPath, null, null, parent);
}
public PathClassLoader(String dexPath, String libraryPath,
ClassLoader parent) {
super(dexPath, null, libraryPath, parent);
}
}
å¾ææ¾ä¸¤è é½ç»§æ¿äºBaseDexClassLoaderç±»ï¼å¹¶åäºä¸ä¸å°è£ ï¼å ·ä½çå®ç°è¿æ¯å¨ç¶ç±»éãä¸é¾çåºï¼ä¸»è¦çåºå«å¨äºPathClassLoaderçoptimizedDirectoryåæ°åªè½æ¯nullï¼é£ä¹optimizedDirectoryæ¯åä»ä¹ç¨çå¢ï¼æ们è¿BaseDexClassLoaderå»ççè¿ä¸ªåæ°ã
public BaseDexClassLoader(String dexPath, File optimizedDirectory,
String libraryPath, ClassLoader parent) {
super(parent);
this.originalPath = dexPath;
this.pathList = new DexPathList(this, dexPath, libraryPath, optimizedDirectory);
}
代ç ä¸ä¸optimizedDirectoryæå ³çå°æ¹æ¯new ä¸ä¸ªDexPathListå®ä¾ã
public DexPathList(ClassLoader definingContext, String dexPath,
String libraryPath, File optimizedDirectory) {
â¦â¦
this.dexElements = makeDexElements(splitDexPath(dexPath), optimizedDirectory);
}
private static Element[] makeDexElements(ArrayList<File> files,
File optimizedDirectory) {
ArrayList<Element> elements = new ArrayList<Element>();
for (File file : files) {
ZipFile zip = null;
DexFile dex = null;
String name = file.getName();
if (name.endsWith(DEX_SUFFIX)) {
dex = loadDexFile(file, optimizedDirectory);
} else if (name.endsWith(APK_SUFFIX) || name.endsWith(JAR_SUFFIX)
|| name.endsWith(ZIP_SUFFIX)) {
zip = new ZipFile(file);
}
â¦â¦
if ((zip != null) || (dex != null)) {
elements.add(new Element(file, zip, dex));
}
}
return elements.toArray(new Element[elements.size()]);
}
private static DexFile loadDexFile(File file, File optimizedDirectory)
throws IOException {
if (optimizedDirectory == null) {
return new DexFile(file);
} else {
String optimizedPath = optimizedPathFor(file, optimizedDirectory);
return DexFile.loadDex(file.getPath(), optimizedPath, 0);
}
}
/
*** Converts a dex/jar file path and an output directory to an
* output file path for an associated optimized dex file.
*/
private static String optimizedPathFor(File path,
File optimizedDirectory) {
String fileName = path.getName();
if (!fileName.endsWith(DEX_SUFFIX)) {
int lastDot = fileName.lastIndexOf(".");
if (lastDot < 0) {
fileName += DEX_SUFFIX;
} else {
StringBuilder sb = new StringBuilder(lastDot + 4);
sb.append(fileName, 0, lastDot);
sb.append(DEX_SUFFIX);
fileName = sb.toString();
}
}
File result = new File(optimizedDirectory, fileName);
return result.getPath();
}
optimizedDirectoryæ¯ç¨æ¥ç¼åæ们éè¦å è½½çdexæ件çï¼å¹¶å建ä¸ä¸ªDexFile对象ï¼å¦æå®ä¸ºnullï¼é£ä¹ä¼ç´æ¥ä½¿ç¨dexæ件åæçè·¯å¾æ¥å建DexFile
对象ã
optimizedDirectoryå¿ é¡»æ¯ä¸ä¸ªå é¨åå¨è·¯å¾ï¼æ 论åªç§å¨æå è½½ï¼å è½½çå¯æ§è¡æ件ä¸å®è¦åæ¾å¨å é¨åå¨ãDexClassLoaderå¯ä»¥æå®èªå·±çoptimizedDirectoryï¼æ以å®å¯ä»¥å è½½å¤é¨çdexï¼å 为è¿ä¸ªdexä¼è¢«å¤å¶å°å é¨è·¯å¾çoptimizedDirectoryï¼èPathClassLoader没æoptimizedDirectoryï¼æ以å®åªè½å è½½å é¨çdexï¼è¿äºå¤§é½æ¯åå¨ç³»ç»ä¸å·²ç»å®è£ è¿çapkéé¢çã
éè¿ä»¥ä¸çåæï¼æ们å¯ä»¥å¾åºäºè åè½ä¸çåºå«
DexClassLoaderï¼è½å¤å è½½æªå®è£ çjar/apk/dex
PathClassLoaderï¼åªè½å 载系ç»ä¸å·²ç»å®è£ è¿çapk