-
-
[旧帖] [分享]android DexClassLoader动态加载技术学习笔记 0.00雪花
-
发表于: 2015-5-21 20:18 1413
-
android DexClassLoader动态加载技术学习笔记
android DexClassLoader动态加载技术学习笔记
类装载器DexClassLoader类结构
继承关系:
A class loader that loads classes from .jar and .apk files containing a classes.dex entry. This can be used to execute code not installed as part of an application.
This class loader requires an application-private, writable directory to cache optimized classes. Use Context.getDir(String, int) to create such a directory:
File dexOutputDir = context.getDir("dex", 0);
Do not cache optimized classes on external storage. External storage does not provide access controls necessary to protect your application from code injection attacks.
翻译:这个类加载器用来从.jar和.apk类型的文件内部加载classes.dex文件。通过这种方式可以用来执行非安装的程序代码,作为程序的一部分进行运行。
这个装载类需要一个程序私有的,可写的文件目录去存放优化后的classes文件。通过Contexct.getDir(String, int)来创建
这个目录:
File dexOutputDir = context.getDir("dex", 0);
不要把优化优化后的classes文件存放到外部存储设备上,防代码注入攻击。
这个类只有一个构造函数:
public DexClassLoader (String dexPath, String optimizedDirectory, String libraryPath, ClassLoader parent)
Added in API level 3
Creates a DexClassLoader that finds interpreted and native code. Interpreted classes are found in a set of DEX files contained in Jar or APK files.
创建一个DexClassLoader用来找出指定的类和本地代码(c/c++代码)。用来解释执行在DEX文件中的class文件。
路径的分隔符使用通过System的属性 path.separator 获得 :.
String separeater = System.getProperty("path.separtor");
或者使用File.separator来使用路径分隔符
Parameters
dexPath 需要装载的APK或者Jar文件的路径。包含多个路径用File.pathSeparator间隔开,在Android上默认是 ":"
optimizedDirectory 优化后的dex文件存放目录,不能为null
libraryPath 目标类中使用的C/C++库的列表,每个目录用File.pathSeparator间隔开; 可以为 null
parent 该类装载器的父装载器,一般用当前执行类的装载器
获得指定的apk的安装的目录,dex的解压缩目录,c/c++库的目录.
File file = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "testdex.jar");
(我在这里把dex目录和apk都放到了sd卡的绝对路径上,用sd卡的根目录来作为apk的安装目录,为安全的话可以放到apk的根目录里面,下文注释中写的有方法)
final File optimizedDexOutputPath = getDir("temp", Context.MODE_PRIVATE);
(dex解压到/data/local/tmp,将文件夹的权限设置为私有,可读写)
DexClassLoader classLoader = new DexClassLoader(file.getAbsolutePath(),
optimizedDexOutputPath.getAbsolutePath(), null,
getClassLoader());
//创建类加载器,把dex加载到虚拟机中
创建一个 DexClassLoader实例
Class<?> iclass = classLoader.loadClass("com.demo.dex.IClass");
构造指定类的构造方法 Constructor<?> istructor = iclass.getConstructor(Context.class);
加载指定的类返回一个Class
Method method = iclass.getMethod("call", null);
然后使用反射调用这个Class
String data = (String) method.invoke(istructor.newInstance(this), null);
注释:下面是指定apk指定路径的方法
//获得包管理器
PackageManager pm = getPackageManager();
List<ResolveInfo> resolveinfoes = pm.queryIntentActivities(intent, 0);
//获得指定的activity的信息
ActivityInfo actInfo = resolveinfoes.get(0).activityInfo;
//获得包名
String pacageName = actInfo.packageName;
//获得apk的目录或者jar的目录
String apkPath = actInfo.applicationInfo.sourceDir;
//dex解压后的目录,注意,这个用宿主程序的目录,android中只允许程序读取写自己
//目录下的文件
String dexOutputDir = getApplicationInfo().dataDir;
//native代码的目录
String libPath = actInfo.applicationInfo.nativeLibraryDir;
//创建类加载器,把dex加载到虚拟机中
DexClassLoader calssLoader = new DexClassLoader(apkPath, dexOutputDir, libPath,
this.getClass().getClassLoader());
新手主要针对dexclassloder学习总结,对invoke反射学习,也代码很短,都加了注释,写个学习笔记,以便于记忆。菜鸟学习笔记,请大牛扶正
源代码地址
链接: http://pan.baidu.com/s/1pJ2xNWv 密码: z6h8
android DexClassLoader动态加载技术学习笔记
类装载器DexClassLoader类结构
继承关系:
A class loader that loads classes from .jar and .apk files containing a classes.dex entry. This can be used to execute code not installed as part of an application.
This class loader requires an application-private, writable directory to cache optimized classes. Use Context.getDir(String, int) to create such a directory:
File dexOutputDir = context.getDir("dex", 0);
Do not cache optimized classes on external storage. External storage does not provide access controls necessary to protect your application from code injection attacks.
翻译:这个类加载器用来从.jar和.apk类型的文件内部加载classes.dex文件。通过这种方式可以用来执行非安装的程序代码,作为程序的一部分进行运行。
这个装载类需要一个程序私有的,可写的文件目录去存放优化后的classes文件。通过Contexct.getDir(String, int)来创建
这个目录:
File dexOutputDir = context.getDir("dex", 0);
不要把优化优化后的classes文件存放到外部存储设备上,防代码注入攻击。
这个类只有一个构造函数:
public DexClassLoader (String dexPath, String optimizedDirectory, String libraryPath, ClassLoader parent)
Added in API level 3
Creates a DexClassLoader that finds interpreted and native code. Interpreted classes are found in a set of DEX files contained in Jar or APK files.
创建一个DexClassLoader用来找出指定的类和本地代码(c/c++代码)。用来解释执行在DEX文件中的class文件。
路径的分隔符使用通过System的属性 path.separator 获得 :.
String separeater = System.getProperty("path.separtor");
或者使用File.separator来使用路径分隔符
Parameters
dexPath 需要装载的APK或者Jar文件的路径。包含多个路径用File.pathSeparator间隔开,在Android上默认是 ":"
optimizedDirectory 优化后的dex文件存放目录,不能为null
libraryPath 目标类中使用的C/C++库的列表,每个目录用File.pathSeparator间隔开; 可以为 null
parent 该类装载器的父装载器,一般用当前执行类的装载器
获得指定的apk的安装的目录,dex的解压缩目录,c/c++库的目录.
File file = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "testdex.jar");
(我在这里把dex目录和apk都放到了sd卡的绝对路径上,用sd卡的根目录来作为apk的安装目录,为安全的话可以放到apk的根目录里面,下文注释中写的有方法)
final File optimizedDexOutputPath = getDir("temp", Context.MODE_PRIVATE);
(dex解压到/data/local/tmp,将文件夹的权限设置为私有,可读写)
DexClassLoader classLoader = new DexClassLoader(file.getAbsolutePath(),
optimizedDexOutputPath.getAbsolutePath(), null,
getClassLoader());
//创建类加载器,把dex加载到虚拟机中
创建一个 DexClassLoader实例
Class<?> iclass = classLoader.loadClass("com.demo.dex.IClass");
构造指定类的构造方法 Constructor<?> istructor = iclass.getConstructor(Context.class);
加载指定的类返回一个Class
Method method = iclass.getMethod("call", null);
然后使用反射调用这个Class
String data = (String) method.invoke(istructor.newInstance(this), null);
注释:下面是指定apk指定路径的方法
//获得包管理器
PackageManager pm = getPackageManager();
List<ResolveInfo> resolveinfoes = pm.queryIntentActivities(intent, 0);
//获得指定的activity的信息
ActivityInfo actInfo = resolveinfoes.get(0).activityInfo;
//获得包名
String pacageName = actInfo.packageName;
//获得apk的目录或者jar的目录
String apkPath = actInfo.applicationInfo.sourceDir;
//dex解压后的目录,注意,这个用宿主程序的目录,android中只允许程序读取写自己
//目录下的文件
String dexOutputDir = getApplicationInfo().dataDir;
//native代码的目录
String libPath = actInfo.applicationInfo.nativeLibraryDir;
//创建类加载器,把dex加载到虚拟机中
DexClassLoader calssLoader = new DexClassLoader(apkPath, dexOutputDir, libPath,
this.getClass().getClassLoader());
新手主要针对dexclassloder学习总结,对invoke反射学习,也代码很短,都加了注释,写个学习笔记,以便于记忆。菜鸟学习笔记,请大牛扶正
源代码地址
链接: http://pan.baidu.com/s/1pJ2xNWv 密码: z6h8
赞赏
看原图
赞赏
雪币:
留言: